Execute Web Tests on BrowserStack cloud

Pre-Requisite

Make sure you have a BrowserStack account. Please refer here

BrowserStack Configuration

To run a test on BrowserStack, we need the respective account’s UserName and AccessKey.

Where to find it?

  1. Login into BrowserStack

  2. Click on the profile icon and Settings

  1. Get the Username and Access Key from Automate section

Create a cloud configuration file

Create browserstack.json file under the cloud_config folder

browserstack.json

{
 "username": "<provide your username>",
 "accessKey": "<provide your access key>",
 "hub": "hub-cloud.browserstack.com"
}

Specify BrowserStack as a hub in configuration

Open web.remote.properties file. Or you could choose to create a new configuration file most suitable to you.

add cloud.config.dir and web.hub property

# Folder name under resources having cloud configurations
cloud.config.dir: cloud_config

# cloud provider eg: browserstack | kobiton | etc
web.hub: browserstack

Execute tests

Execute tests by specifying the config

./gradlew runWebTests -Dconfig=web.remote

Monitor the “Automate” section in BrowserStack for execution details

Avoid clear text credentials in cloud config file

It is not a good practice to specify clear text passwords in any of the source files and configurations. Pass the password as a command-line argument.

  1. Use this syntax "${env: <arg name>}" for the required fields in the browserstack.json file, provided an example below:
{
  "username": "${env:BROWSERSTACK_USERNAME}",
  "accessKey": "${env:BROWSERSTACK_ACCESS_KEY}",
  "hub": "hub-cloud.browserstack.com"
}
  1. Pass these system properties to Gradle task:
    systemProperty "config", System.getProperty("config")
    systemProperty "BROWSERSTACK_USERNAME", System.getProperty("BROWSERSTACK_USERNAME")
    systemProperty "BROWSERSTACK_ACCESS_KEY", System.getProperty("BROWSERSTACK_ACCESS_KEY")
  1. The complete Gradle task
task runWebTests(type: Test) {
    filter {
        excludeTestsMatching "*.mobile.*"
        excludeTestsMatching "*.api.*"
        excludeTestsMatching "*.db.*"
    }
    
    systemProperty "config", System.getProperty("config")
    systemProperty "BROWSERSTACK_USERNAME", System.getProperty("BROWSERSTACK_USERNAME")
    systemProperty "BROWSERSTACK_ACCESS_KEY", System.getProperty("BROWSERSTACK_ACCESS_KEY")

    outputs.upToDateWhen { false }
    useTestNG {
        parallel = "methods"
        threadCount Integer.parseInt(System.getProperty("sessions", "2"))
        includeGroups System.getProperty("tags", "web")
        testLogging.showStandardStreams = true
        useDefaultListeners true
        outputDirectory = file("$buildDir/" + System.getProperty('tags', 'NONE'))
    }
}

Execute the test in terminal or CI as below:

./gradlew runWebTests -Dconfig=web.remote -DBROWSERSTACK_USERNAME=<username> -DBROWSERSTACK_ACCESS_KEY=<access_key>