Espresso tests for virtual devices
This section provides instructions on how to run Espresso tests with the Perfecto Gradle Plugin against emulators in Perfecto. It assumes that you are:
- Familiar with Espresso
- 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 Espresso tests for applications.
- 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 Espresso application and test files are available in the local disk storage.
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 .
apk
files on emulators in the Perfecto data center, and running the test methods - Generating output to the Gradle console and Perfecto's single test report (STR) report
A sample project is available here: https://github.com/PerfectoMobileSA/PerfectoEspressoProject
Prerequisites
Before you get started, make sure you have installed the following:
- Mac or Windows machine
- Gradle
- Java
- Android Studio with Android SDK
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).
+
in the classpath to get the latest version of the build, as follows:
com.perfectomobile.instrumentedtest.gradleplugin:plugin:+
1 | Get started
The starting point is a local Android Studio project without Perfecto configuration: https://github.com/PerfectoMobileSA/PerfectoEspressoProject/tree/master/LocalEspresso. Android Studio projects are integrated with Gradle and include several build.gradle
files – one at the project level and one for each application. In the local sample project:
-
The project-level
build.gradle
file is located here:https://github.com/PerfectoMobileSA/PerfectoEspressoProject/blob/master/LocalEspresso/build.gradle
CopyLocal sample project build.gradle file
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
} -
The app-level
build.gradle
file is located here:CopyLocal sample app build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.example.perfecto.tipcalculator" minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0" testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:24.2.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
}
}
To get started:
- Clone the project: https://github.com/PerfectoMobileSA/PerfectoEspressoProject
- Open Android Studio.
- Select the LocalEspresso workspace.
-
Go to File > Settings > Android SDK > SDK Tools and select Show Package Details.
-
Install Android SDK Build-Tools and set the installed version number to
buildToolsVersion
in the app’s build.gradle file. - Right-click the project and select Synchronize LocalEspresso.
- Fix any Gradle-related issues, such as creating a
local.properties
file under base project to set thesdk.dir
andndk.dir
.
2 | Configure the project for Perfecto
In this step, we update both build.gradle
files with the required Perfecto dependencies. We also create a JSON 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/PerfectoEspressoProject/tree/master/PerfectoEspresso. The following procedure walks you through the configuration.
Expand a step to view its content.
In this step, we work with build configuration scripts called build.gradle
. Android Studio projects are integrated with Gradle and include several build.gradle
files – one at the project level and one for each application. In our sample project:
-
The project-level
build.gradle
file is located here:CopyPerfecto sample project build.gradle file
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
google()
maven {
url "https://repo1.perfectomobile.com/public/repositories/maven"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.perfectomobile.instrumentedtest.gradleplugin:plugin:+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
} -
The app-level
build.gradle
file is located here:CopyPerfecto sample app build.gradle file
apply plugin: 'com.android.application'
apply plugin: 'com.perfectomobile.instrumentedtest.gradleplugin'
perfectoGradleSettings {
configFileLocation "configFile.json"}
android {
compileSdkVersion 26
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.example.perfecto.tipcalculator" minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0" testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:24.2.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
}
}
To add the Perfecto plugin dependency to your build.gradle file:
-
In Android Studio, open your project-level
build.gradle
file -
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.Tip: 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 {
google()
jcenter()
maven {
url "https://repo1.perfectomobile.com/public/repositories/maven/"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "com.perfectomobile.instrumentedtest.gradleplugin:plugin:+" // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
} -
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 {
google()
jcenter()
flatDir dirs: 'libs'
}
dependencies {
classpath "com.perfectomobile.instrumentedtest.gradleplugin:plugin:+"
}
}
-
-
Save the file.
-
Open the app-level
build.gradle
file and add the lines 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"
} -
Save the file.
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>>",
"devices": [
{},
{
"platformName": "Android",
"platformVersion": "13",
"model": "pixel.*"
}
],
"jobName": "some_job",
"jobNumber": 1,
"espresso", "plugin"
"branch": "some_branch",
"projectName": "My_Espresso_project",
"projectVersion": "v1.0",
"tags": [
"espresso", "plugin" ],
"apkPath": "app/build/outputs/apk/debug/app-debug.apk",
"testApkPath": "app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk",
"installationDetails" : {"grantAll" : "true"},
"postExecution" : {"uninstall" : "false" },
"debug": false,
"failBuildOnFailure": false,
"takeScreenshotOnTestFailure": true,
"shard": false,
"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": "<cloud name>",
"securityToken": "<SECURITY TOKEN>"where:
-
cloudURL
is the URL of the Perfecto Lab to connect to, but without the.app
notation. 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 Android configuration parameters for the Gradle Plugin | Virtual devices.
If a device has not been explicitly selected, the system selects the default Android device.
For example, add the following to select:
-
Default Android device:
Copy"devices": [
{}
], -
Specific devices, such as two devices where one is a Google Pixel device version 12 and one is the default Android device:
Copy"devices": [
{},
{
"model": "pixel.*",
"platformVersion": "12"
}
], -
A number of default Android devices (useful for sharding):
Copy"numOfDevices": 20
-
-
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"jobName": "some_job",
"jobNumber": 1,
"branch": "some_branch",
"projectName": "My_Espresso_project",
"projectVersion": "v1.0",
"tags": [
"espresso", "plugin"where:
-
is the CI identification of the build, used for classification of the report in the CI Dashboard.jobName
-
jobNumber
is the CI job number of the build. -
branch
is the job branch as reported in the execution context. -
projectName
is the name of the project, for classification. -
projectVersion
is the version number assigned to the project for this build. -
tags
is the set of tags to associate with the execution.
-
-
Add the application parameters to identify where the application files are located.
For our Android application, these areapkPath
andtestApkPath
:Copy"apkPath": "app/build/outputs/apk/debug/app-debug.apk",
"testApkPath": "app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk",where:
-
apkPath
refers to the path of the.apk
file for the application. -
testApkPath
is the path to the UI test application runner.apk
file.
Important: When testing an Android (aar or jar) library, set both the apkPath and testApkPath fields to the location specified for the assembleAndroidTest gradle task. By default, this location is: <project folder>/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk. You can supply additional parameters if you need to limit the test application to only execute specific methods or classes. -
-
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.
Expand a step to view its content.
-
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.
Copygradlew perfecto-android-inst-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 .apk files onto 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 Android configuration parameters for the Gradle Plugin | Virtual devices.
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.CopySample progress report
Task :app:perfecto-android-inst-vd
Parsing configuration file: configFile.json
Parsed configuration file configFile.json successfully
Starting Execution
Uploading files
Files uploaded
Your session id: c97a6958-a21d-43a1-961d-ad91b379c230
[cddc16a32e9e] [pixel 5 - 12] [2023-03-21 08:12:10] Installing app-debug.apk
[cddc16a32e9e] [pixel 5 - 12] [2023-03-21 08:12:10] Installing app-debug-androidTest.apk
[cddc16a32e9e] [pixel 5 - 12] [2023-03-21 08:12:12] Executing test
[aaff75bf6175] [pixel 6 - 13] [2023-03-21 08:12:14] Installing app-debug.apk
[aaff75bf6175] [pixel 6 - 13] [2023-03-21 08:12:14] Installing app-debug-androidTest.apk
[cddc16a32e9e] [pixel 5 - 12] [2023-03-21 08:12:18] [PASS] Class: com.example.myfirstapp.AllFailedButOneTest, Method: test4
[aaff75bf6175] [pixel 6 - 13] [2023-03-21 08:12:18] Executing test
[cddc16a32e9e] [pixel 5 - 12] [2023-03-21 08:12:25] [FAIL] Class: com.example.myfirstapp.AllFailedButOneTest, Method: test5
[aaff75bf6175] [pixel 6 - 13] [2023-03-21 08:12:28] [PASS] Class: com.example.myfirstapp.AllFailedButOneTest, Method: test4
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, installed, and executed, including notifications of the start and completion of:
- 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.
-
Copy
Sample summary report
-----------------------------------------------------
Total Summary
Results:
Total: 14
Passed: 4
Failed: 10
Skipped: 0
Run successfully on 1 devices
-----------------------------------------------------
Device: pixel 6 - 13
ExecutionId: aaff75bf6175
Results:
Total: 7
Passed: 2
Failed: 5
Skipped: 0
-----------------------------------------------------
Report url: https://demo.reporting.perfectomobile.com/reporting/library? tags%5B0%5D%3D==**************************
To access the execution report, copy the report URL from the summary report on your console and open the URL in your browser. For example:
Report url::
https://demo.reporting.perfectomobile.com/reporting/library? tags%5B0%5D%3D==**************************
For more information on Smart Reporting, see Test analysis with Smart Reporting. For details on the STR, see Single test report (STR).
- The report only includes a single test step and possibly some screenshots.
- 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.
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-android-inst-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/Espresso includes different samples that demonstrate how you can use the Perfecto Gradle plugin for the following different modes/configurations:
- defaultAndroidProjectSample: An Android project with the plugin Json config file located in the default place
- localJarSample: An Android project configured to run with the Perfecto plugin jar file locally
- pluginConfigurationSample: An Android project with the cloudURL/security token configured in the
build.gradle
file - remoteRunSample: A sample showing how to run the plugin without the source code, only with .apk files
- configFileSamples: Different examples of configuring the plugin using a
.json
file. Except for devices, all parameters can be overriden by the command line.