Scenario
A Scenario represents a single test script with a fixed set of test data written in Gherkin. It consists of a sequence of steps that define a particular behavior of the application.
Each Feature file can contain one or more scenarios. A Scenario starts with the Scenario: keyword and is followed by steps like Given, When, Then, and And.
Example Feature file with Multiple Scenario
Feature: Login Feature
Scenario: Super Admin Login
Given user is on login page
When user logs in with username "superadmin" and password "admin@123"
Then user should see the Super Admin dashboard
Scenario: Manager Login
Given user is on login page
When user logs in with username "manager" and password "manager@123"
Then user should see the Manager dashboard
Scenario: Employee Login
Given user is on login page
When user logs in with username "employee" and password "emp@123"
Then user should see the Employee dashboard
In this example feature file, each login flow is identical except the data. This leads to repeated steps. To avoid this, we use Scenario Outline.
Scenario Outline
A Scenario Outline allows you to reuse the same steps with different sets of data using an Examples table.
Example: Using Scenario Outline
Feature: Login Feature
Scenario Outline: User logs in with different roles
Given user is on login page
When user logs in with username "<username>" and password "<password>"
Then user should see the <role> dashboard
Examples:
| username | password | role |
| superadmin | admin@123 | Super Admin |
| manager | manager@123 | Manager |
| employee | emp@123 | Employee |
Here, Cucumber will create 3 separate scenarios during test execution, each one replacing the and with the values from the table.
Here is a simple example of using Scenario and Scenario outline in Selenium POM
Login Page Class (pages/LoginPage.java)
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
WebDriver driver;
// Locators
private By usernameInput = By.id("username");
private By passwordInput = By.id("password");
private By loginButton = By.id("loginBtn");
private By dashboardText = By.id("dashboardHeader");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void enterUsername(String username) {
driver.findElement(usernameInput).clear();
driver.findElement(usernameInput).sendKeys(username);
}
public void enterPassword(String password) {
driver.findElement(passwordInput).clear();
driver.findElement(passwordInput).sendKeys(password);
}
public void clickLogin() {
driver.findElement(loginButton).click();
}
public boolean isDashboardVisible(String role) {
return driver.findElement(dashboardText).getText().contains(role);
}
}
Step Definition Class (stepDefinitions/LoginSteps.java)
package stepDefinitions;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import pages.LoginPage;
import org.junit.Assert;
public class LoginSteps {
WebDriver driver;
LoginPage loginPage;
@Before
public void setup() {
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Given("user is on login page")
public void user_is_on_login_page() {
driver.get("https://example.com/login");
loginPage = new LoginPage(driver);
}
@When("user enters username {string} and password {string}")
public void user_enters_username_and_password(String username, String password) {
loginPage.enterUsername(username);
loginPage.enterPassword(password);
}
@And("clicks the login button")
public void clicks_the_login_button() {
loginPage.clickLogin();
}
@Then("user should see the {string} dashboard")
public void user_should_see_the_dashboard(String expectedRole) {
Assert.assertTrue("Dashboard not visible or incorrect role!",
loginPage.isDashboardVisible(expectedRole));
}
@After
public void teardown() {
driver.quit();
}
}
Output (Example.log)
Launching browser...
Navigating to login page...
Entering credentials: superadmin / admin@123
Clicking login...
Asserting dashboard contains: Super Admin
...
3 Scenarios (3 passed)
15 Steps (15 passed)