Create A Sample Step Definition File In Cucumber
Day 63 - Teach Yourself Test Automation
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 a matching step definition to execute.
Add A Step Definition File
Right click on source test Java folder > New > Package
2. Within create a new Java package pop window, enter name as ‘stepDefinitions’ click ‘Finish’
You can see I already created A Step Definition package.
Below is what you too should also end up with.
4. Right click within the Step Definition package. New > Class
5. Name step definition class file ‘stepDefinition’
6. Click ‘finish’
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 a matching step definition to execute.
Map Feature File With Step Definition In Eclipse
Once you create your feature file and written Gherkin Test Cases, if you have not created and mapped each line of your test case to an executable method in the step definition file, it will be highlighted in your features file.
And this will be highlighted for each Test step not yet mapped to executable code to the step definition file.
Recommended by LinkedIn
Create Step Definition File In Eclipse
Execute your feature file to get step definition suggestions in the console
Right click on feature file > Run As > Run Configurations
Select project from the work space.
Apply > Click on Run.
Your step definitions suggestion file will be produced in the eclipse console for the test cases in your feature that do not have step definitions for a particular scenario.
Copy the entire suggested step definitions steps.
Use suggested step definition to define step definition for your feature file
Create a Java Class file – your step definition file.
Copy paste your suggested step definition steps into your class file.
Import all the packages for the keywords Given, When and Then.
Head back to your feature file and you should see all the warning flags gone because feature file test steps now mapped to executable methods/code in step definition file.
Manually Write A Step Definition File
Create a Java class file and name it
What will map your step definition file steps to each line of your test case in your feature file is the Gherkin keywords, Given, When, Then, And.
Also, use the exact naming of your test step on the feature file to title the corresponding keyword step on your step definition file.
So the mapping/matching between test case on feature file and executable code on step definition file is, keyword match first, then exact wording match.
Example of ‘Given’ keyword method in step definition file;
@Given("^User navigates to the OrangeHRM Demo site$")
public void User_navigates_to_the_OrangeHRM_Demo_site()
{
}
Matching Test Step on feature file;
Given User navigates to the OrangeHRM Demo site
The @Given line on the step definition file will map to Given test step in the feature file
if there is more than one instance of @Given then exact word match of test case title and corresponding match to exact words on Given line on the step definition file.
Originally published: Teach Yourself Test Automation