0
0
Selenium Javatesting~15 mins

Why form testing validates user workflows in Selenium Java - Automation Benefits in Action

Choose your learning style9 modes available
Validate user workflow by testing the login form
Preconditions (2)
Step 1: Enter 'testuser' in the username input field with id 'username'
Step 2: Enter 'Test@1234' in the password input field with id 'password'
Step 3: Click the login button with id 'loginBtn'
Step 4: Wait for the dashboard page to load
Step 5: Verify that the URL is 'https://example.com/dashboard'
Step 6: Verify that the welcome message with id 'welcomeMsg' contains text 'Welcome, testuser!'
✅ Expected Result: User is successfully logged in and redirected to the dashboard page with a welcome message displayed
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify URL after login matches expected dashboard URL
Verify welcome message text is correct
Best Practices:
Use explicit waits to wait for elements or page load
Use By.id locators for stable element identification
Use assertions from TestNG or JUnit for validation
Structure code with setup and teardown methods
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 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.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using brittle XPath locators instead of stable IDs
Not verifying the URL after login
Not closing the browser after test
Bonus Challenge

Now add data-driven testing with 3 different sets of valid username and password inputs

Show Hint