Run Tests with Bitbucket Pipelines

Introduction

You can run your automated tests on BrowserStack Automate using your existing Bitbucket pipelines setup. Bitbucket Pipelines uses bitbucket-pipelines.yml for configuration, and the same can be used to run your tests on BrowserStack too.

To run your tests, there are two things that we recommend you to do:

  1. Setup your environment as per your testing needs (for example, setting up the Local Testing connection if you need to)
  2. Set your BrowserStack authentication details as environment variables and read them in your tests

Setting up your test environment

You can simply update your bitbucket-pipleines.yml as per your needs. Here is an example configuration for your reference:

bitbucket-pipeline.yml
image: maven:3.3.9
pipelines:
  default:
    - step:
        script:
          - mvn -version
          - bash run_local.sh # to setup local testing
          - # Your test runner command to run the tests

To know more about how to set up your pipeline for Java, refer to Java with Bitbucket Pipelines page.

Setting and using environment variables

If you are already using environment variables to specify your BrowserStack authentication details, you can skip this step.

If not, we strongly recommend you not to hardcode the auth details in your code (and hence do not commit and check-in your auth keys into code repositories). This helps in both maintaining a secure codebase, as well as helping you flexibly run your tests using different service accounts based on your needs (for example, run pre-prod regression suites using a team account with higher parallels limit).

Go to your Bitbucket repository’s settings and add the following environment variables:

  • BROWSERSTACK_USER: Your BrowserStack username
  • BROWSERSTACK_ACCESSKEY: Your BrowserStack access key

You can then read these variables in your tests. Here is a sample code to demonstrate how that’s done:

String username = System.getenv("BROWSERSTACK_USERNAME");
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("os", "Windows");
capabilities.setCapability("browser", "chrome");
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accessKey + "@hub.browserstack.com/wd/hub"), capabilities);
username = process.env.BROWSERSTACK_USERNAME
accessKey = process.env.BROWSERSTACK_ACCESS_KEY

var capabilities = {
 "os" : "Windows",
 "browser" : "chrome",
 "browserstack.user" : username,
 "browserstack.key" : accessKey
}

var driver = new webdriver.Builder().
  usingServer("https://hub-cloud.browserstack.com/wd/hub").
  withCapabilities(capabilities).
  build();
String username = System.getenv("BROWSERSTACK_USERNAME");
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("os", "Windows");
capabilities.setCapability("browser", "chrome");
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accessKey + "@hub.browserstack.com/wd/hub"), capabilities);
$username = getenv("BROWSERSTACK_USERNAME");
$accessKey = getenv("BROWSERSTACK_ACCESS_KEY");

$caps = array(
  "os" => "Windows",
  "browser" => "chrome"
);

$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")

caps = {
 'os': 'Windows',
 'browser': 'chrome',
 '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"]

caps = Selenium::WebDriver::Remote::Capabilities.new
caps["os"] = "Windows"
caps["browser"] = "chrome"
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 $caps = {
  "os" => "Windows",
  "browser" => "chrome"
};

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

my $driver = new Selenium::Remote::Driver('remote_server_addr' => $host,
  'port' => '80', 'extra_capabilities' => $caps);

And that’s all! You are now all set to run your tests automatically on BrowserStack Automate on every PR or commit as per your workflows.

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