In the Cucumber framework, the keywords Given, When, Then, And, and But are used to define the steps in a test scenario written in the Gherkin language. Here’s what each keyword represents:
Given
The Given keyword is used to describe the initial context or preconditions of a scenario. It sets the stage for the test by providing the necessary setup or state for the system under test. Given steps typically involve actions like creating objects, setting up data, or configuring the system.
Example:
Given the user is on the login page
When
The When keyword is used to describe an action or event that triggers the scenario. It specifies the behavior that should be exercised or the functionality that should be tested. When steps typically involve user interactions, such as clicking a button or entering data.
Example:
When the user enters a valid username and password
Then
The Then keyword is used to describe the expected outcome or result of the scenario. It specifies the expected behavior or state of the system after the action described in the When step is performed. Then steps typically involve assertions or verifications to check if the system behaves as expected.
Example:
Then the user should be redirected to the dashboard
And, But
The And and But keywords are used to add additional conditions or actions to the scenario. They are used to make the scenario more readable and expressive. And is used to add positive conditions or actions, while But is used to add negative conditions or actions.
Example:
Given the user is logged in
And the user has an active subscription
When the user clicks the "Cancel Subscription" button
Then the subscription should be canceled
But the user should still have access until the end of the billing cycle
By using these keywords, Cucumber scenarios can be written in a structured and readable format that describes the behavior of the system under test. The Given, When, Then structure helps to organize the scenario into a logical flow and makes it easier for both technical and non-technical stakeholders to understand the expected behavior of the system.