You are on page 1of 31

CUCUMBER

FRAMEWORK

Prepared By

PRASATH SAMBANTHAMOORTHY, QA TEAM

Page No 1 TATA Consultancy Services


CONTENTS
......................................................................................................................................................................... 2

SECTION 1: INTRODUCTION TO BDD.................................................................................................................... 4

TDD - TEST DRIVEN DEVELOPMENT:..................................................................................................................................... 4


ATDD - ACCEPTANCE TEST DRIVEN DEVELOPMENT:................................................................................................................... 4
BDD - BEHAVIOR DRIVEN DEVELOPMENT:............................................................................................................................... 4
What is Gherkin?....................................................................................................................................................4
Gherkin Keywords:..................................................................................................................................................4
layers in cucumber:................................................................................................................................................ 5
cucumber dependencies:........................................................................................................................................6
tools & plugin: ....................................................................................................................................................... 7

SECTION 2: HOW TO CREATE FEATURE FILE WITH BDD TEST SCENARIOS.............................................................. 8

STEPS TO CREATE FEATURE FILE:............................................................................................................................................. 8

GUIDELINES TO FOLLOW TO WRITE THE FEATURE FILE:.................................................................................................................... 8


FEATURE FILE SAMPLE:........................................................................................................................................................ 8

SECTION 3: HOW TO IMPLEMENT STEP DEFINITION FOR THE SCENARIOS ............................................................. 9

STEPS TO CREATE STEP DEFINITION FILE:..................................................................................................................................... 9

GUIDELINES TO FOLLOW TO IMPLEMENT STEP DEFINITION FILE:........................................................................................................... 9


STEP DEFINITION FILE SAMPLE:............................................................................................................................................... 9

SECTION 4: HOW TO EXECUTE THE FEATURE FILES............................................................................................. 11

STEPS TO FOLLOW TO CREATE THE RUNNER FILE:......................................................................................................................... 11

GUIDELINES TO FOLLOW TO MAKE RUNNER FILE:........................................................................................................................ 11


RUNNER FILE SAMPLE:...................................................................................................................................................... 11

STEPS TO RUN RUNNER FILE:............................................................................................................................................... 11

STEPS TO CREATE CUCUMBER REPORT:.................................................................................................................................... 12

runner file sample:................................................................................................................................................12

SECTION 5: HOW TO PARAMETERIZE TEST DATA FROM FEATURE FILE................................................................ 14

STEPS TO IMPLEMENT THE PARAMETERIZE DATA IN FEATURE FILE:...................................................................................................... 14

SAMPLE CODE:.............................................................................................................................................................. 14

SECTION 6: HOW TO HANDLE MULTIPLE DATA................................................................................................... 15

STEPS TO IMPLEMENT MULTIPLE DATA:.................................................................................................................................... 15

SAMPLE CODE:.............................................................................................................................................................. 15

SECTION 7: HOW TO USE BACKGROUND KEYWORD IN FEATURE FILE................................................................. 16

Page No 2 TATA Consultancy Services


PURPOSE:.................................................................................................................................................................... 16

LIMITATIONS:................................................................................................................................................................ 16

SAMPLE CODE:.............................................................................................................................................................. 16

SECTION 8: HOW TO RUN MULTIPLE FEATURE FILES AT A TIME.......................................................................... 17

WAY 1:...................................................................................................................................................................... 17
SAMPLE CODE:.............................................................................................................................................................. 17

DRAWBACKS:................................................................................................................................................................ 17

WAY 2:...................................................................................................................................................................... 17
SAMPLE CODE:.............................................................................................................................................................. 17

SECTION 9: HOOKS............................................................................................................................................ 18

PURPOSE:.................................................................................................................................................................... 18

SAMPLE CODE:.............................................................................................................................................................. 18

SECTION 10: HOW TO USE TESTNG ANNOTATION IN CUCUMBER FRAMEWORK ................................................. 21

PURPOSE:.................................................................................................................................................................... 21

STEPS TO IMPLEMENT TESTNG ANNOTATIONS IN CUCUMBER FRAMEWORK:............................................................................................ 21

SAMPLE CODE:.............................................................................................................................................................. 21

SECTION 11: TAGS............................................................................................................................................. 26

PURPOSE:.................................................................................................................................................................... 26

STEPS TO IMPLEMENT THE TAGS:.......................................................................................................................................... 26

SAMPLE CODE:.............................................................................................................................................................. 26

STEPS TO IMPLEMENT TO RUN MULTIPLE TAGS WITH DIFFERENT SCENARIOS IN CUCUMBER RUNNER FILE:........................................................... 27

SAMPLE CODE:.............................................................................................................................................................. 27

STEPS TO IMPLEMENT TO RUN MULTIPLE TAGS WITH SAME SCENARIOS IN CUCUMBER RUNNER FILE:................................................................ 28

SAMPLE CODE:.............................................................................................................................................................. 28

EXCLUDE A TAG:............................................................................................................................................................. 30

SAMPLE CODE:.............................................................................................................................................................. 30

SECTION 12: FEW CUCUMBER EXCEPTION.......................................................................................................... 31

Page No 3 TATA Consultancy Services


Section 1: Introduction to BDD
TDD - TEST DRIVEN DEVELOPMENT:
Process:

 Write the test cases first based on the requirement

 Test cases should be approved by the project team

 Development will be based on the test cases

ATDD - ACCEPTANCE TEST DRIVEN DEVELOPMENT:


Process:

 Write the acceptance test cases first based on the requirement

 Test cases should be approved by the project team

 Development will be based on the test cases

Con: Communication gap between the Technical (Dev & Test teams) and Non-Technical teams (Client /
Product Team)

BDD - BEHAVIOR DRIVEN DEVELOPMENT:


Process:

 Write the test scenarios using Gherkin (Common) language first

 Approve the test scenarios

 BDD test scenarios will drive the development

Purpose:

 To address the communication gap between technical and non-technical teams

 “Getting the Words Right” is the focus of BDD, leading to building one language that is
understood the same way by the project stakeholders

 Vision is to bring in consistency, accuracy and meaning

 Behavior Driven Development (BDD) claims “the words you use about something influence the
way you think about that”

WHAT IS GHERKIN?
 Gherkin is the language that cucumber understands. It is a “Business Readable, Domain Specific
language”

GHERKIN KEYWORDS:
S.NO Gherkin Keywords Description

Page No 4 TATA Consultancy Services


1 Feature Used to describe the functionality
.

2 Background Used to have common precondition for all the scenarios in a feature file
.

3 Scenario Used to describe the high level test scenarios


.

4 Given Used to represent Pre-requisite for a scenario


.

5 When Used to represent test step / test condition


.

6 Then Used to represent the expected result for positive scenario


.

7 But Used to represent the expected result for negative scenario


.

8 And Used to replace this And keyword for readability purpose, if any keyword
. coming multiple times

9 Scenario Outline Used to represent handling multiple data in a single scenario


.

1 Examples Used to pass the multiple data with column name and this keyword should
0 follow Scenario Outline
.

BDD AUTOMATION TOOLS:

 Below are the BDD automation tools with respect to Java & .Net languages.

S.NO Language BDD Tools


1 Java Cucumber, JBehave, JDave, FitNesse, Concordion
.

2 .Net SpecFlow
.

LAYERS IN CUCUMBER:
We have the three layers in Cucumber which are described below:

 Features - This layer we are going to write a test case using Gherkin language

Page No 5 TATA Consultancy Services


 Step Definition / Implementation - This layer whatever test cases we prepared that will be implemented
using selenium code / java code in step definition layer

 Runner (TestNG) - This layer is used for an execution

CUCUMBER DEPENDENCIES:
Below are the dependencies which we need to add into POM.xml file.

<dependency>

<groupId>io.cucumber</groupId>

<artifactId>cucumber-java</artifactId>

<version>X.X.X</version>

</dependency>

<dependency>

<groupId>io.cucumber</groupId>

<artifactId>cucumber-testng</artifactId>

<version>X.X.X</version>

</dependency>

Page No 6 TATA Consultancy Services


TOOLS & PLUGIN:
For execution we need the below two plug-in.

 Cucumber Eclipse Plugin - This is for our execution

 Natural 0.9 - This is the editor to write the Gherkin scenarios using BDD/ATDD
files, without Natural plug-in also we can write but this Natural 0.9 is the better way to write the BDD test
cases easily.

Page No 7 TATA Consultancy Services


Section 2: How to create feature file with BDD test scenarios
STEPS TO CREATE FEATURE FILE:
 Right Click on features package -> New -> File (or) Right Click on features package -> New ->
Other -> General -> File

 Enter the filename.feature (here file extension .feature is the mandatory)

GUIDELINES TO FOLLOW TO WRITE THE FEATURE FILE:


 Feature: Start with upper case; End with : and then give 1 space

 Scenario: Start with upper case; End with : and then give 1 space

 Except Feature & Scenario remaining Gherkin keyword, start with upper case and then give 1
space

 Every Gherkin keyword should start with new line

FEATURE FILE SAMPLE:

Feature: Application login functionality

Scenario: Login with positive credential


Given Open the chrome browser
And Load the application url
And Enter the username as prasathuser1@gmail.com
And Enter the password as test@123
When Click on Login button
Then Homepage should be displayed

Page No 8 TATA Consultancy Services


Section 3: How to implement Step Definition for the scenarios
STEPS TO CREATE STEP DEFINITION FILE:
 Right Click on steps package -> New -> Class

 Enter the filename and create the java file

GUIDELINES TO FOLLOW TO IMPLEMENT STEP DEFINITION FILE:


 Each steps in feature file we have to write the separate method in Step Definition file

 Before each step definition method, to create the linking between the Step definition method &
feature file each steps we have to use @ dollar symbol followed by corresponding feature file
step Gherkin keyword and followed by with in open and close bracket and within double quotes
we need mention the feature file step description. (For Example: @Given(“Open the chrome
browser”))

STEP DEFINITION FILE SAMPLE:


package steps;

import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.github.bonigarcia.wdm.WebDriverManager;

public class StepDefinition {

public WebDriver driver;

@Given("Open the chrome browser")


public void openBrowser() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
}

@Given("Load the application url")


public void loadApplicationUrl() {
driver.get("https://www.amazon.com/");
}

@Given("Enter the username as prasathuser1@gmail.com")


public void enterUsername() {

driver.findElement(By.id("username")).sendKeys("prasathuser1@gmail.com");
}

Page No 9 TATA Consultancy Services


@Given("Enter the password as test@123")
public void enterPassword() {
driver.findElement(By.id("password")).sendKeys("test@123");
}

@When("Click on Login button")


public void clickLoginButton() {
driver.findElement(By.className("decorativeSubmit")).click();
}

@Then("Homepage should be displayed")


public void verifyHomePage() {
boolean displayed =
driver.findElement(By.linkText("Home")).isDisplayed();

if(displayed)
System.out.println("Home Page is displayed");
else
System.out.println("Home Page is not displayed");
}
}

Page No 10 TATA Consultancy Services


Section 4: How to execute the feature files
STEPS TO FOLLOW TO CREATE THE RUNNER FILE:
 Right Click on runner package -> New -> Class

 Enter the filename and create the java file

GUIDELINES TO FOLLOW TO MAKE RUNNER FILE:


 Within runner file and before runner class we need to use the @CucumberOptions annotation and
followed by attributes (features, glue, monochrome & publish)

 In runner file class, extends the AbstractTestNGCucumberTests class to make this file as
runner

 In “features” attribute, mention the feature file path to run

 In “glue” attribute, mentioned the step definition file package folder name

 Use “monochrome = true” attribute to remove the junk characters in the output

RUNNER FILE SAMPLE:


package runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
features = "src/main/java/features/login.feature",
glue = "steps",
monochrome = true
)
public class CucumberRunner extends AbstractTestNGCucumberTests{

STEPS TO RUN RUNNER FILE:


 Right Click on runner file -> Run As -> TestNG Test

 Output will be displayed into console as shown in the below

Page No 11 TATA Consultancy Services


STEPS TO CREATE CUCUMBER REPORT:
 Add “publish = true” attribute in the runner file under @CucumberOptions annotation and then
run it

RUNNER FILE SAMPLE:


package runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
features = "src/main/java/features/login.feature",
glue = "steps",
monochrome = true,
publish = true
)
public class CucumberRunner extends AbstractTestNGCucumberTests{

Note: Cucumber report will be created and report link will be displayed into the console. But this report
will be available within 24 hours only and if we need to keep this reports forever then we need to create
an account in https://reports.cucumber.io/profile.

Page No 12 TATA Consultancy Services


Page No 13 TATA Consultancy Services
Section 5: How to parameterize test data from feature file
STEPS TO IMPLEMENT THE PARAMETERIZE DATA IN FEATURE FILE:
 Enclose the data with '' in the feature file

 Replace the data in the cucumber annotation text with {string} in step definition file (Note: string
starting character is small letter)

 Create argument for the method to get input in the step definition method

 Replace the hardcoded value with the argument variable in the step definition method

SAMPLE CODE:

FEATURE FILE:
Feature: Application login functionality

Scenario: Login with positive credential


Given Open the chrome browser
And Load the application url
And Enter the username as 'prasathuser1@gmail.com'
And Enter the password as 'test@123'
When Click on Login button
Then Homepage should be displayed

STEP DEFINITION FILE:


@Given("Enter the username as {string}")
public void enterUsername(String userName) {
driver.findElement(By.id("username")).sendKeys(userName);
}

@Given("Enter the password as {string}")


public void enterPassword(String password) {
driver.findElement(By.id("password")).sendKeys(password);
}

Page No 14 TATA Consultancy Services


Section 6: How to handle multiple data
STEPS TO IMPLEMENT MULTIPLE DATA:
 Scenario Outline keyword should be used

 Examples should follow Scenario Outline

 Define the column names inside || after Examples (first row for column names)

 Second row onwards give the data inside ||

SAMPLE CODE:

FEATURE FILE:
Feature: Application login functionality

Scenario Outline: Login with positive credential


Given Open the chrome browser
And Load the application url <url>
And Enter the username as <username>
And Enter the password as <password>
When Click on Login button
Then Homepage should be displayed

Examples:
|url|username|password|
|' https://www.amazon.com/'|'prasathuser1@gmail.com'|'test@123'|
|' https://www.amazon.com/'|'prasathuser2@gmail.com'|'test@123'|

STEP DEFINITION FILE:


@Given("Load the application url {string}")
public void loadApplicationUrl(String url) {
driver.get(url);
}

@Given("Enter the username as {string}")


public void enterUsername(String userName) {
driver.findElement(By.id("username")).sendKeys(userName);
}

@Given("Enter the password as {string}")


public void enterPassword(String password) {
driver.findElement(By.id("password")).sendKeys(password);
}

Page No 15 TATA Consultancy Services


Section 7: How to use Background Keyword in Feature file

PURPOSE:
 Background Keyword used to have common precondition for all the scenarios in a feature file

LIMITATIONS:
 Background is not applicable for all the scenarios across multiple feature files

 There is no keyword to execute common post condition

Note: Above two limitation overcome using Hooks

SAMPLE CODE:

FEATURE FILE:
Feature: Application login functionality

Background:
Given Open the chrome browser
And Load the application url 'https://www.amazon.com/'

Scenario Outline: Login with positive credential


Given Enter the username as <username>
And Enter the password as <password>
When Click on Login button
Then Homepage should be displayed

Examples:
|username|password|
|'prasathuser1@gmailcom'|'test1@123'|
|'prasathuser2@gmail.com'|'test2@123'|

Scenario: Login with negative credential


Given Enter the username as 'Demo'
And Enter the password as 'test3@123'
When Click on Login button
But Error message should be displayed

Page No 16 TATA Consultancy Services


Section 8: How to run multiple feature files at a time
WAY 1:
 In runner file, provide the multiple feature file path inside the flower bracket with comma
separator in features attribute under @CucumberOptions annotation.

SAMPLE CODE:

RUNNER FILE:
package runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
features = {"src/main/java/features/login.feature",
"src/main/java/features/createPage.feature"},
glue = "steps",
monochrome = true,
publish = true
)
public class CucumberRunner extends AbstractTestNGCucumberTests{

DRAWBACKS:
 We don’t have the control which feature file will execute first and it will be executed as per the
ASCII values of those feature files which order is listed into the features package in editor

 If we have the 100 feature files, then we need to provide one by one in the feature attribute

WAY 2:
 In runner file, provide the feature file available path in features attribute under
@CucumberOptions annotation. So that all the available feature files under feature package will
be executed.

SAMPLE CODE:

RUNNER FILE:
package runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
features = "src/main/java/features",
glue = "steps",
monochrome = true,
publish = true
)
public class CucumberRunner extends AbstractTestNGCucumberTests{

Page No 17 TATA Consultancy Services


Section 9: Hooks
PURPOSE:
 To overcome background limitations

 To provide precondition for across all feature files - @Before

 To provide post condition for across all feature files - @After

SAMPLE CODE:

FEATURE FILE:

Feature: Application login functionality

#Background:
#Given Open the chrome browser
#And Load the application url 'http://automationpractice.com/index.php'

Scenario Outline: Login with positive credential


Given Enter the username as <username>
And Enter the password as <password>
When Click on Login button
Then Homepage should be displayed

Examples:
|username|password|
|'prasathuser1@gmail.com'|'AutomationDemo1@2022'|
|'prasathuser2@gmail.com'|'AutomationDemo@2022'|

Scenario: Login with negative credential


Given Enter the username as 'Demo'
And Enter the password as 'testing1'
When Click on Login button
But Error message should be displayed

STEP DEFINITION FILE:

package steps;

import org.openqa.selenium.By;
import io.cucumber.java.en.But;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class StepDefinition extends Baseclass{

/*
* @Given("Open the chrome browser") public void openBrowser() {
* WebDriverManager.chromedriver().setup(); driver = new ChromeDriver();
* driver.manage().window().maximize();
* driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5)); }
*
* @Given("Load the application url {string}") public void

Page No 18 TATA Consultancy Services


* loadApplicationUrl(String url) { driver.get(url);
*
driver.findElement(By.xpath("//div[@class='header_user_info']//a")).click();
* }
*/

@Given("Enter the username as {string}")


public void enterUsername(String userName) {
driver.findElement(By.id("email")).sendKeys(userName);
}

@Given("Enter the password as {string}")


public void enterPassword(String password) {
driver.findElement(By.id("passwd")).sendKeys(password);
}

@When("Click on Login button")


public void clickLoginButton() {
driver.findElement(By.xpath("//button[@id='SubmitLogin']")).click();
}

@Then("Homepage should be displayed")


public void verifyHomePage() {
boolean displayed = driver.findElement(By.xpath("//a[@title='My
Store']")).isDisplayed();

if(displayed)
System.out.println("Home Page is displayed");
else
System.out.println("Home Page is not displayed");
}

@But("Error message should be displayed")


public void verifyErrorMessage() {
System.out.println("User name is not wrong");
}
}

HOOKS FILE:

package steps;

import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.github.bonigarcia.wdm.WebDriverManager;

public class Hooks extends Baseclass{

@Before
public void precondition() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();

Page No 19 TATA Consultancy Services


driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.get("http://automationpractice.com/index.php");

driver.findElement(By.xpath("//div[@class='header_user_info']//a")).click();
}

@After
public void postcondition() {
driver.close();
}
}

BASE CLASS FILE:

package steps;

import org.openqa.selenium.chrome.ChromeDriver;

public class Baseclass {


public static ChromeDriver driver;
}

RUNNER FILE:

package runner;

import io.cucumber.testng.AbstractTestNGCucumberTests;

import io.cucumber.testng.CucumberOptions;

@CucumberOptions(

features = "src/main/java/features/login.feature",

glue = "steps",

monochrome = true,

publish = true

public class CucumberRunner extends AbstractTestNGCucumberTests{

Page No 20 TATA Consultancy Services


Section 10: How to use TestNG Annotation in Cucumber Framework
PURPOSE:
 To use any TestNG annotations also in cucumber framework

STEPS TO IMPLEMENT TESTNG ANNOTATIONS IN CUCUMBER FRAMEWORK:


 Copy the precondition & postcondition methods from Hooks to Baseclass

 Replace @AfterMethod & @BeforeMethod TestNG annotation instead of @Before & @After
cucumber annotations in Baseclass

 Extends Baseclass in CucumberRunner class instead of AbstractTestNGCucumberTests

 Extends AbstractTestNGCucumberTests class in Baseclass

 Command @After & @Before method in Hooks class

SAMPLE CODE:

FEATURE FILE:

Feature: Application login functionality

#Background:
#Given Open the chrome browser
#And Load the application url 'http://automationpractice.com/index.php'

Scenario Outline: Login with positive credential


Given Enter the username as <username>
And Enter the password as <password>
When Click on Login button
Then Homepage should be displayed

Examples:
|username|password|
|'prasathuser1@gmail.com'|'AutomationDemo1@2022'|
|'prasathuser2@gmail.com'|'AutomationDemo@2022'|

Scenario: Login with negative credential


Given Enter the username as 'Demo'
And Enter the password as 'testing1'
When Click on Login button
But Error message should be displayed

STEP DEFINITION FILE:

package steps;

import org.openqa.selenium.By;

import io.cucumber.java.en.But;

import io.cucumber.java.en.Given;

Page No 21 TATA Consultancy Services


import io.cucumber.java.en.Then;

import io.cucumber.java.en.When;

public class StepDefinition extends Baseclass{

/*

* @Given("Open the chrome browser") public void openBrowser() {

* WebDriverManager.chromedriver().setup(); driver = new ChromeDriver();

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

* driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5)); }

* @Given("Load the application url {string}") public void

* loadApplicationUrl(String url) { driver.get(url);

* driver.findElement(By.xpath("//div[@class='header_user_info']//a")).click();

*}

*/

@Given("Enter the username as {string}")

public void enterUsername(String userName) {

driver.findElement(By.id("email")).sendKeys(userName);

@Given("Enter the password as {string}")

public void enterPassword(String password) {

driver.findElement(By.id("passwd")).sendKeys(password);

@When("Click on Login button")

public void clickLoginButton() {

driver.findElement(By.xpath("//button[@id='SubmitLogin']")).click();

Page No 22 TATA Consultancy Services


@Then("Homepage should be displayed")

public void verifyHomePage() {

boolean displayed = driver.findElement(By.xpath("//a[@title='My Store']")).isDisplayed();

if(displayed)

System.out.println("Home Page is displayed");

else

System.out.println("Home Page is not displayed");

@But("Error message should be displayed")

public void verifyErrorMessage() {

System.out.println("User name is not wrong");

BASECLASS FILE:

package steps;

import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.github.bonigarcia.wdm.WebDriverManager;

public class Baseclass extends AbstractTestNGCucumberTests{


public static ChromeDriver driver;

@BeforeMethod
public void precondition() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.get("http://automationpractice.com/index.php");

driver.findElement(By.xpath("//div[@class='header_user_info']//a")).click();
}

@AfterMethod
public void postcondition() {
driver.close();

Page No 23 TATA Consultancy Services


}
}

HOOKSCLASS FILE:

package steps;

import java.time.Duration;

import org.openqa.selenium.By;

import org.openqa.selenium.chrome.ChromeDriver;

import io.cucumber.java.After;

import io.cucumber.java.Before;

import io.github.bonigarcia.wdm.WebDriverManager;

public class Hooks extends Baseclass{

//@Before

public void precondition() {

WebDriverManager.chromedriver().setup();

driver = new ChromeDriver();

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

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

driver.get("http://automationpractice.com/index.php");

driver.findElement(By.xpath("//div[@class='header_user_info']//a")).click();

//@After

public void postcondition() {

driver.close();}}

CUCUMBERRUNNER FILE:

package runner;

import io.cucumber.testng.AbstractTestNGCucumberTests;

import io.cucumber.testng.CucumberOptions;

import steps.Baseclass;

Page No 24 TATA Consultancy Services


@CucumberOptions(

features = "src/main/java/features/login.feature",

glue = "steps",

monochrome = true,

publish = true

public class CucumberRunner extends Baseclass{

Page No 25 TATA Consultancy Services


Section 11: Tags
PURPOSE:
 To categories the test cases and execute selected feature file / scenarios – Grouping the test
scenarios

STEPS TO IMPLEMENT THE TAGS:


 To use @ symbol followed by any name in before Scenario keyword in feature file

 Include tags attribute with the @ specified name which is given in the feature file to the inside
@CucumberOpitons annotation in Cucumber runner file

SAMPLE CODE:

FEATURE FILE:

Feature: Application login functionality

#Background:
#Given Open the chrome browser
#And Load the application url 'http://automationpractice.com/index.php'

@Regression
Scenario Outline: Login with positive credential
Given Enter the username as <username>
And Enter the password as <password>
When Click on Login button
Then Homepage should be displayed

Examples:
|username|password|
|'prasathuser1@gmail.com'|'AutomationDemo1@2022'|
|'prasathuser2@gmail.com'|'AutomationDemo@2022'|

Scenario: Login with negative credential


Given Enter the username as 'Demo'
And Enter the password as 'testing1'
When Click on Login button
But Error message should be displayed

CUCUMBER RUNNER FILE:

package runner;

import io.cucumber.testng.AbstractTestNGCucumberTests;

import io.cucumber.testng.CucumberOptions;

import steps.Baseclass;

Page No 26 TATA Consultancy Services


@CucumberOptions(

features = "src/main/java/features",

glue = "steps",

monochrome = true,

publish = true,

tags = "@Regression" // execute only scenarios having @Regression

public class CucumberRunner extends Baseclass{

}
STEPS TO IMPLEMENT TO RUN MULTIPLE TAGS WITH DIFFERENT SCENARIOS IN CUCUMBER RUNNER FILE:
 Use “or” within quotes between two tags in tags attribute inside @CucumberOpitons annotation
in Cucumber runner file

SAMPLE CODE:

FEATURE FILE:

Feature: Application login functionality

#Background:
#Given Open the chrome browser
#And Load the application url 'http://automationpractice.com/index.php'

@Regression
Scenario Outline: Login with positive credential
Given Enter the username as <username>
And Enter the password as <password>
When Click on Login button
Then Homepage should be displayed

Examples:
|username|password|
|'prasathuser1@gmail.com'|'AutomationDemo1@2022'|
|'prasathuser2@gmail.com'|'AutomationDemo@2022'|

@Sanity
Scenario: Login with negative credential
Given Enter the username as 'Demo'
And Enter the password as 'testing1'
When Click on Login button
But Error message should be displayed

Page No 27 TATA Consultancy Services


CUCUMBER RUNNER FILE:

package runner;

import io.cucumber.testng.AbstractTestNGCucumberTests;

import io.cucumber.testng.CucumberOptions;

import steps.Baseclass;

@CucumberOptions(

features = "src/main/java/features",

glue = "steps",

monochrome = true,

publish = true,

//tags = "@Regression" // execute only scenarios having


@Regression

tags = "@Regression or @Sanity" // executing the all scenarios with


either @Regression / @Sanity tags

public class CucumberRunner extends Baseclass{

}
STEPS TO IMPLEMENT TO RUN MULTIPLE TAGS WITH SAME SCENARIOS IN CUCUMBER RUNNER FILE:
 Use “and” within quotes between two tags in tags attribute inside @CucumberOpitons annotation
in Cucumber runner file

SAMPLE CODE:

FEATURE FILE:

Feature: Application login functionality


#Background:
#Given Open the chrome browser
#And Load the application url 'http://automationpractice.com/index.php'
@Regression @sanity
Scenario Outline: Login with positive credential
Given Enter the username as <username>
And Enter the password as <password>
When Click on Login button

Page No 28 TATA Consultancy Services


Then Homepage should be displayed

Examples:
|username|password|
|'prasathuser1@gmail.com'|'AutomationDemo1@2022'|
|'prasathuser2@gmail.com'|'AutomationDemo@2022'|

@Sanity
Scenario: Login with negative credential
Given Enter the username as 'Demo'
And Enter the password as 'testing1'
When Click on Login button
But Error message should be displayed

CUCUMBER RUNNER FILE:

package runner;

import io.cucumber.testng.AbstractTestNGCucumberTests;

import io.cucumber.testng.CucumberOptions;

import steps.Baseclass;

@CucumberOptions(

features = "src/main/java/features",

glue = "steps",

monochrome = true,

publish = true,

//tags = "@Regression" // execute only scenarios having


@Regression

//tags = "@Regression or @Sanity" // executing the all scenarios with


either @Regression / @Sanity tags

tags = "@Regression and @Sanity" // executing the all scenarios having


both @Regression & @Sanity tags

public class CucumberRunner extends Baseclass{}

Page No 29 TATA Consultancy Services


EXCLUDE A TAG:
 Use “not” within quotes followed by tab name in tags attribute inside @CucumberOpitons
annotation in Cucumber runner file

SAMPLE CODE:

CUCUMBER RUNNER FILE:

package runner;

import io.cucumber.testng.AbstractTestNGCucumberTests;

import io.cucumber.testng.CucumberOptions;

import steps.Baseclass;

@CucumberOptions(

features = "src/main/java/features",

glue = "steps",

monochrome = true,

publish = true,

//tags = "@Regression" // execute only scenarios having


@Regression

//tags = "@Regression or @Sanity" // executing the all scenarios with


either @Regression / @Sanity tags

//tags = "@Regression and @Sanity" // executing the all scenarios having


@Regression & @Sanity tags

tags = "not @Sanity" // exclude a tag

public class CucumberRunner extends Baseclass{

Page No 30 TATA Consultancy Services


Section 12: Few Cucumber Exception

S.NO Exception Description


1 UndefinedStepException When step is not implemented in step definition
.

2 DuplicateStepDefinitionExceptio Same step is implemented with multiple methods in steps definition


. n file

3 InvalidMethodException You are not allowed to extend classes that define Step Definition or
. hooks

Page No 31 TATA Consultancy Services

You might also like