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.
1. Start be installing Maven, a software project management and comprehension tool.2. 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
3. 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>
4. Create an empty file called src/test/resources/seleniumcucumber/todo.feature
with the following content:
Feature: ToDo
Scenario: Archiving ToDos
Given I go to my ToDo App
When I archive all todos
Then I should have no todos
5. Edit the file called src/test/java/seleniumcucumber/Stepdefs.java
with the following content:
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;
public class Stepdefs {
private String username, authkey;
private RemoteWebDriver driver;
@Before
public void 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")
public void I_go_to_my_todo_app() throws Throwable {
driver.get("http://crossbrowsertesting.github.io/todo-app.html");
}
@When("I archive all todos")
public void I_click_on_my_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")
public void I_should_have_no_todos() throws Throwable {
List elems = driver.findElements(By.className("done-true"));
assertEquals(0, elems.size());
}
@After
public void teardown() {
if (driver != null) {
driver.quit();
}
}
}
You’ll 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.
mvn test
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. If you have any questions or concerns, feel free to get in touch.