Answer
Testing Multi-Step Form Wizard in Cucumber
Example: 3-Page Registration Wizard
CODE
Page 1: Personal Details (name, email, phone)
Page 2: Account Setup (username, password, security question)
Page 3: Preferences (notifications, theme, language)
ā Submit ā Account Created
Feature File Design
Gherkin
@regression @registration
Feature: Multi-Step Registration Wizard
Background:
Given the user navigates to the registration wizard
@smoke @P1
Scenario: Complete wizard with all valid data (happy path)
When the user fills in personal details:
| firstName | John |
| lastName | Doe |
| email | john@example.com |
| phone | 9876543210 |
And the user clicks Next on page 1
And the user fills in account setup:
| username | johndoe123 |
| password | Test@1234 |
| confirmPassword | Test@1234 |
| securityQuestion | What is your pet? |
| securityAnswer | Fluffy |
And the user clicks Next on page 2
And the user sets preferences:
| emailNotifications | enabled |
| theme | dark |
| language | English |
And the user clicks Submit
Then the account should be created successfully
And the welcome email should be sent to "john@example.com"
@regression @P2
Scenario: Page 1 validation ā missing required fields
When the user clicks Next on page 1 without filling any fields
Then the validation errors should appear:
| First Name is required |
| Email is required |
| Phone is required |
And the user should remain on page 1
@regression @P2
Scenario: Page 1 validation ā invalid email format
When the user fills in personal details:
| firstName | John |
| lastName | Doe |
| email | not-an-email |
| phone | 9876543210 |
And the user clicks Next on page 1
Then the error "Please enter a valid email address" should appear
@regression @P2
Scenario: Password mismatch on page 2
Given the user has completed page 1 successfully
When the user fills in account setup:
| username | johndoe123 |
| password | Test@1234 |
| confirmPassword | Different@99 |
And the user clicks Next on page 2
Then the error "Passwords do not match" should appear
@regression @P3
Scenario: Navigate back from page 2 to page 1 ā data preserved
Given the user has completed page 1 successfully
When the user fills in partial account setup
And the user clicks Back on page 2
Then page 1 should show the previously entered personal details
Page Objects
Java
// WizardPage1.java
public class WizardPage1 extends BasePage {
public void fillDetails(Map<String, String> data) {
waitForClickable(By.id("firstName")).sendKeys(data.get("firstName"));
waitForClickable(By.id("email")).sendKeys(data.get("email"));
waitForClickable(By.id("phone")).sendKeys(data.get("phone"));
}
public void clickNext() { waitForClickable(By.id("next-btn")).click(); }
public List<String> getValidationErrors() {
return driver.findElements(By.css(".field-error"))
.stream().map(WebElement::getText).collect(Collectors.toList());
}
public boolean isOnPage1() {
return waitForVisible(By.css("[data-step='1']")).isDisplayed();
}
}
Step Definitions
Java
public class WizardSteps {
private final ScenarioContext ctx;
@When("the user fills in personal details:")
public void fillPersonalDetails(DataTable data) {
ctx.page1Data = data.asMap(String.class, String.class);
ctx.wizardPage1.fillDetails(ctx.page1Data);
}
@When("the user clicks Next on page {int}")
public void clickNext(int pageNum) {
if (pageNum == 1) ctx.wizardPage1.clickNext();
if (pageNum == 2) ctx.wizardPage2.clickNext();
}
@Then("the account should be created successfully")
public void verifyAccountCreated() {
waitForVisible(By.css(".success-banner"));
ctx.softAssert.assertTrue(
ctx.driver.getCurrentUrl().contains("/account/welcome"),
"Not redirected to welcome page");
}
@Then("the validation errors should appear:")
public void verifyErrors(List<String> expectedErrors) {
List<String> actual = ctx.wizardPage1.getValidationErrors();
for (String expected : expectedErrors) {
ctx.softAssert.assertTrue(
actual.contains(expected),
"Missing error: " + expected);
}
}
}
ScenarioContext Additions
Java
public class ScenarioContext {
public WebDriver driver;
public SoftAssert softAssert = new SoftAssert();
public WizardPage1 wizardPage1;
public WizardPage2 wizardPage2;
public WizardPage3 wizardPage3;
public Map<String, String> page1Data; // preserve between wizard steps
public String registeredEmail;
}
