BDR With Cucumber - 2
BDR With Cucumber - 2
– Training in-depth
1. Feature File
A standalone unit or a single functionality (such as a login) for a project can be called a Feature. Each of these features will have scenarios that must be
tested using Selenium integrated with Cucumber. A file that stores data about features, their descriptions, and the scenarios to be tested, is called a
Feature File.
Cucumber tests are written in these Feature Files that are stored with the extension – “.feature”. A Feature File can be given a description to make the
documentation more legible.
Example:
The Login function on a website
Feature File Name: userLogin.feature
Description: The user shall be able to login upon entering the correct username and password in the correct fields. The user should be directed to the
homepage if the username and password entered are correct.
Keywords such as GIVEN, WHEN, and THEN used to write the test in Cucumber are called Annotations.
GIVEN user navigates to login page by opening Firefox
WHEN user enters correct <username> AND <password> values
THEN user is directed to the homepage
Cucumber Framework – Contd.
2. Step Definitions
Now that the features are written in the feature files, the code for the related scenario has to be run. To know which batch of
code needs to be run for a given scenario, Steps Definitions come into the picture. A Steps Definitions file stores the mapping
data between each step of a scenario defined in the feature file and the code to be executed.
Step Definitions can use both Java and Selenium commands for the Java functions written to map a feature file to the code.
Example:
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class Steps
{
@Given("^user navigates to the login page by opening Firefox$")
//Code to Open Firefox Browser and launch the login page of application to define the GIVEN step of the feature
@When("^user enters correct username and password values$")
//take inputs for username and password fields using find element by xpath. Put the correct username and password values as
inputs to define the WHEN step of the feature
@Then (“^user gets directed to homepage$”)
//Direct to the Homepage of the application as a result of correct username and password inputs in the WHEN step. This
would define the THEN step of the feature
3. Test Runner File
To run the test, user needs a Test Runner File, which is a JUnit Test Runner Class containing the Step Definition location and
the other primary metadata required to run the test.
The Test Runner File uses the @RunWith() Annotation from JUnit for executing tests. It also uses the @CucumberOptions
Annotation to define the location of feature files, step definitions, reporting integrations, etc.
Background in Cucumber
❑ Most of the time user will find that several scenarios in the same feature start
with a common context. Cucumber provides a mechanism for this, by providing
a Background keyword, where user can specify steps that should be run before
each scenario in the feature.
❑ After these steps only user will be able to add a product to cart for checkout
and able to perform the payment. Hence, login part might be common across
all scenarios. So instead of writing them again and again for all tests, user can
move it under the Background keyword.
Scenario Outline & Examples in Cucumber
❑ Scenario includes all the possible circumstances of the feature and test scripts
for these circumstances. The keyword "Scenario" represents a scenario in
Gherkin language. One feature can have multiple scenarios, and each scenario
consists of one or more steps.
❑ Cucumber Scenario Outline in Gherkin Based on Gherkin Reference, the
Scenario Outline keyword can be used to repeat the same steps with different
values or arguments being passed to the step definitions. This is helpful if user
want to test multiple arguments in the same scenario.
❑ When to use scenario outlines user want their scenario outlines to describe
behaviors or examples that are important to the business, not detail every
boundary or test case. Adding too many rows simply creates more work and
obscures where the value is to the business.
❑ All scenario outlines are followed by Examples part that contains the set of data
that has to be passed during the execution of tests. Pipe symbol (|) is used to
specify one or more parameter values in a feature file.
❑ In a single execution, Scenario is executed only once whereas Scenario outline
(For similar data trace) can be executed multiple times depending upon the
data provided as Example.
Tags in Cucumber
❑ It might look very simple when user just have one, two, or maybe five scenarios in a feature file.
However, in real life it does not happen. For each feature under test, user may have 10, 20, or may be
a greater number of scenarios in a single feature file. They may represent different purpose
(Smoke/Integration/End to End/Regression test), different prospective (Developer/QA/BA), different
status (Ready for execution/Work in progress/defect fixes), etc.
❑ Cucumber has already provided a way to organize user scenario execution by using tags in feature file.
User can define each scenario with a useful tag. Later, in the runner file, they can decide which
specific tag (and so as the scenario(s)) user want Cucumber to execute. Tag starts with “@”. After “@”
user can have any relevant text to define their tag.
❑ Suppose, there are two or more scenarios in a feature file. User want to execute only one scenario as
part of smoke test. So, first thing is to identify that scenario and second is to tag it with “@SmokeTest”
text at the beginning of the scenario.
❑ There is no limit in defining tags within the feature file. Based on user need, they can derive tags to be
used and scenarios to be executed.
❑ Tag can also be defined at a feature level. Once user define a tag at the feature level, it ensures that
all the scenarios within that feature file inherits that tag. Depending on the nature of the scenario, user
can use more than one tag for the single feature. Whenever Cucumber finds an appropriate call, a
specific scenario will be executed.
❑ For excluding tag, user can use “not ” in JUnit runner class to exclude smoke test scenario. It will look
like the following.
@RunWith(Cucumber.class)
@Cucumber.Options(plugin = ("pretty"), tags = (“not @SmokeTest"))
Tags using AND/OR in Cucumber
❑ Pretty Report: The first plugin, is Pretty. This provides more verbose output. To
implement this, just specify plugin = "pretty" in CucumberOptions.
@CucumberOptions( plugin = ( "pretty" ) )
❑ Monochrome Mode Reporting: If the monochrome option is set to false, then the console
output is not as readable as it should be. In case the monochrome is not defined in
Cucumber Options, it takes it as false by default.
@CucumberOptions( monochrome = true );
HTML, JSON, XML and JUNIT Reports in Cucumber
❑ HTML Report : To generate custom report in html format, include below plugin in Runner
class,
plugin = {
"pretty", “html:target/html-reports/report.html” }
After execution Cucumber will generate HTML report under target/html-reports/ folder.
❑ JSON Report: To generate custom report in json format include below plugin in Runner
class,
plugin = {
"pretty", "json:target/json-reports/report.json“ }
❑ XML Report: To generate custom report in xml format include below plugin in Runner
class,
plugin = {
"pretty", "junit:target/xml-reports/report.xml“ }
❑ JUNIT Report : In case of using junit, reports can be generated in a separate file depends
on runner and by providing below plugin details in runner class,
plugin = { “pretty”,
“html:target/html-reports/report.html”,
“junit:target/junit-reports/”
}
Extent Report
❑ In the previous video you already learnt how to generate reports in HTML, JSON, XML
and JUNIT Format. However, these reports might not be appropriate sometime to share
with respective stakes holder as demand is complete exhaustive reports with proper
graphs, pie-chart and more granular details.
❑ Extent reports cannot directly integrate with the Cucumber framework. However, in case
user want to generate extent reports for their Cucumber features, user will need to use
some adapter plugin. Additionally, this plugin will allow the Extent report to recognize and
capture scenarios and features present in the framework. It is where the grasshopper
cucumber adapter plugin comes into the picture. The plugin is built on top of the Extent
Report and allows user to quickly generate extent reports for their cucumber framework.
Moreover, this plugin helps in reducing the pain of implementing the reporting in user
framework.
Steps to Generate Extent Report using Cucumber
a. Add grasshopper extent report adapter plugin to the Maven project:
Navigate to the "https://mvnrepository.com/", type "extentreports" in the search box and
select latest "ExtentReports Cucumber7 Adapter". Add the maven dependency in the Maven
project pom.xml file.
b. Add Extent Report Library to the project:
Again, navigate to the "https://mvnrepository.com/", type "extent-pdf-report" in the search box
and select latest "Extent PDF Report". Add the maven dependency in the Maven project
pom.xml file.
c. Create a file named “extent.properties” in same folder where feature files were located. Add
the following lines to the file.
extent.reporter.spark.start=true
extent.reporter.spark.out=target/automation-report.html
d. Add the following lines in user TestRunner.java class
plugin = { "pretty",
"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:" }
After completing all the setup and configuration run TestRunner class and then refresh the
project. A beautiful HTML report will be generated in the target folder.
How to Maintain Backup of All Reports
basefolder.name=target/ExtentReport
basefolder.datetimepattern=dd-MMM-YY HH-mm-ss