Why advanced skills handle complex scenarios in Selenium Java - Automation Benefits in Action
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.
Now add data-driven testing with 3 different sets of form input data