Skip to main content
Transform your testing process with: Real Device Features, Company-wide Licences, & App Percy
No Result Found
Get your setup working faster. Join our Discord for optimisation tips from elite testers. Join our DiscordJoin our Discord

Use BrowserStack’s Gradle plugin to run Espresso tests

Use BrowserStack’s Gradle plugin to run Espresso tests on BrowserStack App Automate.

BrowserStack provides a Gradle plugin GitHub repository to build, upload, and start Espresso tests on App Automate. You can use this plugin for your development workflow and for easy integration with BrowserStack on a CI/CD server.

Getting started

Step 1: Update build.gradle

In order to configure the plugin, add the following to your app-level build.gradle file:

BrowserStack Espresso Gradle plugin version 3.0.0 requires a JSON configuration file. Specify its file path in configFilePath as shown in the steps below. This version also deprecates the runDebugBuildOnBrowserstack command.

a. Add plugin dependencies

Add the plugin dependency and define the plugin task in the project-level build.gradle file as shown below:

buildscript {
  repositories {
    ...
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.com.browserstack.gradle:browserstack-gradle-plugin:3.0.2"
  }
}
...
apply plugin: "com.browserstack.gradle"
...

Add the following Maven repository and plugin dependency in the project-level build.gradle.kts file as shown below:

allprojects {
  repositories {
    google()
    jcenter()
    maven ("https://plugins.gradle.org/m2/")
  }
}




dependencies {
  implementation("gradle.plugin.com.browserstack.gradle:browserstack-gradle-plugin:3.0.2")
}

b. Add browserStackConfig parameters

Add your BrowserStack access credentials and the file path to the Espresso configuration file in the app-level build.gradle file as shown below. Step 2 explains how to create the configuration file.

browserStackConfig {
    username = "YOUR_USERNAME"
    accessKey = "YOUR_ACCESS_KEY"
    configFilePath = "path/to/your/json/configFile"
}

Add your BrowserStack access credentials and the file path to the Espresso configuration file in the app-level build.gradle.kts file as shown below. Step 2 explains how to create the configuration file.

browserStackConfig { this:BrowserStackConfigExtension
    username = "YOUR_USERNAME"
    accessKey = "YOUR_ACCESS_KEY"
    configFilePath = "path/to/your/json/configFile"
}

Step 2: Create an Espresso configuration file

Create a JSON configuration file that contains all parameters to configure your Espresso test execution on BrowserStack. These parameters include setting the device(s), debugging options and any other App Automate feature you want to use. An example configuration file is shown below:

Select device
This device is not available in the free plan. Please upgrade to paid plan.
 

Step 3: Run the Gradle tasks

To build, upload and execute your Espresso tests, run the command below:

gradle clean executeDebugTestsOnBrowserstack

This executes the following:

  1. Build the app (Debug APK) and Espresso test suite (Test APK), as dependencies are declared on assembleDebug and assembleDebugAndroidTest tasks.
  2. Find the latest app APK and test APK in the app directory recursively.
  3. Upload both the APK files to BrowserStack servers.
  4. Execute Espresso test using the uploaded apps and test suites on the devices specified in the Espresso configuration file.

On successful task execution, the test results are available on the command-line interface and on the App Automate dashboard.

Sample command-line output:

Most recent DebugApp apk: /Users/{username}/AndroidStudioProjects/espresso-browserstack/app/build/outputs/apk/debug/app-debug.apk
Most recent TestApp apk: /Users/{username}/AndroidStudioProjects/espresso-browserstack/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk

App upload Response Code : 200
{"app_url":"bs://83c5dab1adac36004b5f6fc72d9d3136c17e3b5c"}

TestSuite upload Response Code : 200
{"test_url":"bs://83c5dab1adac36004b5f6fc72d9d3136c17e3b5c"}

Response Code : 200
{"message":"Success","build_id":"5a2bac8e53d4786bc2895316badc92299ee22fb9"}
View build status at https://app-automate.browserstack.com/builds/5a2bac8e53d4786bc2895316badc92299ee22fb9

For running tests on a project with build variants, such as product flavors, you can run the following command to build, upload, and run your Espresso tests:

gradle clean execute${buildVariantName}DebugTestsOnBrowserstack

Replace ${buildVariantName} with your build variant name, such as a product flavor. For example, if your productFlavor name is phone and you want to test a debug build type of this variant, the command is:

gradle clean executePhoneDebugTestsOnBrowserstack

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle