Behavioral Driven Development (BDD) Framework:
Behavioral-Driven Development is a framework that bridges technical persons and nontechnical stakeholders by facilitating test cases in simple English. It helps stakeholders to keep track of the project by going through the test scenarios.
- Cucumber is the most used Behavioral-Driven Development tool where scenarios are written in simple English, called ’Gherkin’.
- Three files that are important in cucumber are:
- Feature file: A file with .feature extension where test scenarios are written in gherkin language.
- Step Definition file: A file with a .java extension where the technical representation of each step written in a feature file is present.
- Test Runner file: .java extension file, which acts as a bridge between the feature and StepDefinition file. To run the cucumber suite, we need to run this class.
Keywords that are primarily used in feature files are:
- Scenario outline
Below is the snippet for keywords:
Difference between Scenario and Scenario Outline:
Both keywords are used to write scenarios. Still, when we want to parameterize a scenario, i.e., if we want to run a scenario multiple times with different test data, then we use a Scenario outline.
The background is a keyword and kind of scenario that runs before each feature file scenario.
For Example: Suppose there are 4 scenarios in a feature file, and all of them have a common step of logging into the application and navigating to the homepage. Instead of writing that step in all four scenarios, we can remove it from them and keep it as background. Whenever we run a feature file, the background will run before each scenario of the feature file.
Let’s consider a feature of validating the Fashion and Electronics tab functionality in Flipkart. For both functionalities, we need to login and navigate to the homepage.
Step Definition Class:
In the step definition class, we have methods that contain the implementation of each step of scenarios. Step definition is the coding representation of each scenario step.
Note: The highlighted annotations are used in step definition, which is nothing but the keywords of feature files.
In cucumber, we have 2 hooks, i.e. Before and After.
Before: This hook will run before each scenario of the feature file.
After: This hook will run after each scenario of the feature file.
The below screenshot will demonstrate the implementation of these hooks.
Step Definition Class:
Test Runner Class:
Test Runner class acts as a bridge between cucumber steps and step definition methods. It connects both of them. This class does not have a body and contains annotations like RunWith and CucumberOptions, which help the suite run using Junit.
Cucumber Options has the below attributes:
- Features: for this attribute, we must specify the location of feature files inside the project.
Note: Feature files should be kept inside the maven project's src/test/resources folder.
- Glue: The value of this attribute will be the location where the StepDefinition file is kept.
- Monochrome: This attribute is related to console output. Work is more readable than the false value if the value is true.
- Plugin: This attribute is used to specify the cucumber report format and the location where the report will be generated after execution. We can add multiple report formats at a time. In the above screenshot, no HTML and JSON report designs are added.
- Tags: If we have multiple scenarios in different feature files and we want to run specific scenarios as a part of smoke or regression testing, then we will use tags for that selected scenario in feature files and mention those tags as an attribute value in CucumberOptions.
Cucumber is one of the best tools as far as BDD is concerned. With its flow, annotations, and keywords it is helpful for testers to code and maintain test cases. It is the best tool for stakeholders like business analysts to understand and track developments.