Cucumber with Selenium WebDriver and Maven


How to run Cucumber test with Selenium WebDriver?

Cucumber follows Behaviour driven testing (BDD). It is used with Selenium for browser-based automated testing. In this article, we will run a test using Cucumber and Selenium Webdriver. Cucumber uses simple English sentences for tests which are easy to understand and write. It can be used by business representatives and developers simultaneously. A typical test is given below.

 

Pre-requisites to run Selenium with Cucumber-BDD:

  1. Eclipse IDE-: Read this article on how to install Eclipse
  2. Cucumber – Java libraries-: Cucumber libraries will be required to run these tests. Download it from here.
  3. Maven -: Read this article on how to download and configure Maven.

Writing the tests:
Create a new Maven project in Eclipse IDE.
Edit pom.xml to add the following dependencies and plugin.

[code] 4.0.0
com.cucumber.test
com.cucumber.test
0.0.1-SNAPSHOT



junit
junit
4.12
test


info.cukes
cucumber-java
1.2.5


info.cukes
cucumber-junit
1.2.5


info.cukes
cucumber-core
1.2.5


org.seleniumhq.selenium
selenium-java
3.4.0


org.apache.maven.plugins
maven-compiler-plugin
3.6.1

UTF-81.8
1.8
-Werror
[/code]

Now add required files to src/test/java folder. The folder structure is

eclipse-cucumber-maven-webdriver-folder-structure
eclipse-cucumber-maven-webdriver-folder-structure

Create a Java class and name it as TestRunner.java. Enter the following in TestRunner.java

[code]
package com.cucumber.test;

import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
public class TestRunner {
}
[/code]
Similarly, create stepDefination.java and with the following contents

[code]
package com.cucumber.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import static org.junit.Assert.*;

public class stepDefination {
WebDriver driver = null;
WebElement searchbox=null;
String PageTitle=null;
@Given(“^I navigate to google home page$”)
public void i_navigate_to_google_home_page() throws Throwable {
System.setProperty(“webdriver.gecko.driver”, “C:\\Selenium driver\\geckodriver.exe”);
driver = new FirefoxDriver();
driver.get(“https://www.google.com”);
}

@Given(“^I enter \”([^\”]*)\” in search box$”)
public void i_enter_in_search_box(String keyword) throws Throwable {
searchbox = driver.findElement(By.name(“q”));
searchbox.sendKeys(keyword);
}
@When(“^I press enter$”)
public void i_press_enter() throws Throwable {
searchbox.submit();
}
@Then(“^I get results page$”)
public void i_get_results_page() throws Throwable {
PageTitle = driver.getTitle();
System.out.println(PageTitle);
assertEquals(“Cheese -sGoogle Search”, PageTitle);
}
}

[/code]
Finally, create a feature file. Name it as sample.feature. Contents of feature file is

[code]
@smoketest
Feature: To test my cucumber test is running
I want to run a sample feature file.

Scenario: I test if Google search is working
Given I navigate to Google home page
And I enter “Cheese” in search box
When I press enter
Then I get results page
[/code]
Now run the maven project. Enter

[code]mvn clean test[/code]
to get results.