Cucumber Java

Applies to CrossBrowserTesting SaaS, last modified on January 19, 2022

Want a powerful and easy to use command line tool for running Selenium tests? Cucumber might be the option for you. Cucumber provides language-bindings for the powerful browser-driving tool Selenium. Its Gherkin language allows you to write your tests in a way that can be easily read by anyone on your team. Cucumber Java integrates easily with the CrossBrowserTesting platform, so you can perform tests on a wide variety of OS/Device/Browser combinations, all from one test.

Get set up

  • Start by installing Maven, a software project management and comprehension tool.

  • Create a simple project with Cucumber installed by running command:

    mvn archetype:generate \
    -DarchetypeGroupId=io.cucumber \
    -DarchetypeArtifactId=cucumber-archetype \
    -DarchetypeVersion= \
    -DgroupId=seleniumcucumber \
    -DartifactId=seleniumcucumber \
    -Dpackage=seleniumcucumber \
    -Dversion=1.0.0-SNAPSHOT \

  • After changing into the seleniumcucumber/ directory that was just created, add the following dependency to the pom.xml file:


Write tests

Create an empty file called src/test/resources/seleniumcucumber/todo.feature with the following content:

Feature: ToDo App
  Scenario: Archiving ToDos
    Given I go to my ToDo App
    When I archive all todos
    Then I should have no todos

Next we need to define the procedural code. Edit the file called src/test/java/seleniumcucumber/ with the following content:


package seleniumcucumber;

import cucumber.api.PendingException;
import org.junit.Assert.*;
import org.openqa.selenium.remote.*;
import org.openqa.selenium.*;
import java.util.List;

publicclass Stepdefs {
private String username, authkey;
private RemoteWebDriver driver;

publicvoid setUp() throws Throwable {

username = "YOUR_USERNAME"; //replace with your username
authkey = "YOUR_AUTHKEY"; //replace with your authkey

String browser = System.getProperty("browser");

DesiredCapabilities caps = new DesiredCapabilities();

caps.setCapability("name", "selenium cucumber");
caps.setCapability("browserName", "Chrome");
caps.setCapability("platform", "Windows 10");
caps.setCapability("screenResolution", "1366x768");
caps.setCapability("record_video", "true");

driver = new RemoteWebDriver(new URL("http://" + username + ":" + authkey +""), caps);
@Given("I go to my ToDo App")
publicvoid I_go_to_my_todo_app() throws Throwable {

@When("I archive all todos")
publicvoid I_archive_all_todos() throws Throwable {

@Then("I should have no todos")
publicvoid I_should_have_no_todos() throws Throwable {
List elems = driver.findElements(By.className("done-true"));
assertEquals(0, elems.size());

publicvoid teardown() {
if (driver != null) {


Note: You will need to use your Username and Authkey to run your tests on CrossBrowserTesting. To get yours, sign up for a free trial or purchase a plan.

As you can probably make out from our test, we visit a small ToDo App example, interact with our page, and use assertions to verify that the changes we have made are actually reflected in our app.

Now you are ready to run your test using the command:

mvn test

Congratulations! You have successfully integrated CrossBrowserTesting and Cucumber for Java. We kept it short and sweet for our purposes, but there is so much more you can do with Cucumber! Being built on top of Selenium means the sky is the limit as far as what you can do.

For examples and source code to this tutorial, check out our  Cucumber Java GitHub Repository

If you have any trouble, feel free to get in touch. We are always happy to help!

See Also

About Selenium Testing
Selenium and Java
Serenity Java

Highlight search results