BrowserStack integration

How to integrate your workflows with BrowserStack using codemagic.yaml

BrowserStack is a cloud-based mobile testing platform that provides the ability to test your applications on real mobile devices. BrowserStack can be used as a part of your Codemagic CI/CD pipeline to test your applications.

BrowserStack offers two testing environments: App Live and App Automate. You can submit your applications to both testing environments via Codemagic using cURL requests.

You can find sample projects both for the App Live and the App Automate options of BrowserStack in our Sample projects repository.

Configuring BrowserStack in Codemagic

Signing up with BrowserStack is required in order to be able to get the username and access token. You can sign up for free here.

  1. Open your Codemagic app settings, and go to the Environment variables tab.

  2. Enter the desired Variable name, e.g. BROWSERSTACK_USERNAME.

  3. Enter the required value as Variable value.

  4. Enter the variable group name, e.g. browserstack_credentials. Click the button to create the group.

  5. Make sure the Secure option is selected.

  6. Click the Add button to add the variable.

  7. Repeat the process to add the token as BROWSERSTACK_ACCESS_TOKEN

  8. Add the variable group to your codemagic.yaml file

      environment:
        groups:
          - browserstack_credentials

App Live

To use App Live and test your .ipa and .apk artifacts directly on real devices rather than simulators, add the following script to your `codemagic.yaml file:

  scripts:
    - name: Submitting app to Browserstack:
      script: | 
        curl -u "$BROWSERSTACK_USERNAME:$BROWSERSTACK_ACCESS_TOKEN" -X POST "https://api-cloud.browserstack.com/app-live/upload" -F "file=@build/ios/ipa/your_app_release.ipa"
Note: Make sure that you add this cURL request after building the .ipa and .apk, otherwise you cannot attach their paths to the cURL request.

App Automate

In order to use BrowserStack App Automate service through Codemagic, you need to add scripts to your codemagic.yaml file to perform these three steps using REST API endpoints:

  1. Upload your app
  2. Upload test suite
  3. Start testing

In order to upload test suites for android apps, you need to run ./gradlew assembleAndroidTest in your build script. Make sure that your app/build.gradle file includes Instrumentation Runner:

  defaultConfig {
     testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

If you are building your app in release mode, then you also need to build your test suite .apk in release mode by adding the following in app/build.gradle:

    testBuildType "release"

Your codemagic.yaml file will look similar to this:

  scripts:
    - name: Build Android Test release
      script: | 
        cd android # change folder if necessary 
        ./gradlew assembleAndroidTest
    - name: BrowserStack upload
      script: | 
        APP_URL=$(curl -u "$BROWSERSTACK_USERNAME:$BROWSERSTACK_ACCESS_TOKEN" \
          -X POST "https://api-cloud.browserstack.com/app-automate/upload" \
          -F "file=@android/app/build/outputs/apk/release/app-release.apk" \ 
          | jq -r '.app_url') 
    
        TEST_URL=$(curl -u "$BROWSERSTACK_USERNAME:$BROWSERSTACK_ACCESS_TOKEN" \
          -X POST "https://api-cloud.browserstack.com/app-automate/espresso/test-suite" \
          -F "file=@android/app/build/outputs/apk/androidTest/release/app-release-androidTest.apk" \
           | jq -r '.test_url')
     
        curl -X POST "https://api-cloud.browserstack.com/app-automate/espresso/build" \
          -d '{"devices": ["Google Pixel 3-9.0"], "app": "'"$APP_URL"'", "deviceLogs" : true, "testSuite": "'"$TEST_URL"'"}' \
           -H "Content-Type: application/json" -u "$BROWSERSTACK_USERNAME:$BROWSERSTACK_ACCESS_TOKEN"