Writing Scenario in Behat using Steps and Keywords
Feature file consist of different Scenarios and each Scenario is combination of several steps.
Steps made up of keywords and words.
Keywords which step uses are Given, When, Then, And, But. Behat does not technically distinguish between these keywords but we should distinguish this to make our statements more meaningful.
Keywords used in Behat Scenario
Given: The purpose of Given steps is to put the system in a known state before user or external system interact with the system. Given can also be used with tables.
When: The purpose of When is to discribe the key action the user perform.
Then: Purpose of Then is to observe outcome. When user perform any action, Then is expectation of that step.
And or But: If you want to avoid When or Then to repeat again in next line or one after another, you can replace it with And or But. For example: Rather than using Then twice you can write one statement with Then another with And or But. Parser will search for step definition starts with Then or When instead of And or But.
Below are 2 approach of writing same scenario one with repetitive keywords another with combination of And and But, both will be interpreted as same but 2nd approach is better.
Scenario: Cookie Management using Mink Given I am on the homepage Then I should see "media frame" Then I should not see "anything else" When I switch to the "media" frame When I press the "Ok" button Then I should see "header-region" Then I should see "footer-region"
Scenario: Cookie Management using Mink Given I am on the homepage Then I should see "media frame" But I should not see "anything else" When I switch to the "media" frame And I press the "Ok" button Then I should see "header-region" And I should see "footer-region"