Cucumber BDD
Cucumber BDD
Cucumber – BDD
http://tutorialsninja.com/demo/
Step 1—Create a maven project using Archetype –‘maven-archetype-apache’.
Step 2—Go to eclipse marketplace and install the ‘cucumber eclipse plugin’.
Then restart the eclipse.
Step 3—Add cucumber libraries to the maven project
Cucumber-java
Cucumber-Junit
Cucumber-core
Selenium-java
JUnit
Step 4—Create package for ‘Feature files’ and create feature files with
.feature extension.
Feature file: A feature file is usually a common file which stores feature,
scenarios, and feature description to be tested. The feature file is an entry
point, to write the cucumber tests and used as a live document at the time of
2 of 15
testing. The product owner will review these feature files, provide feedback,
and approve these files. Feature file in a project only recognized by the
cucumber tool.
Feature: login
The user should be able to access his account using login functionality
Background:
Given User has opened the application URL
And Navigated to Login Page
Step 5—Run the feature file and get copy the implementations methods which
thrown by console.
Step 6—Create package ‘stepDefs’ and under the package create class (class
name must match with feature file name. Every step in feature file will be
implemented as method in stepDef file.
@RunWith(Cucumber.class)
@CucumberOptions(plugin = { "pretty", "html:target/CucumberHTMLReport.html" })
Background:
The Background keyword is applied to replicate the same steps before all
Scenarios within a Feature File.
Scenario Outline:
The Scenario Outline keyword can be used to run the same Scenario multiple
times, with different combinations of values.
HTML Report:
@CucumberOptions(plugin= {"html:target/CucumberHTMLReport.html"})
Regular Expressions:
DuplicateStepDefinitionException
When Cucumber finds multiple Step Definitions that are the same, it throws a
Duplicate Step Definitions exception.
Sol: remove the duplicate stepdef in implementation
AmbiguousStepDefinitionsException
When Cucumber Finds a Step matches more than one Step Definitions using
regular expressions, it throws an Ambiguous Step Definitions exception.
Sol: remove the duplicate stepdef in implementation
Comments in feature file
# is used to comments any steps in feature file
Ctrl+/ Shortcut for commenting multiple line in feature file
Tags in cucumber
tags="not @smoke"
-- will execute the Scenario’s which is not having the these tag
Hooks
@Before and @After Hooks in cucumber
import io.cucumber.java.After;
import io.cucumber.java.Before;
@Before
public void setup() {
System.out.println("browser got opened");
}
@After
public void teardown() {
System.out.println("Browser got closed");
}
5 of 15
These tags will get executed before and after for every scenario while
execution time
Tagged Hooks in cucumber
We can tag the Hooks for particular scenarios by addressing with their tags.
Then Hooks tags will get execute for that particular tags only.
@Before("@smoke")
public void setup() {
System.out.println("browser got opened");
}
@After("@smoke")
public void teardown() {
System.out.println("Browser got closed");
}
AfterStep this will get executed after finishing of every step execution in
scenario
@BeforeStep
public void beforeStep() {
System.out.println("before every step");
}
@AfterStep
public void afterStep() {
System.out.println("after every step");
}
To be run with particular tags then follow below
@BeforeStep("@register")
public void beforeStep() {
System.out.println("before every step");
}
@AfterStep("@register")
public void afterStep() {
System.out.println("after every step");
}
6 of 15
@Before(order=1)
public void setupone() {
System.out.println("setup one got opened");
}
@Before(order=0)
public void setuptwo() {
System.out.println("set up two got opened");
}
@Before(order=2)
public void setupthree() {
System.out.println("setup three got opened");
}
@Before(value="@register" , order=0)
public void setup() {
System.out.println("Browser got opened");
}
Data Tables
When user need to fill multiple fields with data that time data tables will
useful to reduce to steps in feature file
E.g.:- Forms filling, registration
Feature file
StepDefinition
Runner class name should have the word ‘test’. So that maven identify
the test class.
mvn test-Dcucumber.filter.tags=”@login”
@RunWith(Cucumber.class)
@CucumberOptions(
features="src/test/resources/features",
glue="stepDefinitions",
tags="@all and not @div and not @wip and not @ignore",
plugin = { "pretty", "html:target/CucumberHTMLReport.html"
}
)
}
10 of 15
@Before()
public void setup(Scenario scenario) {
System.out.println("Browser got opened--" + scenario.getName());
}
@After()
public void teardown(Scenario scenario) {
System.out.println("Browser got closed--" + scenario.getName());
}
ClassNotFoundException in cucumber
@Before()
public void setup(Scenario scenario) {
System.out.println("Browser got opened--" + scenario.getName());
}
@After()
public void teardown(Scenario scenario) {
System.out.println("Browser got closed--" + scenario.getName());
}
@BeforeStep()
public void beforeStep() {
System.out.println("before every step");
}
@AfterStep()
public void afterStep() {
System.out.println("after every step");
}
Make changes in TestRunner class as shown in the below to run with hooks.
@RunWith(Cucumber.class)
@CucumberOptions(
features="src/test/resources/features",
glue={"stepDefinitions","hooks"},
tags="@all",
plugin = { "pretty", "html:target/CucumberHTMLReport.html" }
)
dryRun
Cucumber dry run is used for compilation of the Step Definition and Feature
files and to verify the compilation errors. The value of dry run can be
either true or false. The default value of dry run is false and it is a part
of the Test Runner Class file.
12 of 15
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features",
glue = { "stepDefinitions", "hooks" },
tags = "@forgetpassword",
dryRun = true,
plugin = {
"pretty","html:target/CucumberReports/CucumberHTMLReport.html" })
}
Publish attribute to generate cucumber reports on cloud
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features",
glue = { "stepDefinitions", "hooks" },
tags = "@all",
//dryRun = true,
publish=true,
plugin = {
"pretty","html:target/CucumberReports/CucumberHTMLReport.html" })
}
Will get in console after running TestRunner file
From cucumber.properties
13 of 15
Then go to Eclipse marketplace and install and restart your eclipse IDE
Then check ‘RunAs’ options in ‘TestRunner’ class. You should get run as
option ‘1 TestNG Test’.