Professional Documents
Culture Documents
FRAMEWORK
Prepared By
SAMPLE CODE:.............................................................................................................................................................. 14
SAMPLE CODE:.............................................................................................................................................................. 15
LIMITATIONS:................................................................................................................................................................ 16
SAMPLE CODE:.............................................................................................................................................................. 16
WAY 1:...................................................................................................................................................................... 17
SAMPLE CODE:.............................................................................................................................................................. 17
DRAWBACKS:................................................................................................................................................................ 17
WAY 2:...................................................................................................................................................................... 17
SAMPLE CODE:.............................................................................................................................................................. 17
SECTION 9: HOOKS............................................................................................................................................ 18
PURPOSE:.................................................................................................................................................................... 18
SAMPLE CODE:.............................................................................................................................................................. 18
PURPOSE:.................................................................................................................................................................... 21
SAMPLE CODE:.............................................................................................................................................................. 21
PURPOSE:.................................................................................................................................................................... 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
Con: Communication gap between the Technical (Dev & Test teams) and Non-Technical teams (Client /
Product Team)
Purpose:
“Getting the Words Right” is the focus of BDD, leading to building one language that is
understood the same way by the project stakeholders
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
2 Background Used to have common precondition for all the scenarios in a feature file
.
8 And Used to replace this And keyword for readability purpose, if any keyword
. coming multiple times
1 Examples Used to pass the multiple data with column name and this keyword should
0 follow Scenario Outline
.
Below are the BDD automation tools with respect to Java & .Net languages.
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
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>
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.
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
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”))
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;
driver.findElement(By.id("username")).sendKeys("prasathuser1@gmail.com");
}
if(displayed)
System.out.println("Home Page is displayed");
else
System.out.println("Home Page is not displayed");
}
}
In runner file class, extends the AbstractTestNGCucumberTests class to make this file as
runner
In “glue” attribute, mentioned the step definition file package folder name
Use “monochrome = true” attribute to remove the junk characters in the output
@CucumberOptions(
features = "src/main/java/features/login.feature",
glue = "steps",
monochrome = true
)
public class CucumberRunner extends AbstractTestNGCucumberTests{
@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.
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
Define the column names inside || after Examples (first row for column names)
SAMPLE CODE:
FEATURE FILE:
Feature: Application login functionality
Examples:
|url|username|password|
|' https://www.amazon.com/'|'prasathuser1@gmail.com'|'test@123'|
|' https://www.amazon.com/'|'prasathuser2@gmail.com'|'test@123'|
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
SAMPLE CODE:
FEATURE FILE:
Feature: Application login functionality
Background:
Given Open the chrome browser
And Load the application url 'https://www.amazon.com/'
Examples:
|username|password|
|'prasathuser1@gmailcom'|'test1@123'|
|'prasathuser2@gmail.com'|'test2@123'|
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{
SAMPLE CODE:
FEATURE FILE:
#Background:
#Given Open the chrome browser
#And Load the application url 'http://automationpractice.com/index.php'
Examples:
|username|password|
|'prasathuser1@gmail.com'|'AutomationDemo1@2022'|
|'prasathuser2@gmail.com'|'AutomationDemo@2022'|
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;
/*
* @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
if(displayed)
System.out.println("Home Page is displayed");
else
System.out.println("Home Page is not displayed");
}
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;
@Before
public void precondition() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.findElement(By.xpath("//div[@class='header_user_info']//a")).click();
}
@After
public void postcondition() {
driver.close();
}
}
package steps;
import org.openqa.selenium.chrome.ChromeDriver;
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
Replace @AfterMethod & @BeforeMethod TestNG annotation instead of @Before & @After
cucumber annotations in Baseclass
SAMPLE CODE:
FEATURE FILE:
#Background:
#Given Open the chrome browser
#And Load the application url 'http://automationpractice.com/index.php'
Examples:
|username|password|
|'prasathuser1@gmail.com'|'AutomationDemo1@2022'|
|'prasathuser2@gmail.com'|'AutomationDemo@2022'|
package steps;
import org.openqa.selenium.By;
import io.cucumber.java.en.But;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
/*
* driver.manage().window().maximize();
* driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5)); }
* driver.findElement(By.xpath("//div[@class='header_user_info']//a")).click();
*}
*/
driver.findElement(By.id("email")).sendKeys(userName);
driver.findElement(By.id("passwd")).sendKeys(password);
driver.findElement(By.xpath("//button[@id='SubmitLogin']")).click();
if(displayed)
else
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;
@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();
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;
//@Before
WebDriverManager.chromedriver().setup();
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
driver.close();}}
CUCUMBERRUNNER FILE:
package runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import steps.Baseclass;
features = "src/main/java/features/login.feature",
glue = "steps",
monochrome = true,
publish = true
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:
#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'|
package runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import steps.Baseclass;
features = "src/main/java/features",
glue = "steps",
monochrome = true,
publish = true,
}
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:
#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
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,
}
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:
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
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,
SAMPLE CODE:
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,
3 InvalidMethodException You are not allowed to extend classes that define Step Definition or
. hooks