Integrate BrowserStack Automate with Jenkins

Integrate your Selenium test suite with Jenkins and the BrowserStack Selenium grid using our plugin.

Introduction

Jenkins is an open-source Continuous Integration (CI) server, which automates the build and deploy process of your web applications. By running your Selenium test suite in Jenkins, you also automate testing as part of the build process. Add BrowserStack to the mix, and your web apps can be tested across 2000+ real devices and desktop browsers. BrowserStack lets you integrate your Selenium tests with Jenkins using a plugin that connects your Jenkins CI server to the BrowserStack Selenium Grid.

Use the BrowserStack Jenkins plugin to:

  • Configure your BrowserStack credentials for your Jenkins jobs.
  • Set up and tear down the BrowserStack Local binary for testing internal, development, and staging environments.
  • Embed BrowserStack test results, including video, logs, and screenshots in your Jenkins job results.

Prerequisites

You need the following set up before you begin:

  • An existing Jenkins CI server (version 1.653+)
  • A BrowserStack account

Installing the Jenkins plugin

To run your Selenium tests with Jenkins on BrowserStack, you will first need to download our Jenkins plugin. Before installing the plugin, ensure you have the necessary privileges to administer your Jenkins installation. We recommend doing this when there are no active build jobs running on Jenkins.

  1. Click on Manage Jenkins > Manage Plugins.
  2. Click on the Available Plugins tab.
  3. In the search box type BrowserStack.
  4. Choose BrowserStack from the list of available plugins. browserstack automate jenkins plugin
  5. Check the box and install the plugin.
  6. Once the plugin has been successfully installed you will be able to see it in the list of Installed plugins. browserstack automate jenkins plugin
Note: If the BrowserStack plugin doesn’t show up in the list of Available plugins, click on the Check Now button to force Jenkins to pull the latest available plugins from Jenkins update center.

Configuring BrowserStack credentials

Once you’ve installed the plugin, you will need to configure your BrowserStack credentials to complete the integration between Jenkins and your Selenium Webdriver tests. This can be done by either using Jenkins UI or using a Jenkinsfile.

Follow these steps to configure BrowserStack inside Jenkins:

  1. Go to Manage Jenkins > Configure System.
  2. Under the BrowserStack section, click on the Add button next to BrowserStack Credentials. browserstack automate jenkins plugin
  3. Enter your BrowserStack Username and Access Key which you can find on your BrowserStack Account Settings page. browserstack automate jenkins plugin
  4. Save your changes.

Once you add your credentials as mentioned in the above section, Jenkins generates an ID which is used in the Jenkinsfile.

From the Jenkins home page, click on Credentials from the left menu. You can copy the ID for BrowserStack credentials. browserstack automate jenkins plugin

Use the sample code in your Jenkinsfile:

browserstack(credentialsId: '<browserstackCredentialsID>') {
    // code for executing test cases
}

The above Jenkinsfile code block can also be generated with the help of Pipeline Syntax generator tool available in Jenkins.

To generate the step snippet with Snippet Generator:

  1. Navigate to the Pipeline Syntax from a configured Pipeline, or at <jenkins-ip>:<port>/pipeline-syntax.
  2. Select the browserstack:BrowserStack in the Sample Step dropdown menu.
  3. Use the dynamically populated area below the Sample Step dropdown to configure the selected step.
  4. Click Generate Pipeline Script to create a snippet of Pipeline which can be copied and pasted into a Pipeline. browserstack automate jenkins plugin

Configuring BrowserStack Local

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 Selenium Grid and your internal servers. The BrowserStack Jenkins plugin is responsible for:

  • Downloading the BrowserStack local binary for every platform that the build job is running on
  • Setting up and tearing down the secure tunnel.
Note: BrowserStack Local can only be configured per job and not via global configuration.

To enable BrowserStack Local follow these steps:

Under the Build Environment section check the box next to BrowserStack Local. browserstack automate jenkins plugin

Note:
  1. If you are using an externally downloaded binary, you can enter its location at BrowserStack Local Path. If left empty, the plugin will automatically download the binary (recommended). This 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.
  2. Use BrowserStack Local Options to set any additional configuration options when running the binary (Full list of options available here)

To enable BrowserStack Local using a Jenkinsfile add localConfig option as shown in the sample code:

browserstack(credentialsId: '<browserstackCredentialsId>', localConfig: [localOptions: '<local-options>', localPath: '/path/to/local']) {
    // code for executing test cases
}
Note:
  1. If you are using an externally downloaded binary, you can enter its location at localPath. If left empty, the plugin will automatically download the binary (recommended). This 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.
  2. Use localOptions to set any additional configuration options when running the binary (Full list of options available here) ) Once you’ve integrated BrowserStack Local through the Jenkinsfile, you can run your Selenium tests on websites hosted on private, development, and internal servers safely and securely.

Once you’ve integrated BrowserStack Local through the Jenkins plugin, you can run your Selenium tests on websites hosted on private, development, and internal servers safely and securely. Refer to the sample code in the below section.

Using Environment variables

The BrowserStack Jenkins plugin sets the following environment variables:

BROWSERSTACK_USERNAME
BROWSERSTACK_ACCESS_KEY
BROWSERSTACK_BUILD_NAME
BROWSERSTACK_LOCAL
BROWSERSTACK_LOCAL_IDENTIFIER

Use these environment variables to set the capabilities in your tests. For example:

String username = System.getenv("BROWSERSTACK_USERNAME");
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
String buildName = System.getenv("BROWSERSTACK_BUILD_NAME");
String browserstackLocal = System.getenv("BROWSERSTACK_LOCAL");
String browserstackLocalIdentifier = System.getenv("BROWSERSTACK_LOCAL_IDENTIFIER");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("os", "Windows");
capabilities.setCapability("os_version", "10");
capabilities.setCapability("browser", "chrome");
capabilities.setCapability("browser_version", "latest");
capabilities.setCapability("name", "BStack-[Java] Sample Test"); // test buildName
capabilities.setCapability("build", buildName); // CI/CD job name using BROWSERSTACK_BUILD_NAME env variable
capabilities.setCapability("browserstack.local", browserstackLocal);
capabilities.setCapability("browserstack.localIdentifier", browserstackLocalIdentifier);
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accessKey + "@hub.browserstack.com/wd/hub"), capabilities);
var username = process.env.BROWSERSTACK_USERNAME;
var accessKey = process.env.BROWSERSTACK_ACCESS_KEY;
var buildName = process.env.BROWSERSTACK_BUILD_NAME;
var browserstackLocal = process.env.BROWSERSTACK_LOCAL;
var browserstackLocalIdentifier = process.env.BROWSERSTACK_LOCAL_IDENTIFIER;

var capabilities = {
	"os" : "Windows",
	"os_version" : "10",
	"browser" : "chrome",
	"browser_version" : "latest",
	"name": "BStack -[Jenkins] Sample Test", // test name
	"build" : buildName, // CI/CD job name using BROWSERSTACK_BUILD_NAME env variable
	"browserstack.local" : browserstackLocal,
	"browserstack.localIdentifier" : browserstackLocalIdentifier,
	"browserstack.user" : username,
	"browserstack.key" : accessKey
};

var driver = new webdriver.Builder().
  usingServer("https://hub-cloud.browserstack.com/wd/hub").
  withCapabilities(capabilities).
  build();
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace SeleniumTest {
  class Program {
    static void Main(string[] args) {
      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");

      IWebDriver driver;
      OpenQA.Selenium.Chrome.ChromeOptions capability = new OpenQA.Selenium.Chrome.ChromeOptions();
      capability.setCapability("os", "Windows", true);
      capability.AddAdditionalCapability("os_version", "10", true);
      capability.AddAdditionalCapability("browser", "Chrome", true);
      capability.AddAdditionalCapability("browser_version", "latest", true);
      capability.AddAdditionalCapability("name", "BStack-[Jenkins] Sample Test", true); // test name
      capability.AddAdditionalCapability("build", buildName, true); // CI/CD job name using BROWSERSTACK_BUILD_NAME env variable
      capability.AddAdditionalCapability("browserstack.local", browserstackLocal, true);
	  capability.AddAdditionalCapability("browserstack.localIdentifier", browserstackLocalIdentifier, true);
      capability.AddAdditionalCapability("browserstack.user", username, true);
      capability.AddAdditionalCapability("browserstack.key", accessKey, true);
            driver = new RemoteWebDriver(
              new Uri("https://hub-cloud.browserstack.com/wd/hub/"), capability
            );

            // test steps
        }
    }
}
$username = getenv("BROWSERSTACK_USERNAME");
$accessKey = getenv("BROWSERSTACK_ACCESS_KEY");
$buildName = getenv("BROWSERSTACK_BUILD_NAME");
$browserstackLocal = getenv("BROWSERSTACK_LOCAL");
$browserstackLocalIdentifier = getenv("BROWSERSTACK_LOCAL_IDENTIFIER");

$caps = array(
  "os" => "Windows",
  "os_version" => "10",
  "browser" => "chrome",
  "browser_version" => "latest",
  "name" => "BStack-[Jenkins] Sample Test", // test name
  "build" => $buildName, // CI/CD job name using BROWSERSTACK_BUILD_NAME env variable
  "browserstack.local" => $browserstackLocal,
  "browserstack.localIdentifier" => $browserstackLocalIdentifier
);

$web_driver = RemoteWebDriver::create(
  "https://" . $username . ":" . $accessKey . "@hub-cloud.browserstack.com/wd/hub",
  $caps
);
username = os.getenv("BROWSERSTACK_USERNAME")
access_key = os.getenv("BROWSERSTACK_ACCESS_KEY")
build_name = os.getenv("BROWSERSTACK_BUILD_NAME")
browserstack_local = os.getenv("BROWSERSTACK_LOCAL")
browserstack_local_identifier = os.getenv("BROWSERSTACK_LOCAL_IDENTIFIER")

caps = {
 'os': 'Windows',
 'os_version': '10',
 'browser': 'chrome',
 'browser_version': 'latest',
 'name': 'BStack-[Jenkins] Sample Test', # test name
 'build': build_name, # CI/CD job name using BROWSERSTACK_BUILD_NAME env variable
 'browserstack.local': browserstack_local,
 'browserstack.localIdentifier': browserstack_local_identifier,
 'browserstack.user': username,
 'browserstack.key': access_key
}

driver = webdriver.Remote(
    command_executor='https://hub-cloud.browserstack.com/wd/hub',
    desired_capabilities=caps)
username = ENV["BROWSERSTACK_USERNAME"]
access_key = ENV["BROWSERSTACK_ACCESS_KEY"]
build_name = ENV["BROWSERSTACK_BUILD_NAME"]
browserstack_local = ENV["BROWSERSTACK_LOCAL"]
browserstack_local_identifier = ENV["BROWSERSTACK_LOCAL_IDENTIFIER"]

caps = Selenium::WebDriver::Remote::Capabilities.new
caps["os"] = "Windows"
caps["os_version"] = "10"
caps["browser"] = "chrome"
caps["browser_version"] = "latest"
caps["name"] = "BStack-[Jenkins] Sample Test" # test name
caps["build"] = build_name # CI/CD job name using BROWSERSTACK_BUILD_NAME env variable
caps["browserstack.local"] = browserstack_local
caps["browserstack.localIdentifier"] = browserstack_local_identifier
caps["browserstack.user"] = username
caps["browserstack.key"] = access_key

driver = Selenium::WebDriver.for(:remote,
  :url => "https://hub-cloud.browserstack.com/wd/hub",
  :desired_capabilities => caps)
my $username = $ENV{"BROWSERSTACK_USERNAME"};
my $accessKey = $ENV{"BROWSERSTACK_ACCESS_KEY"};
my $buildName = $ENV{"BROWSERSTACK_BUILD_NAME"};
my $browserstackLocal = $ENV{"BROWSERSTACK_LOCAL"};
my $browserstackLocalIdentifier = $ENV{"BROWSERSTACK_LOCAL_IDENTIFIER"};

my $caps = {
  "os" => "Windows",
  "os_version" => "10",
  "browser" => "chrome",
  "browser_version" => "latest",
  "name" => "BStack-[Jenkins] Sample Test", # test name
  "build" => $buildName, # CI/CD job name using BROWSERSTACK_BUILD_NAME env variable
  "browserstack.local" => $browserstackLocal,
  "browserstack.localIdentifier" => $browserstackLocalIdentifier
};

my $host = "$username:$accessKey\@hub-cloud.browserstack.com";

my $driver = new Selenium::Remote::Driver('remote_server_addr' => $host,
  'port' => '80', 'extra_capabilities' => $caps);
Note: You must pass browserstack.local and browserstack.localIdentifier capabilities to test on your local development servers.

Viewing test report in Jenkins

The BrowserStack Jenkins plugin enables you to embed the test results from BrowserStack Automate into Jenkins. It shows summary of the build, and a grid of all the sessions of that build. The plugin helps you to debug faster by highlighting failed tests and linking tests to the Automate dashboard.

Warning: Please make sure to use BROWSERSTACK_BUILD_NAME environment variable to set build name in your test code. If you don’t use BROWSERSTACK_BUILD_NAME the plugin will not be able to embed reports in Jenkins. Refer Using Environment variables section on how to use environment variables.

Enabling reporting is a simple process and doesn’t require any code change, follow the below steps:

  1. Click on Add post-build action dropdown in Post-build Actions section browserstack automate jenkins plugin
  2. Select BrowserStack Test Report from the dropdown browserstack automate jenkins plugin
  3. Click Save

Use the code in your Jenkinsfile

browserStackReportPublisher 'automate'

This is a post build step and should be added after the browserstack block in the Jenkinsfile.

With this integration, you can now view the results of your Selenium Webdriver tests within Jenkins. You can know the summary of a build: browserstack automate jenkins plugin

You can view the list of session of a build by clicking on View Full BrowserStack Report or by clicking on BrowserStack Test Report link in the sidebar. browserstack automate jenkins plugin

Selecting any session from this table will take you to the Automate dashboard where you can refer to video logs, text logs, screenshots, and other debugging options for a session.

Note: This plugin also supports integration with the reporting framework in Java - TestNG and JUnit & NodeJS - Protractor and WebdriverIO. Click here to know more about the integration steps.

Conclusion

By following the steps outlined in this guide, you should have a seamless integration between your existing automation in Jenkins, your Selenium tests, and the BrowserStack Selenium grid. This will help you leverage all the benefits of test automation along with the scale and coverage offered by BrowserStack.

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