Cucumber Framework tool supports Behavior Driven Development (BDD) and is used to write tests for web applications with multiple test data with minimum intervention in code. In Cucumber the executable feature files use Gherkin language, which makes the project flow easy to understand to interpret and execute the test scripts.
Cucumber allows automation of functional scenario and scenario outline validation that is easily read and understood. The cucumber framework allows easier reuse of code in the tests with a time-saving mechanism in automation testing. The cucumber framework works faster and smarter with Selenium to speed up the execution of the test script.
Given, ‘When’ and ‘Then’ are the main keywords to be used in the feature file, without the example keyword, Scenario outline functionality is not able to run in the runner class. Cucumber uses reporter plugins to generate reports for scenarios that are passed or failed and display them within pie charts.
Test scenarios and Test Scenario Outline are written in the file, named as Feature file. It should be saved as login.feature
Tests code are written by focusing on user stories and project flow
The step definition file contains the actual code to execute the Test Scenario in the Features file, which used to be written differently in the step definitions file i.e., Java, Python
When clicking each feature line, it is navigated to the proper Step definition
Advantages of Cucumber
Cucumber supports languages like Java, Python, Ruby, and .net
Cucumber uses Gherkin language which is readable and can promote teamwork
The major advantage of cucumber is the reusability of the steps
Easy to develop and maintain with code reusability
Test script can be written with the knowledge of functional flow on the project
Steps to create your own first Cucumber BDD with Java
1. Feature File
A Feature File is an entry point to the Cucumber tests. This is a file where you will describe your tests in Gherkin language and use it as a live document at the time of testing. The extension of the feature file is ‘example.feature’. Features file contains high-level description of the Test Scenario in simple language.
Cucumber Feature File consists of the following components:
Feature: A feature would describe the current test script which is to be executed
Scenario: Scenario describes the steps and expected outcome for a particular test case
Scenario Outline: Same scenario can be executed for multiple sets of data using a scenario outline. The data is provided in a tabular structure separated by (I I)
Given: Steps are used to describe the initial context of the system
When: Specifies the test action that is to be performed
Then: The expected outcome of the test can be represented by ‘Then’
Sample Feature File 1:
Feature: Login Functionality Feature
@tagname1
Scenario: Login Functionality
Given user navigates to SOFTWARETETINGHELP.COM when user logs in using Username as ‘USER’ and Password ‘PASSWORD’ Then login should be successful
Sample Feature File 2 :
Feature: Login Functionality Feature
@tagname2
Scenario Outline: Login Functionality
Given user launches the webpage URL
When user logs in using Username as <username> and Password <password> Then login should be successful
A step definition file in Cucumber holds the test method and code which are mapped to the Gherkin test case steps on the feature file. In the feature file each test case step once executed will have a matching step definition to execute. The extension of step Definition is mentioned as ‘samplestepdefinition.java’
Sample Step Definition:
@When("user clicks {string} button")
public void user_clicks_button(String string) { driver.findElement(By.xpath("//button[@type='submit']")).click();
}
3. Runner File
To start running, Cucumber needs Test Runner Class. This class uses JUnit Annotation @RunWith(). JUnit starts executing your test. In the src folder create a class called TestRunner. To run the specific feature file cucumber uses standard JUnit Runner and specifies tags in @Cucumber. Options. Multiple tags can be given by using comma separate. Here you can specify the path of the report and the type of report you want to generate.
@RunWith(Cucumber.class)
@CucumberOptions(
Format ={ “pretty”,”json:target/output.json”, “html:target/html/”},
Features={“src/functional-test/resources”},
Tags={“@sample1”,”@sample2”}
)
Public class CucumberRunnerTest {
}
Cucumber reports
Cucumber uses reporter plugins to produce reports that contain information about what scenarios have passed or failed. Cucumber has the capability to generate reports in the form of HTML, XML, JSON & TXT.
For HTML reports, one needs to add html: target/cucumber-reports to the @CucumberOptions plugin option in runner class.
This will generate an HTML report at the target location as index.html
Conclusion
The Cucumber framework allows functional validation in an easy and understandable format in Gherkin language, that helps technical and non-technical team members to grab easily. Cucumber framework as greater flexibility code. It also supports report generation for each scenario.
This website uses cookies to understand your preferences, improve your experience, and gather analytics, in line with GDPR. Learn more or adjust your preferences in our Privacy Policy.
Your daily dose of the Tech world
Don't miss out on the latest tech feeds from the best Digital, Innovation & Software Practitioners across the globe.