Skip to main content
Transform your testing process with: Real Device Features, Company-wide Licences, & App Percy
No Result Found

Integrate BrowserStack App Automate with Jenkins

Integrate your Appium tests with Jenkins using BrowserStack’s plugin.

Jenkins is a continuous integration tool that enables continuous testing, build, and deployment of iterative code changes. CI/CD tools help catch failures ahead of the production stage and mitigate them as they occur.

In this guide, you’ll learn how to:

  1. Install and configure the BrowserStack Jenkins plugin
  2. Integrate Jenkins in your existing test suite
  3. Integrate Jenkins for privately hosted websites
  4. View test reports in Jenkins

Prerequisites

Before you can start integration, ensure that the following tasks are complete.

  • Jenkins CI server (v1.653+) is installed on your machine.
  • Access to the BrowserStack account credentials, namely Access Key and Username.
  • Jenkins server is running locally on the default port http://localhost:8080/, and have created a pipeline.
  • You are a maintainer or owner of your project repository.
  • Jenkinsfile is created in your project repository.

Install and configure the BrowserStack Jenkins plugin

The BrowserStack Jenkins plugin eases the process of configuring Jenkins to run tests on BrowserStack. This section explains how to:

  1. Install the BrowserStack plugin
  2. Configure your BrowserStack credentials in plugin

Step 1: Install the BrowserStack Plugin

To install the BrowserStack plugin, complete the following steps:

  1. Go to your Jenkins page, running by default on http://localhost:8080/, and click Manage jenkins in the left navigation bar.
  2. On the Manage Jenkins page, click Manage Plugins.
  3. On the Plugin Manager page, click the Available tab.
  4. Search for BrowserStack in the search box. choose BrowserStack from the list of available plugins
  5. Select BrowserStack and click either Install without restart or Download now and install after restart, depending on your preference.
  6. On the Installing Plugins/Upgrades page, view the installation status of the plugin.
  7. After installation is successful, click Go back to the top page to configure the plugin.

Step 2: Configure BrowserStack credentials in Jenkins

After you’ve installed the plugin, complete the following steps to configure your BrowserStack credentials in the plugin:

  1. On your Jenkins page (http://localhost:8080/), click Manage Jenkins > Configure System.
  2. Under the BrowserStack Credentials section, click Add > Jenkins.
  3. In the Jenkins Credentials Provider: Jenkins window, enter your Username and Access Key.
  4. Click Authenticate. After the plugin verifies your BrowserStack credentials, a Success text appears.
  5. Click Add at the bottom of the Jenkins Credentials Provider: Jenkins window. You will be redirected to the configuration page. Add credentials
  6. On the configuration page, click Save.
  7. On your Jenkins Dashboard, click Manage Jenkins > Manage Credentials.
  8. In the Credentials section, note down the ID of the credential shown in the ID column. This ID will be used in the Jenkinsfile of your project. Browserstack credentials on jenkins
  9. In the Jenkinsfile of your project, add the browserstack(credentialsId: '<credential_id>') statement and wrap all the test commands inside it, as follows:
Jenkinsfile
Copy icon Copy

The following sample Jenkinsfile defines the setup stage, credential ID, and a sample shell command:

Jenkinsfile
Copy icon Copy

Configure app upload step

Next, you will need to upload your app to BrowserStack servers and for that, you will need to configure the app upload build step. Follow these steps to configure a build step to upload your app to BrowserStack servers:

  1. Go to build job > Configure
  2. In the Build section, click Add build step
  3. Select Upload App to BrowserStack Click on Upload app to BrowserStack option from the build step dropdown

  4. Enter the path to your app file on the local Jenkins server and click Save.

    Uploading application file from local server and clicking save under the build option

Integrate existing test cases

With existing test cases, integrating BrowserStack involves editing your test cases to add BrowserStack capabilities, credentials, and remote URL.

Though your existing test scripts include capabilities, BrowserStack expects specific capabilities, such as browser version, etc. that help determine how tests are run. This example code snippet shows the BrowserStack specific capabilities.

Step 1: Set environment variables in test script

The BrowserStack Jenkins plugin sets the following environment variables:

  • BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY: They are set to your BrowserStack Username and Access Key, respectively, when you configure BrowserStack credentials in Jenkins.
  • BROWSERSTACK_BUILD_NAME: It is generated automatically by Jenkins plugin

Use the following example code snippet to set environment variables and capabilities in your tests:

browserstack.yml
Copy icon Copy
String userName = System.getenv("BROWSERSTACK_USERNAME");
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
String buildName = System.getenv("JENKINS_LABEL");
String app = System.getenv("BROWSERSTACK_APP_ID");

public static void main(String args[]) throws MalformedURLException, InterruptedException {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("app", app);
    caps.setCapability("device", "Samsung Galaxy S8");
    caps.setCapability("os_version", "13.0");
    caps.setCapability("build", buildName);
}

driver = new AndroidDriver(new URL("https://"+userName+":"+accessKey+"@hub-cloud.browserstack.com/wd/hub", caps);
userName = process.env.BROWSERSTACK_USERNAME
accessKey = process.env.BROWSERSTACK_ACCESS_KEY
buildName = process.env.JENKINS_LABEL
app = process.env.BROWSERSTACK_APP_ID

var capabilities = {
    "browserstack.user" : userName,
    "browserstack.key" : accessKey,
    "app" : app,
    "build" : buildName,
    "device" : "Samsung Galaxy S23",
    "os_version" : "13.0",
}

driver = wd.promiseRemote("https://hub-cloud.browserstack.com/wd/hub");

driver
  .init(capabilities)
  //Write your code here
  .fin(function() { return driver.quit(); })
  .done();
userName = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
buildName = Environment.GetEnvironmentVariable("JENKINS_BUILD_LABEL");
app = Environment.GetEnvironmentVariable("BROWSERSTACK_APP_ID");

DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability("browserstack.user", userName);
caps.SetCapability("browserstack.key", accessKey);
caps.SetCapability("app", app);
caps.SetCapability("device", "Samsung Galaxy S23");
caps.SetCapability("os_version", "13");
caps.SetCapability("build", buildName);

AndroidDriver driver = new AndroidDriver(new Uri("https://hub-cloud.browserstack.com/wd/hub"), caps);


$user_name = getenv("BROWSERSTACK_USERNAME")
$access_key = getenv("BROWSERSTACK_ACCESS_KEY")
$buildName = getenv('JENKINS_LABEL');
$app = getenv("BROWSERSTACK_APP_ID")


$capabilities = new DesiredCapabilities();
$capabilities->setCapability("app", app);
$capabilities->setCapability("device", 'Samsung Galaxy S23');
$capabilities->setCapability("os_version", '13');
$capabilities->setCapability("build", buildName);


$driver = RemoteWebDriver::create("https://"+user_name+":"+access_key+"@hub-cloud.browserstack.com/wd/hub", $capabilities);

user_name = os.getenv("BROWSERSTACK_USERNAME")
access_key = os.getenv("BROWSERSTACK_ACCESS_KEY")
build_name = os.getenv("JENKINS_LABEL")

app = os.getenv("BROWSERSTACK_APP_ID")

desired_cap = {
    'app': app,
    'device': 'Samsung Galaxy S23',
    'os_version': '13'
    'build': build_name
}

driver = webdriver.Remote("https://"+user_name+":"+access_key+"@hub-cloud.browserstack.com/wd/hub", desired_cap)

user_name = ENV["BROWSERSTACK_USERNAME"]
access_key = ENV["BROWSERSTACK_ACCESS_KEY"]

build_name = ENV["JENKINS_LABEL"]

app = ENV["BROWSERSTACK_APP_ID"]

desired_caps = {
    'app': app,
    'device': 'Samsung Galaxy S23',
    'os_version': '13.0',
    'build': build_name
}


appium_driver = Appium::Driver.new({
    'caps' => desired_caps,
    'appium_lib' => {
        :server_url => "https://#{user_name}:#{access_key}@hub-cloud.browserstack.com/wd/hub"
    }}, true)

Step 2: Add commands in Jenkinsfile to run test

Add commands in your Jenkinsfile to run your test using the following steps:

  1. Go to the Jenkinsfile of your project.
  2. Add the commands and wrap all the test commands inside the browserStack statement, as follows. To run parallel tests, add the command used to run parallel tests.
Jenkinsfile
Copy icon Copy
Jenkinsfile
Copy icon Copy

Integrate test cases for privately hosted websites

BrowserStack Local Testing allows you to test web applications hosted on private, development, and internal servers. With the Local testing binary, you can create a secure, private connection between the BrowserStack Appium Grid and your internal servers. The BrowserStack Jenkins plugin is responsible for:

If you are using the SDK, you can skip Step 1. The SDK automatically handles localConfig and Browserstack Local Binary path configurations through language bindings. Set “browserstackLocal: true” in your YAML file and trigger your tests using the command in your pipeline script.

  • Downloading the BrowserStack Local binary for every platform that the build job is running on.
  • Setting up and tearing down the secure tunnel.

BrowserStack Local can only be configured per job and not via global configuration.

Step 1: Enable BrowserStack Local in Jenkinsfile

Go to Jenkinsfile of your project and add the localConfig option to the browserstack statement along with the credentials ID as follows:

Adding this option sets the BROWSERSTACK_LOCAL variable to true in your Jenkins pipeline.

Jenkinsfile
browserstack(credentialsId: '<credentials_ID>', localConfig: [localOptions: '<local-options>', localPath: '/path/to/local']) {
    // commands for executing tests
}

Parameters specified within the localConfig parameter are as follows:

Parameter Description Values
localOptions Specify additional configuration options when running the Local binary.

This is an optional parameter.
For example, localOptions: '--force, --proxy-user'. Check out the Local binary flags section to learn about several flags.

Keep it blank to specify none of the configuration.
localPath If you are using an externally downloaded binary, you can enter its location here.

This is an optional parameter.
For example, localPath: '/Users/demo/Downloads/BrowserStackLocal'

Keep it blank if you want BrowserStack Local binary to be downloaded at the current working directory of your Jenkins pipeline. Keeping it blank is recommended especially if you are using Jenkins in master-slave configurations, since the plugin will download the appropriate binary for the build agent OS.

Step 2: Set environment variables in test script

The BrowserStack Jenkins plugin sets the following environment variables:

Use the following example code snippet to set environment variables and capabilities in your tests:

browserstack.yml
Copy icon Copy
String userName = System.getenv("BROWSERSTACK_USERNAME");
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
String browserstackLocal = System.getenv("BROWSERSTACK_LOCAL");
String buildName = System.getenv("BROWSERSTACK_BUILD_NAME");
String browserstackLocalIdentifier = System.getenv("BROWSERSTACK_LOCAL_IDENTIFIER");
String app = System.getenv("BROWSERSTACK_APP_ID");

public static void main(String args[]) throws MalformedURLException, InterruptedException {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("app", app);
    caps.setCapability("device", "Samsung Galaxy S23");
    caps.setCapability("build", buildName);
    caps.setCapability("browserstack.local", browserstackLocal);
    caps.setCapability("browserstack.localIdentifier", browserstackLocalIdentifier);
}

driver = new AndroidDriver(new URL("https://"+userName+":"+accessKey+"@hub-cloud.browserstack.com/wd/hub", caps);

userName = process.env.BROWSERSTACK_USERNAME
accessKey = process.env.BROWSERSTACK_ACCESS_KEY
browserstackLocal = process.env.BROWSERSTACK_LOCAL
buildName = process.env.BROWSERSTACK_BUILD_NAME;
browserstackLocalIdentifier = process.env.BROWSERSTACK_LOCAL_IDENTIFIER
app = process.env.BROWSERSTACK_APP_ID

var capabilities = {
    "browserstack.user" : userName,
    "browserstack.key" : accessKey,
    "app" : app,
    "build" : buildName,
    "device" : "Samsung Galaxy S23",
    "browserstack.local" : browserstackLocal,
    "browserstack.localIdentifier" : browserstackLocalIdentifier
}

driver = wd.promiseRemote("https://hub-cloud.browserstack.com/wd/hub");

driver
  .init(capabilities)
  //Write your code here
  .fin(function() { return driver.quit(); })
  .done();

userName = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
buildName = Environment.GetEnvironmentVariable("BROWSERSTACK_BUILD_NAME");
browserstackLocal = Environment.GetEnvironmentVariable("BROWSERSTACK_LOCAL");
browserstackLocalIdentifier = Environment.GetEnvironmentVariable("BROWSERSTACK_LOCAL_IDENTIFIER");
app = Environment.GetEnvironmentVariable("BROWSERSTACK_APP_ID");

DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability("browserstack.user", userName);
caps.SetCapability("browserstack.key", accessKey);
caps.SetCapability("app", app);
caps.SetCapability("device", "Samsung Galaxy S23");
caps.SetCapability("build", buildName);
caps.SetCapability("browserstack.local", browserstackLocal);
caps.SetCapability("browserstack.localIdentifier", browserstackLocalIdentifier);

AndroidDriver driver = new AndroidDriver(new Uri("https://hub-cloud.browserstack.com/wd/hub"), caps);


$user_name = getenv("BROWSERSTACK_USERNAME")
$access_key = getenv("BROWSERSTACK_ACCESS_KEY")
$browserstack_local = getenv("BROWSERSTACK_LOCAL")
$buildName = getenv("BROWSERSTACK_BUILD_NAME")
$browserstack_local_identifier = getenv("BROWSERSTACK_LOCAL_IDENTIFIER")
$app = getenv("BROWSERSTACK_APP_ID")


$capabilities = new DesiredCapabilities();
$capabilities->setCapability("app", app);
$capabilities->setCapability("device", 'Samsung Galaxy S23');
$capabilities->setCapability("build", buildName);
$capabilities->setCapability("browserstack.local", browserstack_local);
$capabilities->setCapability("browserstack.localIdentifier", browserstack_local_identifier);


$driver = RemoteWebDriver::create("https://"+user_name+":"+access_key+"@hub-cloud.browserstack.com/wd/hub", $capabilities);

user_name = os.getenv("BROWSERSTACK_USERNAME")
access_key = os.getenv("BROWSERSTACK_ACCESS_KEY")
browserstack_local = os.getenv("BROWSERSTACK_LOCAL")
build_name = os.getenv("BROWSERSTACK_BUILD_NAME")
browserstack_local_identifier = os.getenv("BROWSERSTACK_LOCAL_IDENTIFIER")
app = os.getenv("BROWSERSTACK_APP_ID")

desired_cap = {
    'app': app,
    'device': 'Samsung Galaxy S23',
    'browserstack.local': browserstack_local,
    'build': build_name,
    'browserstack.localIdentifier': browserstack_local_identifier
}

driver = webdriver.Remote("https://"+user_name+":"+access_key+"@hub-cloud.browserstack.com/wd/hub", desired_cap)

user_name = ENV["BROWSERSTACK_USERNAME"]
access_key = ENV["BROWSERSTACK_ACCESS_KEY"]
browserstack_local = ENV["BROWSERSTACK_LOCAL"]
build_name = ENV["BROWSERSTACK_BUILD_NAME"]
browserstack_local_identifier = ENV["BROWSERSTACK_LOCAL_IDENTIFIER"]
app = ENV["BROWSERSTACK_APP_ID"]

desired_caps = {
    'app': app,
    'device': 'Samsung Galaxy S8',
    'build': build_name,
    'browserstack.local': browserstack_local,
    'browserstack.localIdentifier': browserstack_local_identifier
}


appium_driver = Appium::Driver.new({
    'caps' => desired_caps,
    'appium_lib' => {
        :server_url => "https://#{user_name}:#{access_key}@hub-cloud.browserstack.com/wd/hub"
    }}, true)

Step 3: Add command in Jenkins to run Local test

You can run Local testing in Jenkins using either of the following options:

  1. Go to the Jenkinsfile of your project repository.
  2. Add the following commands, inside the browserStack statement, to set up the Local binary and run the Local test.

    These commands use the --daemon start and --daemon stop flags to start and stop Local binary respectively as follows. Check out the Binary parameter guide to learn about additional flags used in running Local binary.

Jenkinsfile
pipeline {
agent any
stages {
    stage('setup') {
        steps {
            browserstack(credentialsId: '<credential_ID>', localConfig: [localOptions: '', localPath: '']) {
                // For Linux-based systems, add the following commands in the given console to download the binary, run it, and stop its execution after the test has been executed.
                sh 'wget "https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-x64.zip"'
                sh 'unzip BrowserStackLocal-linux-x64.zip'
                sh './BrowserStackLocal --key $BROWSERSTACK_ACCESS_KEY --daemon start'
                sh '<your_test_commands>'
                sh './BrowserStackLocal --key $BROWSERSTACK_ACCESS_KEY --daemon stop'

                // For macOS-based systems, add the following commands in the given console to download the binary, run it, and stop its execution after the test has been executed.
                sh 'wget "https://www.browserstack.com/browserstack-local/BrowserStackLocal-darwin-x64.zip"'
                sh 'unzip BrowserStackLocal-darwin-x64.zip'
                sh './BrowserStackLocal --key $BROWSERSTACK_ACCESS_KEY --daemon start'
                sh '<your_test_commands>'
                sh './BrowserStackLocal --key $BROWSERSTACK_ACCESS_KEY --daemon stop'

                // For Windows-based systems, add the following commands in the given console to download the binary, run it, and stop its execution after the test has been executed.
                sh 'wget "https://www.browserstack.com/browserstack-local/BrowserStackLocal-win32.zip"'
                sh 'powershell.exe Expand-Archive BrowserStackLocal-win32.zip'
                sh './BrowserStackLocal-win32/BrowserStackLocal.exe --key %BROWSERSTACK_ACCESS_KEY% --daemon start'
                sh '<your-test-commands>'
                sh './BrowserStackLocal --key %BROWSERSTACK_ACCESS_KEY% --daemon stop'
            }
        }
    }
    }
}

If you prefer to manage the Local connection through your test scripts, you can use the language bindings.

With language bindings, the start and stop of local binary is handled in the test script itself. Check out the enabling Local testing using language bindings guide to edit your test scripts.

After you use language binding in your test script, complete the following steps:

  1. Go to the Jenkinsfile of your project repository.
  2. Add the command to run Local test inside the browserStack statement, as follows:
Jenkinsfile
pipeline {
  agent any
  stages {
      stage('setup') {
        steps {
            browserstack(credentialsId: '<credential_ID>', localConfig: [localOptions: '', localPath: '']) {
                // add commands to run Local test
                // For example -----
                // sh 'npm install'
                // sh 'node local.js'
            }
        }
      }
    }
  }

View test reports in Jenkins

The BrowserStack Jenkins plugin enables you to embed the test results from BrowserStack App Automate into Jenkins. The report gives an overall summary of the test build and its sessions.

Ensure that you set the build capability to the BROWSERSTACK_BUILD_NAME environment variable in your test script without which the plugin fails to generate and embed reports in Jenkins. Check out the Using Environment variables section to learn about environment variables.

Enable reporting in Jenkins using the following steps:

  1. Go to the Jenkinsfile of your project.
  2. Add the following code statement after the browserstack block in the Jenkinsfile:
     // Enable reporting in Jenkins
     browserStackReportPublisher 'automate'
    

A sample Jenkinsfile that uses the browserStackReportPublisher 'automate' statement is as follows:

Jenkinsfile
Copy icon Copy

With this integration, you can now view the results of your tests run on BrowserStack within Jenkins.

BrowserStack generated report on Jenkins

Click either View Full BrowserStack Report or BrowserStack Test Report link in the left navigation bar to view the list of test sessions.

BrowserStack Test Report

Selecting any session from this table redirects you to the App Automate dashboard where you can view video recording, text logs, and other debugging options for a session.

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