0
0
Selenium Javatesting~15 mins

Why advanced skills handle complex scenarios in Selenium Java - Automation Benefits in Action

Choose your learning style9 modes available
Verify user can complete a multi-step form with dynamic content
Preconditions (2)
Step 1: Fill in the first step fields with valid data
Step 2: Click the 'Next' button to go to the second step
Step 3: Wait for dynamic content to load on the second step
Step 4: Fill in the second step fields with valid data
Step 5: Click the 'Next' button to go to the third step
Step 6: Fill in the third step fields with valid data
Step 7: Click the 'Submit' button
Step 8: Wait for confirmation message to appear
✅ Expected Result: The confirmation message 'Form submitted successfully' is displayed
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify each step loads correctly before interacting
Verify dynamic content is present before filling fields
Verify confirmation message text after submission
Best Practices:
Use explicit waits to handle dynamic content loading
Use Page Object Model to organize page interactions
Use clear and maintainable locators (id, name, css selectors)
Avoid Thread.sleep; prefer WebDriverWait
Use assertions from a testing framework like TestNG or JUnit
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.time.Duration;

public class MultiStepFormTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeClass
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.get("https://example.com/multi-step-form");
    }

    @Test
    public void testCompleteMultiStepForm() {
        // Step 1: Fill first step
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("firstName"))).sendKeys("John");
        driver.findElement(By.id("lastName")).sendKeys("Doe");
        driver.findElement(By.id("nextBtn")).click();

        // Step 2: Wait for dynamic content
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicField")));
        driver.findElement(By.id("dynamicField")).sendKeys("Dynamic Data");
        driver.findElement(By.id("nextBtn")).click();

        // Step 3: Fill third step
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email"))).sendKeys("john.doe@example.com");
        driver.findElement(By.id("submitBtn")).click();

        // Verify confirmation message
        WebElement confirmation = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("confirmationMessage")));
        Assert.assertEquals(confirmation.getText(), "Form submitted successfully");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test uses Selenium WebDriver with Java and TestNG for assertions.

We start by opening the multi-step form page in the @BeforeClass setup.

Each step waits explicitly for the required elements to be visible before interacting. This avoids timing issues with dynamic content.

We fill fields by locating them with By.id, which is clear and stable.

We click the 'Next' or 'Submit' buttons to move through the form steps.

After submission, we wait for the confirmation message and assert its text matches the expected success message.

The @AfterClass method closes the browser to clean up.

This approach shows advanced skills by handling dynamic waits, using good locators, and structuring the test clearly.

Common Mistakes - 3 Pitfalls
Using Thread.sleep() instead of explicit waits
Using brittle XPath locators with absolute paths
Not verifying that each step loaded before interacting
Bonus Challenge

Now add data-driven testing with 3 different sets of form input data

Show Hint