Skip to main content

Selenium with Cucumber Java

Learn how to run your first Selenium Webdriver test with Cucumber on BrowserStack Automate.

Important: Sample test scripts are available in the cucumber-java-browserstack repository.

The sample test script in this section is compatible with JSON wire protocol-based client bindings. Check out our W3C-based scripts in the selenium-4 branch of the repository.

Introduction

BrowserStack gives you instant access to our Selenium Grid of 3000+ real devices and desktop browsers. Running your Selenium tests with Cucumber on BrowserStack is simple.

This guide will help you:

  1. Run your first test
  2. Understand the details of your test
  3. Mark tests as passed or failed
  4. Debug your app

Prerequisites

  • 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.
  • Maven is installed on your machine, Maven environment variables are set, and Maven bin is added to system path, $PATH. Check out the official website to download the latest version of Maven.
  • Git installed on your machine.
  • Java installed on your machine.

Run your first test

Important: The sample test script in this section is compatible with JSON wire protocol-based client bindings. Check out our W3C-based scripts in the selenium-4 branch of the cucumber-java-browserstack repository.

To run your first Cucumber test on BrowserStack, complete the following steps:

Step 1: Clone the cucumber-java-browserstack sample repo on GitHub using the following command:

   git clone https://github.com/browserstack/cucumber-java-browserstack.git
   cd cucumber-java-browserstack

Step 2: Install the required dependencies by running the following command in your command-line:

    mvn install

Step 3: Set your BrowserStack credentials and browser-device combinations in the parallel.conf.json file in the cucumber-java-browserstack/src/test/resources/conf directory as follows:

This file also contains the capabilities that define the browsers to run the test.

     {
        "server": "hub.browserstack.com",
        "user": "YOUR_USERNAME",
        "key": "YOUR_ACCESS_KEY",

        "capabilities": {
            "bstack:options": {
            "buildName": "cucumber-java-browserstack",
            "debug": "true"
            }
        },

        "environments": [
            {
            "browserName": "chrome"
            },
            {
            "browserName": "firefox"
            },
            {
            "browserName": "safari"
            }
        ]
        }

Alternatively, you can set the environment variables in your system as shown below:

# setx.exe does not set the environment variable in the current command prompt, but it will be available in subsequent command prompts
setx BROWSERSTACK_USERNAME="YOUR_USERNAME"
setx BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"

# Verify whether the variables have been set
echo %BROWSERSTACK_USERNAME%
echo %BROWSERSTACK_ACCESS_KEY%
# Set these values in your ~/.zprofile (zsh) or ~/.profile (bash)
export BROWSERSTACK_USERNAME="YOUR_USERNAME"
export BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"

# Verify whether the variables have been set
echo $BROWSERSTACK_USERNAME
echo $BROWSERSTACK_ACCESS_KEY

Step 4: Run a parallel test using the following command:

     mvn test -P parallel

Step 5: View your tests on BrowserStack on the BrowserStack Automate Dashboard.

Protip: You can use our capability builder and select from a range of custom capabilities that BrowserStack supports.

Understand the details of your test

Learn about how the different components of the test suite work together using the following information.

Understand your first test script

When you run the mvn test -P parallel command, the RunWebDriverCucumberTests.java file within the cucumber-java-browserstack/src/test/java/com/browserstack directory is executed, which uses the Cucumber TestNG Runner to run the StackDemo.feature file.

When the test is triggered, it:

  • Opens the bstackdemo.com website.
  • Adds a product to the cart.
  • Verifies whether the product is added to the cart.
  • Marks the test as passed or failed based on whether the product is available in the cart.
StackDemo.feature
  Feature: BrowserStack Demo

  Scenario: Add product to cart
    Given I am on the website 'https://www.bstackdemo.com'
    When I select a product and click on 'Add to cart' button
    Then the product should be added to cart

Understand the Cucumber Java configuration file

In the sample repository, the configuration file named parallel.conf.json exists in the cucumber-java-browserstack/src/test/resources/conf/ directory of the project. It is preloaded with default configuration for connecting tests to BrowserStack, and sample capabilities, such as browser name and its version, for running tests.

parallel.conf.json
     {
        "server": "hub.browserstack.com",
        "user": "YOUR_USERNAME",
        "key": "YOUR_ACCESS_KEY",

        "capabilities": {
            "bstack:options": {
            "buildName": "cucumber-java-browserstack",
            "debug": "true"
            }
        },

        "environments": [
            {
            "browserName": "chrome"
            },
            {
            "browserName": "firefox"
            },
            {
            "browserName": "safari"
            }
        ]
        }

Integrate your tests with BrowserStack

The RunWebDriverCucumberTests.java file in the cucumber-java-browserstack/src/test/java/com/browserstack directory helps in running the integration of your test with BrowserStack. It uses the Cucumber TestNG Runner to run Cucumber feature files.

RunWebDriverCucumberTests.java

@CucumberOptions(
    glue = "com.browserstack.stepdefs",
    plugin = {
        "pretty",
        "html:reports/tests/cucumber/html",
        "timeline:reports/tests/cucumber/timeline",
        "junit:reports/tests/cucumber/junit/cucumber.xml",
        "testng:reports/tests/cucumber/testng/cucumber.xml",
        "json:reports/tests/cucumber/json/cucumber.json"
    }
)
public class RunWebDriverCucumberTests {

    private TestNGCucumberRunner testNGCucumberRunner;
    private Local local;
    private static final ThreadLocal<ManagedWebDriver> threadLocalWebDriver = new ThreadLocal<>();

    @BeforeClass(alwaysRun = true)
    public void setUpClass() {
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
    }

    private synchronized static void setThreadLocalWebDriver(ManagedWebDriver managedWebDriver) {
        threadLocalWebDriver.set(managedWebDriver);
    }

    public synchronized static ManagedWebDriver getManagedWebDriver() {
        return threadLocalWebDriver.get();
    }

    @Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "scenarios")
    public void feature(PickleWrapper pickleWrapper, FeatureWrapper featureWrapper, ManagedWebDriver managedWebDriver) {
        if(Utility.isLocal(managedWebDriver) && local==null){
            local = new Local();
            Utility.startLocal(local, managedWebDriver);
        }
        managedWebDriver.setTestName(pickleWrapper.getPickle().getName());
        setThreadLocalWebDriver(managedWebDriver);
        testNGCucumberRunner.runScenario(pickleWrapper.getPickle());
    }

    @DataProvider(name = "scenarios", parallel = true)
    public Iterator<Object[]> scenarios() {
        Object[][] scenarios = testNGCucumberRunner.provideScenarios();
        //Get Iterator of Object arrays consisting PickleWrapper, FeatureWrapper and ManagedWebDriver
        return new LazyInitWebDriverIterator(scenarios);
    }

    @AfterClass(alwaysRun = true)
    public void tearDownClass() {
        if(local != null){
            try {
                local.stop();
            } catch (Exception e) {
                throw new Error("Unable to stop BrowserStack Local.");
            }
        }
        if (testNGCucumberRunner == null) {
            return;
        }
        testNGCucumberRunner.finish();
    }
}

The StackDemoSteps.java file in the cucumber-java-browserstack/src/test/java/com/browserstack/stepdefs directory contains the methods to configure and create the connection with BrowserStack as follows:

StackDemoSteps.java
    public class StackDemoSteps {
    private WebDriver driver;
    private HomePage homePage;

    @Before
    public void setUp(Scenario scenario) throws Exception {
        driver = RunWebDriverCucumberTests.getManagedWebDriver().getWebDriver();
        homePage = new HomePage(driver);
    }

    @Given("^I am on the website '(.+)'$")
    public void I_am_on_the_website(String url) throws Throwable {
        driver.get(url);
        Thread.sleep(2000);
    }

    @When("^I select a product and click on 'Add to cart' button")
    public void I_select_a_product_and_add_to_cart() throws Throwable {
        homePage.selectFirstProductName();
        homePage.clickAddToCartButton();
        Thread.sleep(2000);
    }

    @Then("the product should be added to cart")
    public void product_should_be_added_to_cart() {
        homePage.waitForCartToOpen();
        assertEquals(homePage.getSelectedProductName(), homePage.getProductCartText());
    }

    @Then("the page should contain '(.+)'$")
    public void page_should_contain(String expectedTitle) {
        assertTrue(driver.getPageSource().contains(expectedTitle));
    }

    @After
    public void teardown(Scenario scenario) throws Exception {
        if (scenario.isFailed()) {
            Utility.setSessionStatus(driver, "failed", String.format("%s failed.", scenario.getName()));
        } else {
            Utility.setSessionStatus(driver, "passed", String.format("%s passed.", scenario.getName()));
        }
        Thread.sleep(2000);
        driver.quit();
      }
    }

Understand the dependencies

The required dependencies to run all the tests in the repository are added to the pom.xml file as follows:

pom.xml
  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.browserstack</groupId>
    <artifactId>cucumber-java-browserstack</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>cucumber-java-browserstack</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <cucumber.version>6.9.1</cucumber.version>
        <testng.version>7.4.0</testng.version>
        <selenium-java.version>3.141.59</selenium-java.version>
        <browserstack-local-java.version>1.0.6</browserstack-local-java.version>
        <json-simple.version>1.1.1</json-simple.version>
        <slf4j.version>1.7.30</slf4j.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium-java.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.browserstack</groupId>
            <artifactId>browserstack-local-java</artifactId>
            <version>${browserstack-local-java.version}</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>${json-simple.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>single</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>3.0.0-M5</version>
                        <configuration>
                            <suiteXmlFiles>
                                <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                            </suiteXmlFiles>
                            <argLine>
                                -Ddataproviderthreadcount=1
                            </argLine>
                            <systemPropertyVariables>
                                <caps>src/test/resources/conf/single.conf.json</caps>
                                <cucumber.publish.quiet>true</cucumber.publish.quiet>
                                <cucumber.features>src/test/resources/features/test</cucumber.features>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>parallel</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>3.0.0-M5</version>
                        <configuration>
                            <suiteXmlFiles>
                                <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                            </suiteXmlFiles>
                            <argLine>
                                -Ddataproviderthreadcount=4
                            </argLine>
                            <systemPropertyVariables>
                                <caps>src/test/resources/conf/parallel.conf.json</caps>
                                <cucumber.publish.quiet>true</cucumber.publish.quiet>
                                <cucumber.features>src/test/resources/features/test</cucumber.features>
                            </systemPropertyVariables>
                            <properties>
                                <property>
                                    <name>parallel</name>
                                    <value>methods</value>
                                </property>
                            </properties>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>local</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>3.0.0-M5</version>
                        <configuration>
                            <suiteXmlFiles>
                                <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                            </suiteXmlFiles>
                            <argLine>
                                -Ddataproviderthreadcount=1
                            </argLine>
                            <systemPropertyVariables>
                                <caps>src/test/resources/conf/local.conf.json</caps>
                                <cucumber.publish.quiet>true</cucumber.publish.quiet>
                                <cucumber.features>src/test/resources/features/localtest</cucumber.features>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
  </project>

Mark tests as passed or failed

BrowserStack does not know whether the assertions in your tests have passed or failed because only the test script knows whether the assertions have passed. Therefore, based on the assertions on your script, you have to clearly inform BrowserStack whether your tests have passed or not using information in this section.

BrowserStack provides a comprehensive REST API to access and update information about your tests. The following sample code snippet allows you to mark your tests as pass or fail based on the assertions in your Cucumber Java test cases.

  // Mark test as pass / fail

  public static void mark() throws URISyntaxException, UnsupportedEncodingException, IOException {
    URI uri = new URI("https://YOUR_USERNAME:YOUR_ACCESS_KEY@api.browserstack.com/automate/sessions/<session-id>.json");
    HttpPut putRequest = new HttpPut(uri);

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add((new BasicNameValuePair("status", "completed")));
    nameValuePairs.add((new BasicNameValuePair("reason", "")));
    putRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpClientBuilder.create().build().execute(putRequest);
  }

Debug your app

BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests. Learn more about how to debug tests on BrowserStack using the Automate Dashboard.

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 Cucumber 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. To enable Visual Logs, set the browserstack.debug capability to true.

  "capabilities" = {
    "browserstack.debug": true
  }

Sample Visual Logs from Automate Dashboard: BrowserStack Automate Visual Logs

Video recording

Every test that runs on the BrowserStack Selenium grid is recorded as it is executed on our remote machine. This feature is beneficial 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 browserstack.video capability to false.

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

Next steps

After 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?

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