0% found this document useful (0 votes)
45 views58 pages

Cucumber

1. Cucumber is a framework used for Behavior Driven Development (BDD) and supports Ruby, Java, Groovy, Python, .NET and PHP. It allows converting requirements into executable tests written in a business-readable language. 2. Key aspects of Cucumber include feature files using the Gherkin language with keywords like Feature, Scenario, Given, When, Then. Step definitions are used to define the code implementation for each step. 3. Common Cucumber annotations include @Given, @When, @Then. Cucumber options allow configuration for tags, plugins, and output formats. Page object model is a common design pattern used to organize Cucumber code.

Uploaded by

shivashan2207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views58 pages

Cucumber

1. Cucumber is a framework used for Behavior Driven Development (BDD) and supports Ruby, Java, Groovy, Python, .NET and PHP. It allows converting requirements into executable tests written in a business-readable language. 2. Key aspects of Cucumber include feature files using the Gherkin language with keywords like Feature, Scenario, Given, When, Then. Step definitions are used to define the code implementation for each step. 3. Common Cucumber annotations include @Given, @When, @Then. Cucumber options allow configuration for tags, plugins, and output formats. Page object model is a common design pattern used to organize Cucumber code.

Uploaded by

shivashan2207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

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 clickrun asjunit)

1.CUCUMBER PROGRAM TO LAUNCH FACEBOOK

1.1 Feature File


CUCUMBER

1.2 TestRunnerClass

1.3 StepdefinitionClass
CUCUMBER

1.4 Cucumber Architecture

2.LAUNCH FACEBOOK AND PASSING THE PARAMETER USING


EXAMPLE
 In cucumber using Scenario Outline and Example keyword we can pass
multiple parameters for same scenario.

2.1 Feature file


CUCUMBER

2.2 TestRunnerClass

2.3 StepdefinitionClass

3.DESIGN PATTERNS
 Page Object Model(POM).
 Singleton.
CUCUMBER

3.1 CUCUMBER WITH BASE CLASS AND POM


 It is widely used design pattern in Cucumber for enhancing test
maintenance and reducing code duplication.

3.1.1 Feature file

3.1.2 TestRunnerClass
CUCUMBER

3.1.3 POJO CLASS


CUCUMBER
3.1.4 BaseClass

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.3 Feature File

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;

public static void launchBrowser() {


System.setProperty("webdriver.chrome.driver",
"C:\\Users\\k_sur\\Desktop\\Nitish\\nitish\\Cucumber\\driver\\chromedriver.exe");
driver = new ChromeDriver();

public static void maximizeWindow() {


driver.manage().window().maximize();
}

public static void loadUrl(String url) {


driver.get(url);

public static void type(WebElement e, String value) {


e.sendKeys(value);

public static void btnClick(WebElement e) {


e.click();

public static void getTittle() {


String title = driver.getTitle();
System.out.println(title);

public static void getUrl() {


String currentUrl = driver.getCurrentUrl();
System.out.println(currentUrl);
}

public static void quitBrowser() {


driver.quit();

public static void selectByVisibleText(WebElement element, String text) {

Select s = new Select(element);


s.selectByVisibleText(text);
}}

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 class BookHotelPOJOClass extends 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;

public List<WebElement> getFname() {


return fname;
}

public List<WebElement> getLname() {


return lname;
}

public List<WebElement> getAddress() {


return Address;
}

public List<WebElement> getCcnum() {


return ccnum;
}

public List<WebElement> getCardtype() {


return cardtype;
}

public List<WebElement> getMonth() {


return month;
}
CUCUMBER
public List<WebElement> getYear() {
return year;
}

public List<WebElement> getCvv() {


return cvv;
}

public List<WebElement> getBook() {


return 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 class ConfirmationPOJOClass extends BaseClass {

public ConfirmationPOJOClass() {
PageFactory.initElements(driver, this);
}

@FindBy(id = "search_hotel")
private List<WebElement> searchhotel;

public List<WebElement> getSearchhotel() {


return 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 class HotelConfirmPOJOClass extends BaseClass {

public HotelConfirmPOJOClass() {
PageFactory.initElements(driver, this);
}
CUCUMBER
@FindBy(id = "radiobutton_0")
private List<WebElement> radio;
@FindBy(id = "continue")
private List<WebElement> Continue;

public List<WebElement> getRadio() {


return radio;
}

public List<WebElement> getContinue() {


return 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 class LoginPOJOClass extends 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;

public List<WebElement> getUser() {


return user;
}

public List<WebElement> getPass() {


return pass;
}

public List<WebElement> getLog() {


return 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 class SearchHotelPOJOClass extends 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;

public List<WebElement> getLocation() {


return location;
}

public List<WebElement> getHotels() {


return hotels;
}

public List<WebElement> getRoomtype() {


return roomtype;
}

public List<WebElement> getRooms() {


return rooms;
}

public List<WebElement> getAdult_room() {


return adult_room;
}

public List<WebElement> getChild_room() {


return child_room;
}

public List<WebElement> getSubmit() {


return submit;
}
}
3.2.9 SearchHotelPOJOClass
CUCUMBER

PageObjectManager

package com.page;

import org.pojo.BookHotelPOJOClass;
import org.pojo.ConfirmationPOJOClass;
import org.pojo.HotelConfirmPOJOClass;
import org.pojo.LoginPOJOClass;
import org.pojo.SearchHotelPOJOClass;

public class PageObjectManager {

LoginPOJOClass loginPage;
SearchHotelPOJOClass serchPage;
HotelConfirmPOJOClass hotelConfirmPage;
BookHotelPOJOClass bookingPage;
ConfirmationPOJOClass confirmPage;

public LoginPOJOClass getLoginPage() {


return (loginPage == null) ? loginPage = new LoginPOJOClass() : loginPage;
}

public SearchHotelPOJOClass getSerchPage() {


return (serchPage == null) ? serchPage = new SearchHotelPOJOClass() : serchPage;
}

public HotelConfirmPOJOClass getHotelConfirmPage() {


return (hotelConfirmPage == null) ? hotelConfirmPage = new HotelConfirmPOJOClass() :
hotelConfirmPage;
}

public BookHotelPOJOClass getBookingPage() {


return (bookingPage == null) ? bookingPage = new BookHotelPOJOClass() : bookingPage;
}
CUCUMBER
public ConfirmationPOJOClass getConfirmPage() {
return (confirmPage == null) ? confirmPage = new 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 {

PageObjectManager page = new PageObjectManager();

@Given("User is on adactin page")


public void user_is_on_adactin_page() {
launchBrowser();
loadUrl("https://adactin.com/HotelApp/");
maximizeWindow();
}

@When("User enters {string}and{string}")


public void user_enters_and(String userName, String password) {

type(page.getLoginPage().getUser().get(0), userName);
type(page.getLoginPage().getPass().get(0), password);

@When("User clicks login button")


public void user_clicks_login_button() {

btnClick(page.getLoginPage().getLog().get(0));

@Then("User verify success message")


public void user_verify_success_message() {
System.out.println("Successfuly login");
}

@When("User select {string}and{string}and{string}and{string}and{string}and{string}")


public void user_select_and_and_and_and_and(String location, String hotel, String roomType,
String numOfRooms,String adultsPerRoom, String childperRoom) {

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

@When("User click search button")


public void user_click_search_button() {
btnClick(page.getSerchPage().getSubmit().get(0));
}

@When("User select the Hotel")


public void user_select_the_Hotel() {

btnClick(page.getHotelConfirmPage().getRadio().get(0));
}

@When("User click the continue button")


public void user_click_the_continue_button() {
btnClick(page.getHotelConfirmPage().getContinue().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);
}

@When("User click BookNow button")


public void user_click_BookNow_button() {
btnClick(page.getBookingPage().getBook().get(0));
}

@When("User click search hotel")


public void user_click_search_hotel() throws InterruptedException {
Thread.sleep(5000);
btnClick(page.getConfirmPage().getSearchhotel().get(0));
quitBrowser();
}

3.2.11 StepDefinitionClass
CUCUMBER

3.2.12 Cucumber Architecture


TO PASS THE PARAMETER USING SQL IN CUCUMBER
1. Create maven project and add ojdbc jar.
2. Pass the JDBC connection in base class.

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;

public class Base {


public static WebDriver driver;
CUCUMBER

public static void launchBrowser() {


System.setProperty("webdriver.chrome.driver",
"C:\\Users\\k_sur\\eclipseworkspace\\CucumberFirst\\driver\\chromedriver.exe");
driver = new ChromeDriver();
}

public static void maximizeWindow() {


driver.manage().window().maximize();
}
public static void loadUrl(String url) {
driver.get(url);
}
public static void type(WebElement element, String name) {
element.sendKeys(name);
}

public static void btnClick(WebElement e) {


e.click();

public static void quitBrowser() {


driver.quit();

public static String getDataFromSql(String query) throws Throwable {


Connection con = null;
String name = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "admin");

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.*;

public class StepDefinition extends Base {

@Given("User is on login page")


public void user_is_on_login_page() {
launchBrowser();// launch the browser
maximizeWindow();// maximize the window
loadUrl("https://www.facebook.com/");
}

@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
}

@Then("User get success message")


public void user_get_success_message() {
System.out.println("done...");
}

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;

public class Base {


public static WebDriver driver;

public static void launchBrowser() {


System.setProperty("webdriver.chrome.driver",
"C:\\Users\\k_sur\\eclipse-
workspace\\CucumberFirst\\driver\\chromedriver.exe");
driver = new ChromeDriver();

public static void maximizeWindow() {


driver.manage().window().maximize();
}

public static void loadUrl(String url) {


driver.get(url);

public static void type(WebElement element, String name) {


element.sendKeys(name);

public static void btnClick(WebElement e) {


e.click();

public static void quitBrowser() {


driver.quit();

@SuppressWarnings({ "deprecation", "resource" })


public static String getDataFromExcel(int row, int cell) throws IOException {
String value = null;
File loc = new
File("C:\\Users\\k_sur\\eclipseworkspace\\CucumberFirst\\Excel\\Book1.xlsx");
CUCUMBER
FileInputStream stream = new FileInputStream(loc);
Workbook w = new XSSFWorkbook(stream);
Sheet s = w.getSheet("Greens");
Row r = s.getRow(row);
Cell c = r.getCell(cell);

int type = c.getCellType();

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 {

double numericCellValue = c.getNumericCellValue();


long l = (long) numericCellValue;
value = String.valueOf(l);
}

}
return value;
}

}
BaseClass

StepDefinitionClass
CUCUMBER

Excel File

Output

DATA TABLE IN CUCUMBER


1. asList(1D-Without Header).
2. asLists(2D-Without Header).
3. asMap(1D-With Header).
4. asMaps(2D-With Header).
CUCUMBER
1.1D-WITHOUT HEADER(asList)

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;

public class Base {


public static WebDriver driver;

public static void launchBrowser() {


System.setProperty("webdriver.chrome.driver",
"C:\\Users\\k_sur\\eclipse-
workspace\\CucumberFirst\\driver\\chromedriver.exe");
driver = new ChromeDriver();

public static void maximizeWindow() {


driver.manage().window().maximize();
}
CUCUMBER
public static void loadUrl(String url) {
driver.get(url);

public static void type(WebElement element, String name) {


element.sendKeys(name);

public static void btnClick(WebElement e) {


e.click();

public static void quitBrowser() {


driver.quit();

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;

public class FacebookLoginPOJO extends Base {


public FacebookLoginPOJO() {
PageFactory.initElements(driver, this);
}
//login page locators
@FindBy(id = "email")
private WebElement userName;
@FindBy(id = "pass")
private WebElement password;
@FindBy(id = "loginbutton")
private WebElement btnLog;

public WebElement getUserName() {


return userName;
}

public WebElement getPassword() {


return password;
}

public WebElement getBtnLog() {


return btnLog;
}

}
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.*;

public class StepDefinition extends Base {


FacebookLoginPOJO f;

@Given("User is on login page")


public void user_is_on_login_page() {
launchBrowser();
loadUrl("https://www.facebook.com/");
maximizeWindow();

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

@Then("User get success message")


public void user_get_success_message() {
quitBrowser();
System.out.println("done...");
}

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

@Then("User get verification message")


public void user_get_verification_message() {
quitBrowser();
System.out.println("done...");

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

@Then("User get message")


public void user_get_message() {
quitBrowser();
System.out.println("done...");

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

type(f.getUserName(), maps.get(1).get("userName"));// 2d with header


type(f.getPassword(), maps.get(0).get("password"));// 2d with header
btnClick(f.getBtnLog());

@Then("User get some message")


public void user_get_some_message() {
quitBrowser();
System.out.println("done...");
}

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.

Feature File One

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;

public class BaseClass {


public static WebDriver driver;

public static void launchBrowser() {


System.setProperty("webdriver.chrome.driver",
"C:\\Users\\k_sur\\Desktop\\Nitish\\cucumber
jenkins\\jenkins\\driver\\chromedriver.exe");
driver = new ChromeDriver();

public static void maximizeWindow() {


driver.manage().window().maximize();
CUCUMBER
}

public static void loadUrl(String url) {


driver.get(url);

public static void type(WebElement e, String value) {


e.sendKeys(value);

public static void btnClick(WebElement e) {


e.click();

public static String getTittle() {


String title = driver.getTitle();
return title;

public static String getUrl() {


String Url = driver.getCurrentUrl();
return Url;

public static void quitBrowser() {


driver.quit();

public static void selectByVisibleText(WebElement element, String text) {

new Select(element).selectByVisibleText(text);

public static String getTextAttribute(WebElement element) {


String attribute = element.getAttribute("value");
return attribute;

@SuppressWarnings({ "deprecation", "resource" })


public static String getDataFromExcel(int row, int cell) throws IOException {
String value = null;
File loc = new File("C:\\Users\\k_sur\\eclipse-
workspace\\Cucumber\\Excel\\integration.xlsx");
FileInputStream stream = new FileInputStream(loc);
Workbook w = new XSSFWorkbook(stream);
Sheet s = w.getSheet("Sheet1");
Row r = s.getRow(row);
Cell c = r.getCell(cell);
CUCUMBER
int type = c.getCellType();

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 {

double numericCellValue = c.getNumericCellValue();


long l = (long) numericCellValue;
value = String.valueOf(l);
}

}
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 class LoginPOJOClass extends BaseClass {

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;
}

public WebElement getPassword() {


return password;
}

public WebElement getLogin() {


return login;
}

public WebElement getFname() {


return fname;
}

public WebElement getLname() {


return lname;
}

public WebElement getSignup() {


return signup;
}

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.*;

public class StepDefinition extends BaseClass {

LoginPOJOClass l;

@Given("User on the login page")


public void user_on_the_login_page() {

@When("User enters username and password")


public void user_enters_username_and_password() {
l = new LoginPOJOClass();

type(l.getUsername(), "[email protected]");
type(l.getPassword(), "345678");

@Then("User clicks login button")

public void user_clicks_login_button() {


CUCUMBER
l = new LoginPOJOClass();

btnClick(l.getLogin());
}

@When("User enters fname and lname")


public void user_enters_fname_and_lname() {
l = new LoginPOJOClass();

type(l.getFname(), "nitish");
type(l.getLname(), "kumar");

@Then("User click signup button")


public void user_click_signup_button() {
l = new LoginPOJOClass();

btnClick(l.getSignup());
}

@When("User enters username and password for {int}d")


public void user_enters_username_and_password_for_d(Integer int1,
io.cucumber.datatable.DataTable d) {
List<String> li = d.asList();
l = new LoginPOJOClass();

type(l.getUsername(), li.get(0));
type(l.getPassword(), li.get(1));
}

@When("User enters username and password for {int}d without header")


public void user_enters_username_and_password_for_d_without_header(Integer int1,
io.cucumber.datatable.DataTable d) {
List<List<String>> list = d.asLists();
l = new LoginPOJOClass();

type(l.getUsername(), list.get(0).get(0));
type(l.getPassword(), list.get(0).get(1));

@When("User enters the username and password")


public void user_enters_the_username_and_password() throws IOException {
l = new LoginPOJOClass();
type(l.getUsername(), getDataFromExcel(1, 1));
type(l.getPassword(), getDataFromExcel(1, 1));

@Then("User clicks the login button")


public void user_clicks_the_login_button() {
btnClick(l.getLogin());
}

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

Feature File one

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;

public class BaseClass {


public static WebDriver driver;

public static void launchBrowser() {


System.setProperty("webdriver.chrome.driver",
"C:\\Users\\k_sur\\Desktop\\Nitish\\cucumber
jenkins\\jenkins\\driver\\chromedriver.exe");
driver = new ChromeDriver();

public static void maximizeWindow() {


driver.manage().window().maximize();
}
CUCUMBER
public static void loadUrl(String url) {
driver.get(url);

public static void type(WebElement e, String value) {


e.sendKeys(value);

public static void btnClick(WebElement e) {


e.click();

public static String getTittle() {


String title = driver.getTitle();
return title;

public static String getUrl() {


String Url = driver.getCurrentUrl();
return Url;

public static void quitBrowser() {


driver.quit();

public static void selectByVisibleText(WebElement element, String text) {

new Select(element).selectByVisibleText(text);

public static String getTextAttribute(WebElement element) {


String attribute = element.getAttribute("value");
return attribute;

@SuppressWarnings({ "deprecation", "resource" })


public static String getDataFromExcel(int row, int cell) throws IOException {
String value = null;
File loc = new File("C:\\Users\\k_sur\\eclipse-
workspace\\Cucumber\\Excel\\integration.xlsx");
FileInputStream stream = new FileInputStream(loc);
Workbook w = new XSSFWorkbook(stream);
Sheet s = w.getSheet("Sheet1");
Row r = s.getRow(row);
Cell c = r.getCell(cell);

int type = c.getCellType();


CUCUMBER
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 {

double numericCellValue = c.getNumericCellValue();


long l = (long) numericCellValue;
value = String.valueOf(l);
}

}
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 class LoginPOJOClass extends BaseClass {

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;

public WebElement getUsername() {


return username;
}
CUCUMBER

public WebElement getPassword() {


return password;
}

public WebElement getLogin() {


return login;
}

public WebElement getFname() {


return fname;
}

public WebElement getLname() {


return lname;
}

public WebElement getSignup() {


return 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.*;

public class StepDefinition extends BaseClass {

LoginPOJOClass l;

@Given("User on the login page")


public void user_on_the_login_page() {

@When("User enters username and password")


public void user_enters_username_and_password() {
l = new LoginPOJOClass();

type(l.getUsername(), "[email protected]");
type(l.getPassword(), "345678");

@Then("User clicks login button")

public void user_clicks_login_button() {


l = new LoginPOJOClass();

btnClick(l.getLogin());
CUCUMBER
}

@When("User enters fname and lname")


public void user_enters_fname_and_lname() {
l = new LoginPOJOClass();

type(l.getFname(), "nitish");
type(l.getLname(), "kumar");

@Then("User click signup button")


public void user_click_signup_button() {
l = new LoginPOJOClass();

btnClick(l.getSignup());
}

@When("User enters username and password for {int}d")


public void user_enters_username_and_password_for_d(Integer int1,
io.cucumber.datatable.DataTable d) {
List<String> li = d.asList();
l = new LoginPOJOClass();

type(l.getUsername(), li.get(0));
type(l.getPassword(), li.get(1));
}

@When("User enters username and password for {int}d without header")


public void user_enters_username_and_password_for_d_without_header(Integer int1,
io.cucumber.datatable.DataTable d) {
List<List<String>> list = d.asLists();
l = new LoginPOJOClass();

type(l.getUsername(), list.get(0).get(0));
type(l.getPassword(), list.get(0).get(1));

@When("User enters the username and password")


public void user_enters_the_username_and_password() throws IOException {
l = new LoginPOJOClass();
type(l.getUsername(), getDataFromExcel(1, 1));
type(l.getPassword(), getDataFromExcel(1, 1));

@Then("User clicks the login button")


public void user_clicks_the_login_button() {
btnClick(l.getLogin());
}

Stepdfinition Class
CUCUMBER

Output
Feature File Grouping
 tags={“@EndToEnd”}

FeatureFile One

Feature File Two


CUCUMBER

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;

public class BaseClass {


public static WebDriver driver;

public static void launchBrowser() {


System.setProperty("webdriver.chrome.driver",
"C:\\Users\\k_sur\\Desktop\\Nitish\\cucumber
jenkins\\jenkins\\driver\\chromedriver.exe");
driver = new ChromeDriver();

public static void maximizeWindow() {


driver.manage().window().maximize();
}

public static void loadUrl(String url) {


driver.get(url);

public static void type(WebElement e, String value) {


e.sendKeys(value);

public static void btnClick(WebElement e) {


e.click();

public static String getTittle() {


String title = driver.getTitle();
return title;

public static String getUrl() {


String Url = driver.getCurrentUrl();
return Url;

public static void quitBrowser() {


driver.quit();

public static void selectByVisibleText(WebElement element, String text) {

new Select(element).selectByVisibleText(text);

public static String getTextAttribute(WebElement element) {


String attribute = element.getAttribute("value");
CUCUMBER
return attribute;

@SuppressWarnings({ "deprecation", "resource" })


public static String getDataFromExcel(int row, int cell) throws IOException {
String value = null;
File loc = new File("C:\\Users\\k_sur\\eclipse-
workspace\\Cucumber\\Excel\\integration.xlsx");
FileInputStream stream = new FileInputStream(loc);
Workbook w = new XSSFWorkbook(stream);
Sheet s = w.getSheet("Sheet1");
Row r = s.getRow(row);
Cell c = r.getCell(cell);

int type = c.getCellType();

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 {

double numericCellValue = c.getNumericCellValue();


long l = (long) numericCellValue;
value = String.valueOf(l);
}

}
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 class LoginPOJOClass extends BaseClass {

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;

public WebElement getUsername() {


return username;
}

public WebElement getPassword() {


return password;
}

public WebElement getLogin() {


return login;
}

public WebElement getFname() {


return fname;
}

public WebElement getLname() {


return lname;
}

public WebElement getSignup() {


return 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.*;

public class StepDefinition extends BaseClass {

LoginPOJOClass l;

@Given("User on the login page")


CUCUMBER
public void user_on_the_login_page() {

@When("User enters username and password")


public void user_enters_username_and_password() {
l = new LoginPOJOClass();

type(l.getUsername(), "[email protected]");
type(l.getPassword(), "345678");

@Then("User clicks login button")

public void user_clicks_login_button() {


l = new LoginPOJOClass();

btnClick(l.getLogin());
}

@When("User enters fname and lname")


public void user_enters_fname_and_lname() {
l = new LoginPOJOClass();

type(l.getFname(), "nitish");
type(l.getLname(), "kumar");

@Then("User click signup button")


public void user_click_signup_button() {
l = new LoginPOJOClass();

btnClick(l.getSignup());
}

@When("User enters username and password for {int}d")


public void user_enters_username_and_password_for_d(Integer int1,
io.cucumber.datatable.DataTable d) {
List<String> li = d.asList();
l = new LoginPOJOClass();

type(l.getUsername(), li.get(0));
type(l.getPassword(), li.get(1));
}

@When("User enters username and password for {int}d without header")


public void user_enters_username_and_password_for_d_without_header(Integer int1,
io.cucumber.datatable.DataTable d) {
List<List<String>> list = d.asLists();
l = new LoginPOJOClass();

type(l.getUsername(), list.get(0).get(0));
type(l.getPassword(), list.get(0).get(1));

@When("User enters the username and password")


CUCUMBER
public void user_enters_the_username_and_password() throws IOException {
l = new LoginPOJOClass();
type(l.getUsername(), getDataFromExcel(1, 1));
type(l.getPassword(), getDataFromExcel(1, 1));

@Then("User clicks the login button")


public void user_clicks_the_login_button() {
btnClick(l.getLogin());
}
@When("User enters the username and password using excel data")
public void user_enters_the_username_and_password_using_excel_data() throws IOException {
l = new LoginPOJOClass();
type(l.getUsername(), getDataFromExcel(1, 1));
type(l.getPassword(), getDataFromExcel(1, 1));
}

@Then("User click the login button")


public void user_click_the_login_button() {
l = new LoginPOJOClass();
btnClick(l.getLogin());
}

Stepdfinition Class

Output
CUCUMBER

You might also like