Launch Qt Application under Testing (AUT)
Launch Application
Due to cross-platform Qt's Qt automation mechanism, the normal manual start method does not allow Qt applications to be automated directly by CukeTest. When automating a Qt application or detecting Qt objects, it needs to be opened in a specific way. The following describes the way to start a Qt application in different scenarios.
Launch from the interface
- Click on CukeTest's Main Interface menu "Tools" -> "Start Application..."; or click on the Model Manager interface menu "Operation" -> "Launch Application...";
- Specify the path of an executable
.exe
file, and then check the qt option in the plug-in list; - Click the "Run" button.
In addition, the main interface also provides some special startup entries.
1. Start the built-in sample
In the menu "Tools" -> "Start Sample", sample applications of various technology types are provided for debugging or learning.
2. Listen to running applications (Windows systems only)
For applications started by other methods, monitoring is not performed by default, but you can use the following method to make CukeTest start monitoring the application:
- Click "Operation" → "Start Application..." in the menu bar of the model manager
In the Launch Application dialog box, select the Running Applications tab. Click the Detect button to start the detection.
- Move the cursor to the target Qt application window, then the tool will highlight the window where the mouse is hovering, which is to click the left mouse button.
- A dialog box displays the detected application and its process ID. At this time, click the run button to load the Qt agent for the target Qt application.
At this point, if the Qt agent is successfully loaded, the status bar of the model manager will show the detected Qt information.
Launching applications from scripts
Then we can use the launchQtProcessAsync() method provided by the QtAuto
module in the leanpro.qt
library to launch the application and load the Qt agent when we write the automation script.
Since some of the larger Qt applications take longer to load, you can set a timeout for the Qt plugin at startup and wait for the application to be detected or the window object to be detected before returning after startup so that subsequent operations can be executed normally.
Since the script that starts the application is usually written in the BeforeAll
lifecycle in the hooks.js
file, the script is usually written as follows:
const { BeforeAll } = require('cucumber');
const { Util } = require('leanpro.common');
const { QtAuto } = require('leanpro.qt');
let model = QtAuto.loadModel("<model file path>");
BeforeAll(async function () {
await QtAuto.launchQtProcessAsync("/usr/lib/cuketest/bin/sample", { launchTimeout:15 });
await model.getApplication('<App Name>').exists(10);
})
In the above script, launchTimeout
is the timeout of Qt plugin in seconds, the default value is 10 seconds, it is recommended to set this parameter manually for applications with a launch time greater than 10 seconds; model.getApplication
returns the Application object detected in the model, call this object's exists(10)
method of this object to wait for the application startup to complete, here it will wait up to 10 seconds.
Such a hook would enable the ability to start the sample
application before each project start.
For applications opened by other means, you can call the winHookProcess()
method and pass in the application's process number PID
to start listening, see winHookProcess()
method introduction for details.
Launching applications from the cmd
The cmd direct boot method is only suitable for Linux platforms.
Method 1: In the Model Manager, select "Operations" → "Qt Launch Terminal". In the opened terminal
Method 2: Set a global variable
LD_PRELOAD
before starting the application in any command line. The default path value is:/usr/lib/cuketest/bin/agents/libqtagent.so
(some systems in/opt/apps/com.leanpro.cuketest/files/lib/cuketest/bin/agents/libqtagent.so
). Assuming that the path of the target application is/usr/lib/cuketest/bin/sample
, then the command to start the application is:$> export LD_PRELOAD=/usr/lib/cuketest/bin/agents/libqtagent.so $> /usr/lib/cuketest/bin/sample
Because the environment variables set by using the
export
command on the command line will only take effect in the current command line environment. If it is not a Qt application that is started after setting this environment variable, it will not take effect, see Qt Automation Mechanism for details.
sudo starts the application under test
If the application under test requires sudo permission to run, the environment variable will be switched to the root user accordingly when running with sudo, which will cause the set LD_PRELOAD
to fail, so you need to specify the LD_PRELOAD
environment variable when running sudo, for example :
sudo LD_PRELOAD=/usr/lib/cuketest/bin/agents/libqtagent.so ./yourAppName
If the value of LD_PRELOAD
has been set (you can see a string of paths by using echo $LD_PRELOAD
at this time), then the command started by sudo can be written as:
sudo LD_PRELOAD=$LD_PRELOAD ./yourAppName
yourAppName
is the name/path of the application under test.
Qt application view
If Qt is started in the appropriate manner as described above, the detected Qt information is displayed on the status bar of CukeTest and on the status bar of the Model Manager. Clicking on it will display information about the Qt application, including the application name, path, PID, and the version of Qt used.