Why form testing validates user workflows 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 LoginFormTest { private WebDriver driver; private WebDriverWait wait; @BeforeClass public void setUp() { // Set path to chromedriver executable if needed driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.manage().window().maximize(); } @Test public void testLoginWorkflow() { driver.get("https://example.com/login"); WebElement usernameInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); usernameInput.sendKeys("testuser"); WebElement passwordInput = driver.findElement(By.id("password")); passwordInput.sendKeys("Test@1234"); WebElement loginButton = driver.findElement(By.id("loginBtn")); loginButton.click(); // Wait for dashboard URL wait.until(ExpectedConditions.urlToBe("https://example.com/dashboard")); String currentUrl = driver.getCurrentUrl(); Assert.assertEquals(currentUrl, "https://example.com/dashboard", "URL after login should be dashboard URL"); WebElement welcomeMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("welcomeMsg"))); String welcomeText = welcomeMsg.getText(); Assert.assertTrue(welcomeText.contains("Welcome, testuser!"), "Welcome message should greet the user"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
This test script uses Selenium WebDriver with Java and TestNG framework.
Setup: We create a ChromeDriver instance and a WebDriverWait for explicit waits. The browser window is maximized for better visibility.
Test steps: The test navigates to the login page URL. It waits until the username input is visible, then enters the username. It finds the password input and enters the password. Then it clicks the login button.
After clicking login, it waits explicitly until the URL changes to the dashboard URL. This confirms the page navigation happened.
It asserts the current URL matches the expected dashboard URL.
Then it waits for the welcome message element to appear and asserts that the text contains the expected greeting.
Teardown: The browser is closed after the test to clean up.
This structure ensures the test follows the user workflow precisely and validates the form's effect on navigation and UI changes.
Now add data-driven testing with 3 different sets of valid username and password inputs