Often times when writing multiple scenarios within a singular feature file you see repeated test steps. Initial test steps that are common across all scenarios and scenario outlines can be pulled out into a Background test step. These steps get executed before every scenario. This can greatly reduce the number of test steps in the feature file, and increase readability.
Points to keep in Mind:
*Keep the Background steps short
*These steps won’t be written out each time the user reads the scenario, so it’s best to have something simple that the user can remember while reading through your feature file
*Keep the Background steps vivid
*Don’t make the feature file too long
Hooks allow us to perform actions at various points in the cucumber test cycle. Before hooks will be run before the first step of each scenario. They will run in the same order of which they are registered. After hooks will be run after the last step of each scenario, even when there are failing, undefined, pending or skipped steps. They will run in the opposite order of which they are registered.
Similar to JUnit @Before and @After tagging a method with either of these will cause the method to run before or after (respectively) each scenario runs. Common functionality like starting or stop browsers are nice to place in these hooks. They reduce the number of common test steps in each scenario.
We can also indicate that @Before and @After and run with scenarios with desired tags
e.x. @Before(‘@web’) for tests needing a browser launched
Cucumber provides a simple method to organize features and scenarios by user determined classifications. This is implemented using the convention that any space delimited string found in a feature file that is prefaced with the commercial at (@) symbol is considered a tag. Any string may be used as a tag and any scenario or entire feature can have multiple tags associated with it. Be aware that tags are heritable within Feature files.
- Scenarios inherit tags from the Feature statement
- Examples inherit tags from the Feature and Scenario statement
Scenario Outline: Successful login Given I want to use the browser [browser] When I set the username to emailAddress And I set the password to testpassword When I login to Facebook Then I am on the Home page @Regression Examples: | browser | | Firefox | | Chrome | | InternetExplorer | @Smoke Examples: | browser | | Firefox |
In the above example, if we run providing the tag @Smoke we would only run the Successful login test for the Firefox browser. If instead we run providing the tag @Regression we would instead run all the three browsers. Providing no tag would run all tests, both Smoke and Regression.