Cucumber
Cucumber
CUCUMBER FRAMEWORK
1. BDD (Behaviour Driven Development) with the execution of TDD (Test
Driven Development).
2. Used to convert the exact requirements into plain English language.
3. It support insprint automation.
4. Non-Technical people can also understand the automation coverage.
5. Cucumber supports Ruby,Java,Grovy,Python,.NET and PHP.
6. We can use the Gherkin language or Gherkin Keyword (Plain
English).
7. Cucumber options.
8. Cucumber Reports.
CUCUMBER ANNOTATIONS
@Given
@When
@Then
@CucumberOptions
BDD FRAMEWORK
1. Cucumber
2. Jbehave
3. Nbehave
4. Specflow
GHERKIN Keyword / GHERKIN Language
Feature
Scenario
Given
When
Then
And
But
Scenario Outline
Examples
Background
CUCUMBER
Feature
1. Feature defines the logical test funtionality test [What you will test the
feature file].
2. Feature keyword is present at the starting of the feature file.
Scenario
1. Business rule[Provides the scenario name /purpose of the scenario].
Given
1. Some precondition step.
When
1. Some key actions.
Then
1. To observe the outcomes or validation.
Scenario Outline
1. Used to execute the same scenario with multiple times wih different set of
test datas.
Examples
1. Used to pass the parameters using the | (pipe symbol) | separate the each
variables.
But
1. Used to add negative type comments.
Background
1. Define steps which is are common to all the tests in the feature file.
And
1. Used to add conditions in feature file.
CUCUMBER
CUCUMBER OPTIONS
features
glue
dryRun
monochrome
plugin
pretty
tags
strict
features
feature option is used to locate the Feature folder in project folder
structure.
FEATURE
Glue
glue option is used to locate the stepDefinition class.
GLUE
CUCUMBER
dryRun
dryRun option can either set as true or false.
if it is set as true means check the every step mentioned in the feature file
has corresponding code written in StepDefinition class or not.so in case
any of the step is mismatch it update the snippets alone.
dryRun
monochrome
monochrome option can either set as true or false.
If it is set as true, it means that the console output for the Cucumber
test are much more readable.
if it is set as false, then the console output is not as readable.
Monochrome(false)
CUCUMBER
Monochrome(true)
plugin
plugin option is used to specify different formatting options for the
output reports(html,json,xml).
pretty
pretty option is used to Print the Gherkin source with additional colors
and stack traces for errors.
Pretty
tags
tags option is for which tags in the feature file should be executed
tags={“@<tag name>”}
strict
strict option is true will fail execution, if there are undefined or pending
steps
CUCUMBER
Cucumber Options
Option Purpose Default
Value
features set: The paths of the feature files {}
Glue set: The paths of the step definition files {}
Tags instruct: What tags in the feature files should be executed {}
dryRun true: Checks if all the steps have the Step Definition false
monochrome true: Display the console output in much readable format false
plugin set: What all the report formats to use {}
Strict true: Will fail execution, if there are undefined or pending steps false
Steps:
1. Add the cucumber plugin [cucumber eclipse plugin] in Eclipse
marketplace.
2. Create maven project
3. Add the cucumber dependency(cucumber-junit,cucumber-java,selenium-
java).
4. Create feature file in (src/test/resources).
5. Create TestRunner class(src/test/java).
6. Generate the snippets.
7. Copy the step definition files.
8. Copy the generated snippets and paste in the Stepdefinition class.
9. Run the code using testrunner class (right clickrun asjunit)
1.2 TestRunnerClass
1.3 StepdefinitionClass
CUCUMBER
2.2 TestRunnerClass
2.3 StepdefinitionClass
3.DESIGN PATTERNS
Page Object Model(POM).
Singleton.
CUCUMBER
3.1.2 TestRunnerClass
CUCUMBER
3.1.5 Architecture
3.2 CUCUMBER WITH SINGLETON
Reducing the object creation we go for singleton design pattern.
3.2.1 SINGLETON
CUCUMBER
3.2.4 TestRunnerClass
CUCUMBER
package com.libglobal;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class BaseClass {
public static WebDriver driver;
3.2.5 BaseClass
CUCUMBER
package org.pojo;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.libglobal.BaseClass;
public BookHotelPOJOClass() {
PageFactory.initElements(driver, this);
}
@FindBy(id = "first_name")
private List<WebElement> fname;
@FindBy(id = "last_name")
private List<WebElement> lname;
@FindBy(id = "address")
private List<WebElement> Address;
@FindBy(id = "cc_num")
private List<WebElement> ccnum;
@FindBy(id = "cc_type")
private List<WebElement> cardtype;
@FindBy(id = "cc_exp_month")
private List<WebElement> month;
@FindBy(id = "cc_exp_year")
private List<WebElement> year;
@FindBy(id = "cc_cvv")
private List<WebElement> cvv;
@FindBy(id = "book_now")
private List<WebElement> book;
3.2.6 BookHotelPOJOClass
package org.pojo;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.libglobal.BaseClass;
public ConfirmationPOJOClass() {
PageFactory.initElements(driver, this);
}
@FindBy(id = "search_hotel")
private List<WebElement> searchhotel;
}
3.2.7 ConfirmationPOJOClass
package org.pojo;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.libglobal.BaseClass;
public HotelConfirmPOJOClass() {
PageFactory.initElements(driver, this);
}
CUCUMBER
@FindBy(id = "radiobutton_0")
private List<WebElement> radio;
@FindBy(id = "continue")
private List<WebElement> Continue;
3.2.8 HotelConfirmPOJOClass
package org.pojo;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.libglobal.BaseClass;
public LoginPOJOClass() {
PageFactory.initElements(driver, this);
}
@FindBy(id = "username")
private List<WebElement> user;
@FindBy(id = "password")
private List<WebElement> pass;
@FindBy(id = "login")
private List<WebElement> log;
3.2.8 LoginPOJOClass
CUCUMBER
package org.pojo;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.libglobal.BaseClass;
public SearchHotelPOJOClass() {
PageFactory.initElements(driver, this);
}
@FindBy(id = "location")
private List<WebElement> location;
@FindBy(id = "hotels")
private List<WebElement> hotels;
@FindBy(id = "room_type")
private List<WebElement> roomtype;
@FindBy(id = "room_nos")
private List<WebElement> rooms;
@FindBy(id = "adult_room")
private List<WebElement> adult_room;
@FindBy(id = "child_room")
private List<WebElement> child_room;
@FindBy(id = "Submit")
private List<WebElement> submit;
PageObjectManager
package com.page;
import org.pojo.BookHotelPOJOClass;
import org.pojo.ConfirmationPOJOClass;
import org.pojo.HotelConfirmPOJOClass;
import org.pojo.LoginPOJOClass;
import org.pojo.SearchHotelPOJOClass;
LoginPOJOClass loginPage;
SearchHotelPOJOClass serchPage;
HotelConfirmPOJOClass hotelConfirmPage;
BookHotelPOJOClass bookingPage;
ConfirmationPOJOClass confirmPage;
3.2.10 PageObjectManager
Package com.stepdefinition;
import com.libglobal.BaseClass;
import com.page.PageObjectManager;
import io.cucumber.java.en.*;
public class StepDefinition extends BaseClass {
type(page.getLoginPage().getUser().get(0), userName);
type(page.getLoginPage().getPass().get(0), password);
btnClick(page.getLoginPage().getLog().get(0));
selectByVisibleText(page.getSerchPage().getLocation().get(0), location);
selectByVisibleText(page.getSerchPage().getHotels().get(0), hotel);
selectByVisibleText(page.getSerchPage().getRoomtype().get(0), roomType);
selectByVisibleText(page.getSerchPage().getRooms().get(0), numOfRooms);
CUCUMBER
selectByVisibleText(page.getSerchPage().getAdult_room().get(0), adultsPerRoom);
selectByVisibleText(page.getSerchPage().getChild_room().get(0), childperRoom);
}
btnClick(page.getHotelConfirmPage().getRadio().get(0));
}
@When("User enter
{string}and{string}and{string}and{string}and{string}and{string}and{string}and{string}")
public void user_enter_and_and_and_and_and_and_and(String firstNmae, String lastName, String
billingAddress,String creditCardNo, String creditCardType, String expiryMonth, String expiryYear,
String cvvNumber) {
type(page.getBookingPage().getFname().get(0), firstNmae);
type(page.getBookingPage().getLname().get(0), lastName);
type(page.getBookingPage().getAddress().get(0), billingAddress);
type(page.getBookingPage().getCcnum().get(0), creditCardNo);
selectByVisibleText(page.getBookingPage().getCardtype().get(0), creditCardType);
selectByVisibleText(page.getBookingPage().getMonth().get(0), expiryMonth);
selectByVisibleText(page.getBookingPage().getYear().get(0), expiryYear);
type(page.getBookingPage().getCvv().get(0), cvvNumber);
}
3.2.11 StepDefinitionClass
CUCUMBER
TestRunnerClass
CUCUMBER
Feature File
PojoClass
package com.libglobal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
name = rs.getString("first_name");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return name;
}
BaseClass
CUCUMBER
package com.stepdefinition;
import com.libglobal.Base;
import com.page.FacebookLoginPOJO;
import io.cucumber.java.en.*;
@When("User enters the userName and password and click login button")
public void user_enters_the_userName_and_password_and_click_login_button() throws Throwable {
FacebookLoginPOJO f = new FacebookLoginPOJO();
// get data from sql
type(f.getUserName(), getDataFromSql("select first_name from employees where
first_name='Sundar'"));
type(f.getPassword(), getDataFromSql("select first_name from employees where
first_name='Ellen'"));
btnClick(f.getBtnLog());// clicks the login button
quitBrowser();// quits the browser
}
StepDefinitionClass
Output
CUCUMBER
TO PASSING THE PARAMETER USING EXCEL IN CUCUMBER
1. Create Maven project add poi-ooxml,commons-io dependencies.
2. Create Folder and copy paste the Excel.
Feature file
TestRunnerClass
CUCUMBER
package com.libglobal;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
if (type == 1) {
value = c.getStringCellValue();
} else if (type == 0) {
if (DateUtil.isCellDateFormatted(c)) {
Date dateCellValue = c.getDateCellValue();
SimpleDateFormat sim = new SimpleDateFormat("dd-mm-yyyy");
value = sim.format(dateCellValue);
} else {
}
return value;
}
}
BaseClass
StepDefinitionClass
CUCUMBER
Excel File
Output
1D –List Diagram
1D-Without Header
CUCUMBER
2.2D-WITHOUT HEADER(asLists)
2D-List Diagram
2D-Without Header
CUCUMBER
3.1D-WITH HEADER(asMap)
1D-Map Diagram
1D-With Header
CUCUMBER
3.2D-WITH HEADER(asMaps)
2D-Map Diagram
2D-With Header
CUCUMBER
Feature File
TestRunnerClass
package com.libglobal;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
BaseClass
package com.page;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.libglobal.Base;
}
Login POJO
CUCUMBER
package com.stepdefinition;
import java.util.List;
import java.util.Map;
import com.libglobal.Base;
import com.page.FacebookLoginPOJO;
import io.cucumber.java.en.*;
@When("User enters the userName and password using data table ONE DIMENTIONAL LIST and click
login button")
public void
user_enters_the_userName_and_password_using_data_table_ONE_DIMENTIONAL_LIST_and_click_login_button(
io.cucumber.datatable.DataTable dataFromList) {
f = new FacebookLoginPOJO();
List<String> list = dataFromList.asList();
type(f.getUserName(), list.get(0));// 1d without header
type(f.getPassword(), list.get(1));// 1d without header
btnClick(f.getBtnLog());
}
@When("User enters the userName and password using data table TWO DIMENTIONAL LIST and click
login button")
public void
user_enters_the_userName_and_password_using_data_table_TWO_DIMENTIONAL_LIST_and_click_login_button(
io.cucumber.datatable.DataTable dataFromLists) {
f = new FacebookLoginPOJO();
List<List<String>> lists = dataFromLists.asLists();
type(f.getUserName(), lists.get(0).get(0));// 2d without header
type(f.getPassword(), lists.get(0).get(1));// 2d without header
btnClick(f.getBtnLog());
}
CUCUMBER
@When("User enters the username and password ONE DIMENTIONAL MAP and click login button")
public void user_enters_the_username_and_password_ONE_DIMENTIONAL_MAP_and_click_login_button(
io.cucumber.datatable.DataTable dataFromMap) {
f = new FacebookLoginPOJO();
Map<String, String> map = dataFromMap.asMap(String.class, String.class);
type(f.getUserName(), map.get("userName"));// 1d with header
type(f.getPassword(), map.get("password"));// 1d with header
btnClick(f.getBtnLog());
@When("User enters the userName using data table TWO DIMENTIONAL MAP and password and click
login button")
public void
user_enters_the_userName_using_data_table_TWO_DIMENTIONAL_MAP_and_password_and_click_login_button(
io.cucumber.datatable.DataTable dataFromMaps) {
f = new FacebookLoginPOJO();
List<Map<String, String>> maps = dataFromMaps.asMaps();
Stepdefinition Class
HOOKS IN CUCUMBER
Cucumber supports hooks which are blocks of code that run before or
after each scenario.Cucumber Hooks allows us to better manage the
code workflow and helps as to reduce the code redundancy.
In hooks we will be having two anatations @Before and @After.
In Hooks we can priortize the execution order.
In Hooks we can use tags (taged hooks).
CUCUMBER
@Before
Which is going to execute before each scenario like
1. Launch browser.
2. Load url.
3. Delete cookies.
4. Maximize window.
5. Waits.
@After
Which is going to execute after each scenario like
1. Close browser.
2. Quit browser.
3. Screenshot.
4. Closing the database connection.
TestRunner Class
CUCUMBER
HooksClass
package org.libglobal;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
new Select(element).selectByVisibleText(text);
if (type == 1) {
value = c.getStringCellValue();
} else if (type == 0) {
if (DateUtil.isCellDateFormatted(c)) {
Date dateCellValue = c.getDateCellValue();
SimpleDateFormat sim = new SimpleDateFormat("dd-mm-yyyy");
value = sim.format(dateCellValue);
} else {
}
return value;
}
BaseClass
package com.pojo;
import org.libglobal.BaseClass;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public LoginPOJOClass() {
PageFactory.initElements(driver, this);
}
@FindBy(id = "email")
private WebElement username;
@FindBy(id = "pass")
private WebElement password;
@FindBy(id = "loginbutton")
private WebElement login;
@FindBy(id = "u_0_m")
private WebElement fname;
@FindBy(id = "u_0_o")
private WebElement lname;
@FindBy(id = "u_0_13")
private WebElement signup;
CUCUMBER
public WebElement getUsername() {
return username;
}
LoginPojoClass
package com.stepdefinition;
import java.io.IOException;
import java.util.List;
import org.libglobal.BaseClass;
import com.pojo.LoginPOJOClass;
import io.cucumber.java.en.*;
LoginPOJOClass l;
type(l.getUsername(), "[email protected]");
type(l.getPassword(), "345678");
btnClick(l.getLogin());
}
type(l.getFname(), "nitish");
type(l.getLname(), "kumar");
btnClick(l.getSignup());
}
type(l.getUsername(), li.get(0));
type(l.getPassword(), li.get(1));
}
type(l.getUsername(), list.get(0).get(0));
type(l.getPassword(), list.get(0).get(1));
StepDefinitionClass
CUCUMBER
Cucumber Architecture
CUCUMBER TAGS
Using tags we can run the bulk of testcases.
Using tags we can skip the bulk of testcase.
Syntax
tags={“@tagname”}
ignore
tags={“~@tagname”}
groups using OR operator
using OR operator we can run more than one groups.
Comma ( , ) operator is used for OR.
tags={“@tagname1 , @tagname2”}
groups using AND operator
Tags={“@tagname1”,”@tagname2”}
CUCUMBER
TestRunner Class
CUCUMBER
Hooks Class
package org.libglobal;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
new Select(element).selectByVisibleText(text);
value = c.getStringCellValue();
} else if (type == 0) {
if (DateUtil.isCellDateFormatted(c)) {
Date dateCellValue = c.getDateCellValue();
SimpleDateFormat sim = new SimpleDateFormat("dd-mm-yyyy");
value = sim.format(dateCellValue);
} else {
}
return value;
}
BaseClass
package com.pojo;
import org.libglobal.BaseClass;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public LoginPOJOClass() {
PageFactory.initElements(driver, this);
}
@FindBy(id = "email")
private WebElement username;
@FindBy(id = "pass")
private WebElement password;
@FindBy(id = "loginbutton")
private WebElement login;
@FindBy(id = "u_0_m")
private WebElement fname;
@FindBy(id = "u_0_o")
private WebElement lname;
@FindBy(id = "u_0_13")
private WebElement signup;
Pojo Class
package com.stepdefinition;
import java.io.IOException;
import java.util.List;
import org.libglobal.BaseClass;
import com.pojo.LoginPOJOClass;
import io.cucumber.java.en.*;
LoginPOJOClass l;
type(l.getUsername(), "[email protected]");
type(l.getPassword(), "345678");
btnClick(l.getLogin());
CUCUMBER
}
type(l.getFname(), "nitish");
type(l.getLname(), "kumar");
btnClick(l.getSignup());
}
type(l.getUsername(), li.get(0));
type(l.getPassword(), li.get(1));
}
type(l.getUsername(), list.get(0).get(0));
type(l.getPassword(), list.get(0).get(1));
Stepdfinition Class
CUCUMBER
Output
Feature File Grouping
tags={“@EndToEnd”}
FeatureFile One
TestRunnerClass
Hooks Class
package org.libglobal;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
CUCUMBER
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
new Select(element).selectByVisibleText(text);
if (type == 1) {
value = c.getStringCellValue();
} else if (type == 0) {
if (DateUtil.isCellDateFormatted(c)) {
Date dateCellValue = c.getDateCellValue();
SimpleDateFormat sim = new SimpleDateFormat("dd-mm-yyyy");
value = sim.format(dateCellValue);
} else {
}
return value;
}
BaseClass
package com.pojo;
import org.libglobal.BaseClass;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public LoginPOJOClass() {
PageFactory.initElements(driver, this);
}
@FindBy(id = "email")
private WebElement username;
CUCUMBER
@FindBy(id = "pass")
private WebElement password;
@FindBy(id = "loginbutton")
private WebElement login;
@FindBy(id = "u_0_m")
private WebElement fname;
@FindBy(id = "u_0_o")
private WebElement lname;
@FindBy(id = "u_0_13")
private WebElement signup;
Pojo Class
package com.stepdefinition;
import java.io.IOException;
import java.util.List;
import org.libglobal.BaseClass;
import com.pojo.LoginPOJOClass;
import io.cucumber.java.en.*;
LoginPOJOClass l;
type(l.getUsername(), "[email protected]");
type(l.getPassword(), "345678");
btnClick(l.getLogin());
}
type(l.getFname(), "nitish");
type(l.getLname(), "kumar");
btnClick(l.getSignup());
}
type(l.getUsername(), li.get(0));
type(l.getPassword(), li.get(1));
}
type(l.getUsername(), list.get(0).get(0));
type(l.getPassword(), list.get(0).get(1));
Stepdfinition Class
Output
CUCUMBER