Scenario Outline is the Cucumber equivalent of TestNG DataProvider. You write one test scenario and run it with many data sets. This is how you do data-driven testing in BDD.
@shopping @search
Feature: Shopping Portal Search
Background:
Given the user is on the Shopping Portal home page
@smoke
Scenario Outline: Search returns correct results
When the user searches for "<searchTerm>"
Then <resultCount> products should be displayed
And the results should contain "<expectedProduct>"
Examples:
| searchTerm | resultCount | expectedProduct |
| Laptop | 3 | Laptop Pro |
| T-Shirt | 5 | Cotton T-Shirt |
| Coffee | 2 | Coffee Mug |
| xyz123abc | 0 | |You can have multiple Examples tables with labels. This makes your data more organized:
Scenario Outline: Login validation
Given the user is on the Banking Portal login page
When the user enters username "<username>"
And the user enters password "<password>"
And the user clicks the login button
Then the result should be "<outcome>"
And the message should show "<message>"
Examples: Valid Credentials
| username | password | outcome | message |
| testuser | password123 | pass | Welcome testuser |
| admin | admin123 | pass | Welcome admin |
Examples: Invalid Credentials
| username | password | outcome | message |
| invalid | wrong | fail | Invalid credentials |
| | pass123 | fail | Username is required |@Then("the result should be {string}")
public void checkResult(String outcome) {
if (outcome.equals("pass")) {
wait.until(ExpectedConditions.urlContains("/dashboard"));
Assert.assertTrue(
driver.getCurrentUrl().contains("/dashboard"));
} else {
WebElement error = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".error-message")));
Assert.assertTrue(error.isDisplayed());
}
}
@Then("the message should show {string}")
public void checkMessage(String expectedMsg) {
if (expectedMsg.startsWith("Welcome")) {
WebElement welcome = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("welcome-message")));
Assert.assertTrue(
welcome.getText().contains(expectedMsg));
} else {
WebElement error = driver.findElement(
By.cssSelector(".error-message"));
Assert.assertEquals(error.getText(), expectedMsg);
}
}Q: What is Scenario Outline in Cucumber? How is it different from Scenario?
A: Scenario Outline is a template scenario that runs with multiple data sets from an Examples table. Placeholders in angle brackets (<username>) are replaced with values from each row. A regular Scenario runs once with fixed data. Scenario Outline runs once per row in the Examples table. It is the Cucumber equivalent of TestNG DataProvider. I use it for testing login with multiple credentials, search with different terms, and form validation with various inputs. You can also have multiple labeled Examples tables to organize valid and invalid test data.
Key Point: Scenario Outline = one test, many data sets. Use Examples tables with labels for clean organization.