Skip to main content

Selenium with Serenity

Your guide to running Selenium Webdriver tests with Serenity on BrowserStack.

Note: Code samples in this guide can be found in the serenity-browserstack repo in GitHub.


BrowserStack gives you instant access to our Selenium Grid of 3000+ real devices and desktop browsers. Running your Selenium tests with Serenity on BrowserStack is simple. This guide will help you in :

  1. Running your first test
  2. Integrating your tests with BrowserStack
  3. Marking tests as passed or failed
  4. Debugging your app


  • You need to have BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
  • Before you can start running your Selenium tests with Serenity, ensure you have following libraries installed (It is already included in the sample repo that we will use in this guide):
      <!-- Install using maven -->

Running your first test

Protip: Selenium 4 is now supported on BrowserStack. To use the Selenium 4 client bindings, modify your existing test scripts as follows:
  • Edit or add capabilities in the W3C format using our W3C capability generator.
  • Add the seleniumVersion capability in your test script and set the value to 4.0.0.

To run Selenium tests with Serenity on BrowserStack Automate, follow the below steps:

  1. Clone the serenity-browserstack repo on GitHub with BrowserStack’s sample test, using the following command:

     git clone
     cd serenity-browserstack
  2. Install the dependencies using the following command:

    mvn install
  3. Update file within the serenity-browserstack directory with your BrowserStack credentials as shown below:

     webdriver.driver = provided
     webdriver.provided.type = mydriver
     webdriver.provided.mydriver = com.browserstack.BrowserStackSerenityDriver
     serenity.driver.capabilities = mydriver
     browserstack.key=YOUR_ACCESS_KEY[Serenity] Sample Test
  4. Now, execute your first test on BrowserStack using the following command:

     mvn verify -P single
  5. View your test results on the BrowserStack Automate dashboard.
  • You can use our capability builder and select from a wide range of custom capabilities that BrowserStack supports.
  • You have to pre-pend bstack_ with any capability that you choose and enter it in your file.

Details of your first test

Following is the sample Serenity test case that you ran above. The test searches for “BrowserStack” on Google, and checks if the title of the resulting page is “BrowserStack - Google Search”. You can find the below script in the Serenity BrowserStack sample repository
package com.browserstack.cucumber;

import com.browserstack.BrowserStackSerenityTest;
import cucumber.api.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;

@CucumberOptions(features = "src/test/resources/features/single.feature")
public class SingleTest extends BrowserStackSerenityTest {
// Google Page
public class GooglePage extends PageObject {

    @FindBy(name = "q")
    WebElementFacade search;

    @FindBy(name = "btnK")
    WebElementFacade searchButton;

    public void searchForString(String searchString) {

    public void submitForm() throws Exception {;

    public void titleShouldMatch(String matchTitle) {
Feature: Google's Search Functionality

    Scenario: Can find search results
        When I type query as "BrowserStack"
        And I submit
        Then I should see title "BrowserStack - Google Search"

Integrating your tests with BrowserStack

Your Serenity test case has been integrated to run on BrowserStack using the snippet shown below. A class has been defined which contains the methods to configure and create the connection with BrowserStack. You can find the script in the Serenity BrowserStack sample repository. The module reads from file where you need to put the BrowserStack Hub URL and credentials.
package com.browserstack;

import com.browserstack.local.Local;

import java.util.Map;
import java.util.HashMap;

import org.junit.BeforeClass;
import org.junit.AfterClass;

import net.thucydides.core.util.EnvironmentVariables;
import net.thucydides.core.util.SystemEnvironmentVariables;

public class BrowserStackSerenityTest {
    static Local bsLocal;

    public static void setUp() throws Exception {
        EnvironmentVariables environmentVariables = SystemEnvironmentVariables.createEnvironmentVariables();

        String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
        if (accessKey == null) {
            accessKey = (String) environmentVariables.getProperty("browserstack.key");

        String environment = System.getProperty("environment");
        String key = "bstack_browserstack.local";
        boolean is_local = environmentVariables.getProperty(key) != null
                && environmentVariables.getProperty(key).equals("true");

        if (environment != null && !is_local) {
            key = "environment." + environment + ".browserstack.local";
            is_local = environmentVariables.getProperty(key) != null
                    && environmentVariables.getProperty(key).equals("true");

        if (is_local) {
            bsLocal = new Local();
            Map<String, String> bsLocalArgs = new HashMap<String, String>();
            bsLocalArgs.put("key", accessKey);

    public static void tearDown() throws Exception {
        if (bsLocal != null) {
Note: You can also set the environment variables BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY with your credentials and they would be read by the file above.

Marking tests as passed or failed

BrowserStack provides a comprehensive REST API to access and update information about your tests. Shown below is a sample code snippet which allows you to mark your tests as pass or fail based on the assertions in your Serenity test cases.

// Mark test as pass / fail

public static void mark() throws URISyntaxException, UnsupportedEncodingException, IOException {
  URI uri = new URI("<session-id>.json");
  HttpPut putRequest = new HttpPut(uri);

  ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  nameValuePairs.add((new BasicNameValuePair("status", "<passed/failed>")));
  nameValuePairs.add((new BasicNameValuePair("reason", "The string reason for pass/fail goes here")));
  putRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));


You can find the full reference to our REST API.

Debugging your app

BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests.

Text Logs

Text Logs are a comprehensive record of your test. They are used to identify all the steps executed in the test and troubleshoot errors for the failed step. Text Logs are accessible from the Automate dashboard or via our REST API.

Visual Logs

Visual Logs automatically capture the screenshots generated at every Selenium command run through your Serenity tests. Visual logs help with debugging the exact step and the page where failure occurred. They also help identify any layout or design related issues with your web pages on different browsers.

Visual Logs are disabled by default. In order to enable Visual Logs you will need to set browserstack.debug capability to true in your file:


Sample Visual Logs from Automate Dashboard: BrowserStack Automate Visual Logs

Video Recording

Every test run on the BrowserStack Selenium grid is recorded exactly as it is executed on our remote machine. This feature is particularly helpful whenever a browser test fails. You can access videos from Automate Dashboard for each session. You can also download the videos from the Dashboard or retrieve a link to download the video using our REST API.

Note: Video recording increases test execution time slightly. You can disable this feature by setting the capability to false.
# set this in your file = false

In addition to these logs BrowserStack also provides Raw logs, Network logs, Console logs, Selenium logs, Appium logs and Interactive session. You can find the complete details to enable all the debugging options

Next steps

Once you have successfully run your first test on BrowserStack, you might want to do one of the following:

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?


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
Talk to an Expert