Basic Gherkin Feature Model
Feature: User can create an article Scenario: user logs in Given user is on HomePage When user navigates to LoginPage And user enters UserName and Password Then message displayed Login Successful Scenario: user creates a new article Given user is logged in and on HomePage When user clicks CreateArticleLink And user enters ArticleTitle And user enters ArticleBody And user neters ArticleTags And user clicks NewArticleSubmitButton Then message is displayed Article Created Successfully And NewArticlePage title is MyNewArticle
Feature Keyword
- Defines the functionality the test targets
- Multiple scenarios can be created to cover this functionality
Feature: User can create an article
Feature: User can create an article This feature will ensure a user can login and the product handles invalid credentials correctly
Feature: User can create an article This feature will ensure a user can login and the product handles invalid credentials correctly Unit test scenarios will be listed first, then end-to-end scenarios that are less granular, but reuse the unit test scenario methods
Background Keyword
- Background holds test steps that are present in each scenario, such as initialization/setup steps to prepare the test bed for the targeted functionality
- These steps will be ran BEFORE each scenario
Feature: A background runs again for each scenario Background: Given User is on HomePage And HomePage title is “HomePage” When user clicks LoginLink Then LoginPage title is “LoginPage” When user enters LoginUsername “<>” and LoginPassword “<>” and clicks LoginSubmit Then HomePage title is “HomePage”
Scenario Keyword
- Scenario equates to a test case & ideally targets the smallest unit of functionality possible
- End-To-End scenarios might need to also be created & ideally should re-use the steps in atomized scenarios already created.
- Consistent verbiage & syntax of the features will lend itself to formalizing the test step vocabulary and style.
- Well thought out scenario descriptions will use few words and make test data easily identified & will spee the test step creation & subsequent automation – especially considering the method names will closely reflect these phrases.
Scenario: user logs in
Scenario Outline Keyword
- Scenario Outline with the Examples keyword allows data driven testing & is my go-to method, even if only a single set of data will be used.
- Not only does it make the scenario more readable, but inevitably one will need to run multiple data sets through the test to increase coverage.
Feature: Scenario Outline: Given When Then But Examples: | arg1 | arg2 | arg3 | | a | 1 | name1 | | b | 2 | name2 |
Given Keyword
- Given defines preconditions for the scenario, such as
- browser at a Home Page & title is correct
- Elements are present
- Login routines
- Item creation for a 'Delete Item' test
Scenario: user logs in Given user is on HomePage
When Keyword
- When defines a action preformed in a test
- By using And, multiple actions can be enumerated
- Strategic use of And should produce re-useable Test Definitions / methods so the framework is concise and more easily maintained.
Scenario: user logs in Given user is on HomePage When user navigates to LoginPage
Then Keyword
- Then defines the outcome of an action
- Verification resides here
Scenario: user logs in Given user is on HomePage When user navigates to LoginPage And user enters UserName and Password Then message displayed Login Successful
And Keyword
- And defines additional conditions
Scenario: user logs in Given user is on HomePage When user navigates to LoginPage And user enters UserName and Password Then message displayed Login Successful Scenario: user creates a new article Given user is logged in and on HomePage When user clicks CreateArticleLink And user enters ArticleTitle And user enters ArticleBody And user neters ArticleTags And user clicks NewArticleSubmitButton Then message is displayed Article Created Successfully And NewArticlePage title is MyNewArticle
Tip
If there are many And steps OR multiple When/Then steps, this is an indication that
-
more granular scenarios are needed to test those functions
-
this scenario needs less granular steps, with THESE new methods simply 'batching' the methods from the previous scenarios
user_logs_in(){ user_enters_LoginUsername(){} user_enters_LoginPassword(){} user_enters_clicks_LoginButton(){} }
then continue with the granual steps for the When/Then steps
When user clicks AddItem button Then AddItem page is displayed
But Keyword
- helps define negative test case
- use to signify an expected error condition
Feature: User can't login with incorrect credentials Scenario: user logs in Given user is on HomePage When user navigates to LoginPage And user enters UserName and Password But LoginPage credentials are invalid Then message displayed Incorrect Login Credentials
* Keyword
- * can stand in for any test step
- this descreases readability of the scenario, BUT does prevent headaches if keywords are changed later as the Gherkin text is refined.
- As a keyword changes, the associated Test Definition will need to have its annotation updated to match or the definition won't be found
Scenario: user logs in * user is on HomePage * user navigates to LoginPage * user enters UserName and Password * message displayed Login Successful
Basic Model
Feature: Scenario: Given When Then But Scenario: Given When Then But
ScenarioOutline Model for multiple Data Sets
Feature: Scenario Outline: Given When Then But Examples: | arg1 | arg2 | arg3 | | a | 1 | name1 | | b | 2 | name2 |
Reusing Background Logic for Multiple Scenario(Outline) Feature Files
- Background section will be performed BEFORE EACH scenario, as 'setup' or 'initialization' steps
Feature: A background runs again for each scenario Background: Given User is on HomePage And HomePage title is “HomePage” When user clicks LoginLink Then LoginPage title is “LoginPage” When user enters LoginUsername “<>” and LoginPassword “<>” and clicks LoginSubmit Then HomePage title is “HomePage” Scenario Outline: user is able to create an article with title, body, and tags Given User is logged in When user clicks CreateNewArticleLink Then CreateNewArticleLinkPage title is “<>” Scenario Outline: Given User is logged in When Then But Scenario Outline: Given When Then But
Example with Indentation
- Different styles of indentation
Feature: Create a new article Background: Given user is on HomePage And HomePage title is HomePageTitle When user clicks on LoginPageLink Then LoginPage title is LoginPageTitle When user enters LoginUsername "<username>" and LoginPassword "<password>" and clicks LoginSubmitButton Then HomePage title is HomePageTitle Scenario Outline: User can create new article with title, body, and tags Given CreateNewArticleLink is visible When user clicks new article link Then CreateNewArticlePage title is CreateNewArticlePageTitle When user enters title "<title>" and body "<body>" and tags "<tags>" and clicks CreateArticleSaveAndPublishButton Then newly created ArticlePage title is ArticlePageTitle Then user closes browser Examples: | username | password | title | body | tags | | Admin | Admin | NewArticle1 | NewBody1 | _deleteme | | Admin | Admin | NewArticle2 | NewBody2 | _deleteme | | Admin | Admin | NewArticle3 | NewBody3 | _deleteme | | Admin | Admin | NewArticle4 | NewBody4 | _deleteme | Scenario Outline: User can preview new article with title, body, and tags AGAIN Given CreateNewArticleLink is visible When user clicks new article link Then CreateNewArticlePage title is CreateNewArticlePageTitle When user enters title "<title>" and body "<body>" and tags "<tags>" and clicks CreateArticlePreviewButton Then ArticlePreviewPage title is ArticlePreviewPageTitle Then user closes browser Examples: | username | password | title | body | tags | | Admin | Admin | NewArticle1 | NewBody1 | _deleteme | | Admin | Admin | NewArticle2 | NewBody2 | _deleteme | | Admin | Admin | NewArticle3 | NewBody3 | _deleteme | | Admin | Admin | NewArticle4 | NewBody4 | _deleteme |
References
- https://docs.cucumber.io/bdd/
- https://docs.cucumber.io/gherkin/reference/
- http://docs.testplant.com/ePF/using/epf-advanced-gherkin.htm
- http://www.thinkcode.se/blog/2016/06/22/cucumber-antipatterns
- C#: http://specflow.org/
- https://automationpanda.com/2018/02/03/are-gherkin-scenarios-with-multiple-when-then-pairs-okay/
- http://docs.behat.org/en/v2.5/guides/1.gherkin.html
- Log in to post comments
Tags