Cucumber is a testing framework which helps in performing acceptance testing, where test scripts are written in a BDD approach. A feature file is created and written in plain english and the corresponding testscripts are driven using selenium.
- The annotations that are commonly used are : Given, And,When,Then, But, Before and After
- A feature file should contain the feature description that tells about the features the test scripts would cover, test scenarios and may contain a background or scenario outline as well.
Maven Dependencies:
<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-core</artifactId> <version>1.1.5</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.1.5</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.1.5</version> </dependency>
- Verify that the Junit version is 4.10 or above.
Feature File:
Feature: A feature would describe the current testscript that is to be executed
Background: Background describes the steps that would be executed before each scenario
Scenario: Scenario would describe the steps and expected outcome for a particular test case.
Scenario Outline: Using Scenario Outline, we can execute the same scenarios for multiple sets of data. The data is provided in a tabular structure(separated by | |) under Examples. The step definitions can be parameterized by providing the column header in angular brackets(< >). Double quotes for each parameter needs to be put in the table and not in the step definition.
Given: Given specifies the context of the text to be executed. Given step can also be parameterized using datatables.
When: When specifies the test action to be performed
Then: Then specifies the expected outcome of the test
- An example of a feature file is provided in the below screenshot
Image may be NSFW.
Clik here to view.
- Parameters can be passed in each stepdefinition within double quotes(“”)
- Tags can be provided for different scenarios, features or backgrounds. The tags would help in determining the specific scripts that would be executed.
- Once the feature file is written, we need to create a test class which would run the feature file definitions. It would look similar as below:
Image may be NSFW.
Clik here to view.
- We need to add the path of the feature file in the cucmber options and then execute the test file
- Since we have not yet prepared test script for each definition. On execution, methods would be created using the step definition phrase and would appear in the console, suggesting that we need to implement these methods in order for the test scripts to be executed successfully. Note that the test methods would be having an underscore format.
Image may be NSFW.
Clik here to view.
- Copy the console output and put in a class file. Fill in the funtion body with the desired scripts .
- One can add the Before and After annotations as well, which would be executed before or after each scenario respectively.
- The step definitions are transformed using regular expressions. We could use further regular expressions like .* in step definition phrase to increase the reusability of functions across scenarios.
Image may be NSFW.
Clik here to view.
- Once the script for step definitions is written, execute the runner file and the test scripts would be executed successfully.
Written By: - Jaya Mohanty, QA Engineer, Mindfire Solutions
Image may be NSFW.
Clik here to view.
Clik here to view.
