Cucumber Java

Applies to CrossBrowserTesting SaaS, last modified on January 10, 2023

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=4.2.6.1 \
    -DgroupId=seleniumcucumber \
    -DartifactId=seleniumcucumber \
    -Dpackage=seleniumcucumber \
    -Dversion=1.0.0-SNAPSHOT \
    -DinteractiveMode=false

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

    <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
    </dependency>

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/Stepdefs.java with the following content:

src/test/java/seleniumcucumber/Stepdefs.java

package seleniumcucumber;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import cucumber.api.java.en.Then;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import org.junit.Assert.*;
import org.openqa.selenium.remote.*;
import org.openqa.selenium.*;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

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

@Before
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 +"@hub.crossbrowsertesting.com:80/wd/hub"), caps);
}
@Given("I go to my ToDo App")
publicvoid I_go_to_my_todo_app() throws Throwable {
driver.get("http://crossbrowsertesting.github.io/todo-app.html");
}

@When("I archive all todos")
publicvoid I_archive_all_todos() throws Throwable {
driver.findElement(By.name("todo-1")).click();
driver.findElement(By.name("todo-2")).click();
driver.findElement(By.name("todo-3")).click();
driver.findElement(By.name("todo-4")).click();
driver.findElement(By.name("todo-5")).click();
driver.findElement(By.linkText("archive")).click();
}


@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());
}

@After
publicvoid teardown() {
if (driver != null) {
driver.quit();
}
}

}

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
TestNG
Serenity Java

Highlight search results