XCUITest tests for virtual devices
This section provides instructions on how to run XCUITest tests with the Perfecto Gradle Plugin against simulators in Perfecto. It assumes that you are:
- Familiar with XCUITest
- Have existing tests to work with
- Are a novice user of Perfecto
The Perfecto Gradle Plugin allows you to:
- Select a device or multiple devices from the Perfecto Lab to run Apple XCTest/XCUITest.
- Install the application and test files onto the selected devices.
- Run the test methods on the devices.
- See the progress of the test set on the console.
- Access the Report Library to view the results of the tests.
Setting up the Gradle plugin involves these tasks:
- Installing the Gradle plugin to prepare the build.gradle file
- Configuring parameters through a configuration file (recommended).
- Activating the plugin, understanding the output, and connecting to Smart Reporting execution reports
The following steps assume that the Xcode application and test files are available in the local disk storage. In this example, we use Apple Xcode to prepare the application files.
Each Gradle task supports the following actions:
- Reading of Perfecto configuration parameters that select the devices to install and run the instrumentation tests
- Installing the application, testing
.app
files on Perfecto Lab devices, and running the test methods - Generating output to the Gradle console and the Perfecto single test report (STR) report
A sample project is available here: https://github.com/PerfectoMobileSA/PerfectoXCUITestProject
Prerequisites
Before you get started, make sure you have installed the following:
In addition, you need access to the Perfecto Gradle plugin. You can download it either automatically, after adding the required lines of code to the build.gradle
file, as described in step 2 below, or, if your organization does not permit direct download, by pre-downloading it to a local libs folder (see also Install the Perfecto Gradle plugin manually).
Restrictions
The following restrictions apply:
-
The Perfecto Gradle plugin is not backward compatible. Make sure you always use
+
in the classpath to get the latest version of the build. Add the following line to thedependencies
section in the project-levelbuild.gradle
file:Copyclasspath "com.perfectomobile.instrumentedtest.gradleplugin:plugin:+"
-
In Live Stream, no video is available.
1 | Get started
The starting point is a local Xcode project without Perfecto configuration: https://github.com/PerfectoMobileSA/PerfectoXCUITestProject/tree/master/LocalXCUITest.
To get started:
- Clone the project: https://github.com/PerfectoMobileSA/PerfectoXCUITestProject/tree/master/LocalXCUITest
- Open Xcode.
- Choose the LocalXCUITest workspace.
- Perform Product > Clean and Build.
2 | Configure the project for Perfecto
In this step, we prepare the application files and add the following new files to the project:
- A build.gradle file that will specify the required Perfecto dependencies
- A JSON configuration file that holds all Perfecto configurations, including security information, the Perfecto cloud name, Smart Reporting information, and test data.
The updated project is located here: https://github.com/PerfectoMobileSA/PerfectoXCUITestProject/tree/master/PerfectoXCUITest. The following procedure walks you through the configuration.
Expand a step to view its content.
Executing XCUITest and XCTest tests on iOS simulators involves creating an .xctestproducts
file that contains all test products.
You can prepare this file using the Xcode IDE or the xcodebuild command-line tool. Because of a restriction that applies when peparing the files using the Xcode IDE (details are included in the procedure), consider peparing them using the command-line tool.
Click the relevant procedure to expand the steps.
-
Run the following command:
Copyxcodebuild build-for-testing [-project *.xcodeproj | -workspace *.xcworkspace] -destination generic/platform='iOS Simulator' -scheme <YOUR_SCHEME> -testProductsPath <PATH>
-
In Apple Xcode, in the Navigator area on the left, click your project.
-
On the Build Settings tab, under Architectures, set Build Active Architectures Only to No. This will build your project to support all architectures.
-
On the toolbar at the top of the project window, select an iOS simulator device. This sets the scheme destination.
-
From the Product menu, select Perform Action > Build and Export Test Products.
Restriction: If your project is configured to use Test Plan, when using the Xcode IDE method to create the test products, only the selected Test Plan will be included in the test products. Therefore, if you have multiple test plans, first choose which test plan to include in the test product (Product > Test Plan), and then create the.xctestproducts
file. -
Select the export destination for the
.xctestproducts
file, and then click Build and Export.
Optionally, you can upload your test products (the .xctestproducts
file) to the Perfecto repository. This is helpful when you later specify the repository path in the xctestProductsPath
parameter.
To upload test products to the repository:
-
On a macOS and Linux machine, zip the
.xctestproducts
file.Restriction: Windows machines cannot properly compress the.xctestproducts
file. -
Upload the ZIP file to the repository. For details on uploading the file through the Perfecto UI, see Manage the repository.
In this step, we create a build configuration script called build.gradle
that defines the project and its tasks.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
maven {
url "https://repo1.perfectomobile.com/public/repositories/maven" }
}
dependencies {
classpath "com.perfectomobile.instrumentedtest.gradleplugin:plugin:+" }
}
apply plugin: 'com.perfectomobile.instrumentedtest.gradleplugin'
perfectoGradleSettings {
configFileLocation "configFile.json"}
-
Create a new file named
build.gradle
. -
Add the lines that define the location of the plugin library and the dependency on the plugin to the
build.gradle
file. Gradle will look to verify that the plugin is installed before performing the task.This is the recommended configuration method. However, if there are problems using the automatic download method described here, you can download the Perfecto Gradle plugin manually.
Do one of the following, depending on whether the plugin library is already downloaded or you want to locate and download the plugin library automatically:
-
To configure Gradle to automatically locate and download the plugin library, add the following lines to the
build.gradle
file:Copybuildscript {
repositories {
jcenter()
maven {
url "https://repo1.perfectomobile.com/public/repositories/maven" }
}
dependencies {
classpath "com.perfectomobile.instrumentedtest.gradleplugin:plugin:+" }
} -
If the plugin library is already downloaded to a folder (for example the libs sub-folder), add the following lines to the build.gradle file:
Copybuildscript {
repositories {
jcenter()
flatDir dirs: 'libs'
}
dependencies {
classpath "com.perfectomobile.instrumentedtest.gradleplugin:plugin:+" }
}
-
-
Add the line that defines the plugin task:
Copyapply plugin: 'com.perfectomobile.instrumentedtest.gradleplugin'
-
Add the following line to load any configurations from the
configFile.json
configuration file.CopyperfectoGradleSettings {
configFileLocation "configFile.json"}
In this step, you create the JSON text file that contains all configuration settings. This is the recommended practice. Configurations include the URL of the Perfecto Lab, the security token to use, which devices to select, which tags to use, and so on.
{
"cloudURL": "<<cloud name>>",
"securityToken": "<<SECURITY TOKEN>>",
"xctestProductsPath": "Test.xctestproducts",
"devices": [
{
"platformVersion": "15.*",
"model": "iPhone13" },
{
"platformVersion": "16.*",
"model": "iPhone 14" }
],
"tags": [
"xcuitest",
"sample" ],
"projectName": "IOS-XCUITest",
"projectVersion": "v10.9-test",
"appName": "Sample_App",
"jobName": "some_job",
"jobNumber": 1,
"branch": "master",
"failBuildOnFailure": false,
"runUnitTests": true,
"runUiTests": true,
"shard": false,
"takeScreenshotOnTestEnd": true,
"testTimeout": "60000"
}
- Create a configuration file (
configFile.json
) as a JSON text file in a known folder. -
Add the connection parameters to the configuration file.
Copy"cloudURL": "<your cloud url>",
"securityToken": "<your cloud security token>"where:
-
cloudURL
is the URL of the Perfecto Lab to connect to, but without the.app
notation and without protocol information (http://
orhttps://
). For example: mobilecloud.perfectomobile.com (but not mobilecloud.app.perfectomobile.com) -
securityToken
is the tester's personal security token for the Perfecto Lab. See also Generate security tokens.
-
-
Add the device selection parameters to the configuration file, as shown in the following examples. For details on platform-specific parameters, see iOS configuration parameters for the Gradle Plugin.
If a device has not been explicitly selected, the system selects the default iOS device.
The following code samples illustrate how to select any iOS device, specific devices, and a number of default iOS devices.
CopySpecific devices, such as two devices where one is an iPhone 13 device of iOS version 16 (using a regular expression) and one is a default iOS simulator."devices": [
{
"model":"iPhone 13",
"platformVersion":"16.*" },
{}
] -
Add the reporting parameter settings to add tags to the execution report. For more information on adding tags, see Tag-driven reports (RTDD workflow).
Copy"tags": ["plugin", "xctest", "demo"],
"projectName": "playground",
"projectVersion": "1.5",
"jobName": "newFeature",
"jobNumber": "45",where:
-
tags
is the set of tags to associate with the execution. -
projectName
is the name of the project, for classification. -
projectVersion
is the version number assigned to the project for this build. -
jobName
is the CI identification of the build, used for classification of the report in the CI Dashboard. -
jobNumber
is the CI job number of the build.
-
-
Add the test products parameter to identify where the
.xctestproducts
file is located, as follows:Copy"xctestProductsPath": "<path-to-xctestproducts>"
where
is the path to thexctestProductsPath
.xctestproducts
containing the test products. -
Save the configuration file.
3 | Run the plugin and view the report
This step walks you through running the Perfecto Gradle plugin and viewing the test report in Perfecto.
- The report only includes a single test step that includes all commands.
- It is not possible to add custom failure reasons from within tests.
- The video shown is for one execution, but the report artifact you can download includes all executions.
For more information on Smart Reporting, see Test analysis with Smart Reporting. For details on the STR, see Single test report (STR).
Expand a step to view its content.
.xctestproducts
file. If you have not create a ZIP file beforehand, make sure to run the Gradle plugin on a macOS or Linux machine.-
Open a command-line (terminal) window in the folder where you want to execute the plugin.
-
Execute the plugin using the following command:
Here we also supply the full path to the configuration file created in step 3. You can add other configuration parameters (except for device selection) to the command as needed.
Copygradle perfecto-xctest-vd -PconfigFileLocation=configFile.json
This will:
-
Select the devices as specified in the Device selection parameters of the configuration file (or a random device if no specification is provided).
-
Install the application and test the .app files on the device.
-
Run the test methods (based on the configuration parameters).
-
Send output to the console window.
-
Generate an execution report that can be viewed in the Test analysis with Smart Reporting interface.
The command-line parameters can set any of the configuration parameters, except for device selection parameters. For more information, see iOS configuration parameters for the Gradle Plugin.
For information on running the Gradle plugin over a proxy connection, see Proxy connection.
During the execution, the plugin reports on the progress of the execution and the completion of each test method to the command-line window.
CopyStarting a Gradle Daemon (subsequent builds will be faster)
:perfecto-xctest
Parsing configuration file: configFile.json
Parsed configuration file configFile.json successfully
Starting Execution
Files uploaded
Your session id: ***
[71cc7562ccf6] [iPhone 13 - 15.5] [2023-05-16 10:45:52] Executing test.
[71cc7562ccf6] [iPhone 13 - 15.5] [2023-05-16 10:46:23] [FAIL] Class: TestUITests.SampleTests, Method: testFailure
[71cc7562ccf6] [iPhone 13 - 15.5] [2023-05-16 10:46:29] [PASS] Class: TestUITests.TestUITests, Method: testPositiveCase
[71cc7562ccf6] [iPhone 13 - 15.5] [2023-05-16 10:46:33] [PASS] Class: TestUITests.TestUITests, Method: testSample
[71cc7562ccf6] [iPhone 13 - 15.5] [2023-05-16 10:46:50] Execution finished
Test execution finishedAt the end of the execution, the command-line window displays a high-level summary report of the completion status for each device. The report includes:
- Resolution of configuration settings: The configuration file used and the list of the configuration settings for the test run
- Progress notifications as the test is configured and executed, including notifications of the start and completion of:
- Allocating the device
- Installing the application and UI runner files
- Executing the test
- Completion status for each test method on each device
At the end of the summary report, the plugin provides the URL of the single test report (STR) for the execution run.
CopyTotal Summary
Results:
Total: 3
Passed: 2
Failed: 1
Skipped: 0
Run successfully on 1 devices
-----------------------------------------------------
Device: iPhone 13 - 15.5
ExecutionId: 71cc7562ccf6
Results:
Total: 3
Passed: 2
Failed: 1
Skipped: 0
-----------------------------------------------------
Report url: https://demo.app.perfectomobile.com/reporting/library?tags%5B0%5D%3D=***If allocating a device fails, the following error message appears:
CopyFailed to run on 1 devices
The following executions exit with error:
ExecutionId: 66b4a4ff31ef. Message: failed to execute message: Error message -
Proxy connection
If you run the Gradle plugin over a proxy connection, you can supply the proxy information as follows:
-
As Java parameters when activating the plugin. You can use the same Java Proxy parameters also for proxies supporting SSL encryption.
http.proxyHost: The IP address of the proxy
http.proxyPort: The IP port used for the connection
http.proxyUser: The username for connecting to the proxy server
http.proxyPassword: The password for connecting to the proxy server
For example:
Copygradlew perfecto-xctest-vd -DconfigFileLocation=configFile.json -Dhttp.proxyHost=10.0.0.100 -Dhttp.proxyPort=8800 -Dhttp.proxyUser=someUserName -Dhttp.proxyPassword=somePassword
-
As parameters defined explicitly in the
gradle.properties
file. For example:CopysystemProp.http.proxyHost=<http-proxy-host>
systemProp.http.proxyPort=<http-proxy-port>
systemProp.https.proxyHost=<https-proxy-host>
systemProp.https.proxyPort=<https-proxy-port>
Samples of plugin use
The Perfecto GitHub repository https://github.com/PerfectoCode/XcTest/tree/master/samples includes different samples that demonstrate how you can use the Perfecto Gradle plugin for the following different modes/configurations:
- defaultSample: An Xcode project containing the needed files for running the Perfecto XCTest task
- localJarSample: An Xcode project configured to run the Perfecto .jar file locally
- buildGradleConfiguration: An Xcode project with the configuration included in the
build.gradle
file - configFilesSamples: Different examples of configuring the plugin using a
.json
file. Except for devices, all parameters can be overridden by the command line.