Cucumber with Selenium WebDriver
In this post, we will setup a Cucumber and Selenium WebDriver Project. We will automate the Google calculator feature using cucumber as a TDD framework and Selenium WebDriver for web UI automation. You can download the entire project(18kb only) here – Cucumber with Selenium Sample Project.
The structure of the maven project would be something like this-
Now we will refer to each file marked in the image one by one.
Content
POM File
A POM file describes all the dependencies required in the project. This project is created as a maven project so all you need to do is include the POM file and all the required dependencies/libraries will get downloaded from maven repositories.
The libraries used in this project are-
- Selenium, JUnit, Cucumber-Java, and Cucumber-JUnit.
- Pom.xml file content-
4.0.0
CucumberJavaProject
cucumberJava
0.0.1-SNAPSHOT
junit
junit
4.11
test
org.seleniumhq.selenium
selenium-java
2.39.0
info.cukes
cucumber-java
1.1.2
test
info.cukes
cucumber-junit
1.1.2
test
Feature File
This file will define the scenario to be tested. In this example we will write a scenario to test the Google calculator. Feature file content-
Feature: Check addition in Google calculatorcontent
In order to verify that Google calculator work correctly
As a user of Google
I should be able to get correct addition result
Scenario: Addition
Given I open Google
When I enter "2+2" in search textbox
Then I should get result as "4"
Step Definition File
This file implements the steps stated in feature file e.g. for the step “Given I open Google” the step definition file will have a function that will launch a browser and open Google as an implementation to this step.
package artOfTesting.test;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class googleCalcStepDefinition {
protected WebDriver driver;
@Before
public void setup() {
driver = new FirefoxDriver();
}
@Given("^I open google$")
public void I_open_google() {
//Set implicit wait of 10 seconds and launch google
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.google.co.in");
}
@When("^I enter "([^