Execute tests in LambdaTest device cloud

Download the sample app

wget https://github.com/testvagrant/Ekam-Template/releases/download/iosapp/sample_app.ipa

LambdaTest Configuration

Make sure you have a LambdaTest account. Please refer here

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

Where to find it?

  1. Login into LambdaTest Dashboard

  2. Click on the profile icon and choose Account Settings. Open Password & Security Tab.

  3. Get the Username and Access Key

Create a cloud configuration file

Create lambdatest.json file under the src/test/resources/cloud_config folder

lambdatest.json

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

Specify app name and capabalities

Let us specify the name of the app & few mandatory desired capabilities to launch the application

Create file resources/mobile/lambdatest_mobilefeed.json file & edit properties as below

{
    "desiredCapabilities": [
        {
            "app": "sample_app.ipa",
            "platformName": "iOS",
            "automationName": "XCUITest"
        }
    ]
}

Filter devices for test execution

While executing tests in cloud devices, we often want to filters the devices/OS of our choice. We can pass this information by creating a file under resource/mobile as shown below

Under src/test/resources/mobile create a file lambdatest_ios_device_filters.json and specify devices to execute tests on.

{
  "platformVersion": {
    "include": [],
    "operator": "",
    "exclude": []
  },
  "model": {
    "include": [
      "iPhone 11 Pro"
    ],
    "exclude": []
  },
  "udid": {
    "include": [],
    "exclude": []
  }
}

Update config

Let us update mobile.remote.properties config. Or you could choose to create a new configuration file most suitable to you.

Let us add below properties:

  1. mobile.target : Set to iOS to execute tests against iOS
  2. mobile.feed : Feed file having app name and other capabalities
  3. mobile.filters : File specifying devices to use for test execution
  4. cloud.config.dir : Folder having cloud provider details
  5. mobile.hub : File having hub config for cloud (LambdaTest in this case)
  6. mobile.remote.uploadapp : Set to true to upload the app to LambdaTest
# Target mobile platform eg: android | ios
mobile.target: iOS

# Feed file having desiredCapabilities
mobile.feed: lambdatest_mobilefeed

# Filters to chose devices for test execution
mobile.filters: lambdatest_ios_device_filters

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

# cloud provider eg: browserstack | kobiton | etc
mobile.hub: lambdatest

# Upload app before execution <true | false>
mobile.remote.uploadapp: true

Execute tests

Execute tests by specifying the config

./gradlew runMobileTests -Dconfig=mobile.remote

Monitor the LambdaTest dashboard 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 lambdatest.json file, provided an example below:
{
  "username": "${env:LAMBDATEST_USERNAME}",
  "accessKey": "${env:LAMBDATEST_ACCESS_KEY}",
  "hub": "mobile-hub.lambdatest.com"
}
  1. Pass these system properties to Gradle task:
    systemProperty "config", System.getProperty("config")
    systemProperty "LAMBDATEST_USERNAME", System.getProperty("LAMBDATEST_USERNAME")
    systemProperty "LAMBDATEST_ACCESS_KEY", System.getProperty("LAMBDATEST_ACCESS_KEY")
  1. The complete Gradle task
task runMobileTests(type: Test) {
    filter {
        excludeTestsMatching "*.web.*"
        excludeTestsMatching "*.api.*"
        excludeTestsMatching "*.db.*"
    }

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

Execute the test in terminal or CI as below:

./gradlew runMobileTests -Dconfig=mobile.remote -DLAMBDATEST_USERNAME=<username> -DLAMBDATEST_ACCESS_KEY=<access_key>

Execute the test by locking/unlocking the cache:

We can lock and unlock the cache by using the config cloud.lambdatest.cache.lock

  1. true(default): It will lock the device for each test
  2. false: It will randomly select the device for each test

Now lets execute the test by passing the config as below:

./gradlew runMobileTests -Dconfig=mobile.remote -DLAMBDATEST_USERNAME=<username> -DLAMBDATEST_ACCESS_KEY=<access_key> -Dcloud.lambdatest.cache.lock=<true/false>