Scenario

Imagine a scenario as a small part of the story or feature you're telling about the software. It's like a little scene explaining what should happen in a specific situation.


Each scenario describes one situation or test case within the bigger feature. For example, in a login feature, one scenario might describe a successful login, while another might describe what happens when someone enters the wrong password.


Scenarios are written in simple language using keywords like Given, When, Then, and others. These keywords help explain the steps or actions that need to happen in that situation.


In the following example, the scenario is named "Successful login." It tells the story of a user logging in successfully by being on the login page, entering the right credentials, and then being logged in.

SuccessfulLogin.feature
Copy
Scenario: Successful login
  Given the user is on the login page
  When the user enters valid credentials
  Then the user should be logged in successfully

Scenarios act like a checklist for testing. Each one describes a specific situation or behavior the software should follow. It helps developers write the code, and testers use these scenarios to check if the software behaves as expected.

Keywords

Keywords plays an important role to differentiate between the pre-conditions, action and expected results. Let's understand each of these keywords in detail.


Given

Think of Given as the part where you're setting up the stage for a particular scenario. It's like describing the initial conditions or the starting point of your story about the software.

The Given step is used to describe the preconditions or the context needed for the scenario to happen. It's where you say what needs to exist or be true before the main action occurs.


In the above example, Given the user is on the login page sets the scene by stating where the user needs to be before attempting to log in. It's like saying, "Before the login happens, make sure the user is on the login page."

If you have more than one pre-conditions or Given conditions, you can use And instead of multiple Given statement.


SuccessfulLogin.feature
Copy
Scenario: Successful login
  Given the user is on the login page
  And all cookies are cleared
  When the user enters valid credentials
  Then the user should be logged in successfully


In the above example, We have two pre condition and hence we have used And keyword to combine these condition. Also this will give end user a clear meaning about the scenario.

When

Think of When as the moment when something happens in your story about the software. It's like the action or event that's being performed or triggered.


The When step describes the specific action that the user or the system performs. It's where you explain what happens or what the user does in the software.


In the previous example's scenario, When the user enters valid credentials describes the action the user takes. It's the moment when the user inputs the correct login details.


The When step represents a crucial moment in the scenario. It's what leads to the result or outcome being tested.


For developers, the When step guides the code implementation, ensuring that the actions described are accurately performed in the software.

Then

Think of Then as the part where you check what happens as a result of the action described in the When step. It's like the expected outcome or the verification step in your story about the software.


The Then step specifies the expected result or the outcome that you're checking for after the action takes place. It's where you describe what the software should do or how it should behave.


The Then step is written in straightforward language, detailing what should happen after the action. It's about the expected result or behavior.


In the previous example scenario, Then the user should be logged in successfully states the expected outcome. It's verifying that after entering the correct credentials, the expected behavior is successful login.


The Then step represents the check or verification point. It ensures that the software behaves as expected after the action (When) is performed.


For developers, the Then step guides the implementation of verification logic, confirming that the software performs as specified.


If you have multiple expected outcomes, you can use And keyword and But keyword. And and But are used to add more steps to a scenario and are extensions of the previous step either continuing the same type of step or presenting an exception or alternative.


The And keyword is used to continue a step of the same type without repeating the keyword. It's like saying "Also do this" or "Continue with the same kind of step." In general, it is expecting to have a positive outcome or validation when used with Then.


The But keyword is used similarly to And but indicates an exception or contrast to the preceding step. It's like saying "This is different from what we just said" or "Here's an alternative perspective." In general, It is expecting to have a negative outcome or validation when used with Then.

SuccessfulLogin.feature
Copy
Scenario: Successful login
  Given the user is on the login page
  When the user enters valid credentials
  Then the user should be logged in successfully
  And the user should see the dashboard
  But the user should not see the login link


In the above scenario, Then user should be logged in successfully extends the expected outcome to include another positive validation And the user should see the dashboard followed by a negative outcome But the user should not see the login link represent an exception or a contrasting expectation after the successful login.