0
0
Selenium Javatesting~15 mins

Why browser control drives test flow in Selenium Java - Automation Benefits in Action

Choose your learning style9 modes available
Verify login flow controls test execution by browser state
Preconditions (2)
Step 1: Open browser and navigate to login page URL
Step 2: Enter 'user@example.com' in the email input field
Step 3: Enter 'Password123' in the password input field
Step 4: Click the login button
Step 5: Wait for the dashboard page to load
Step 6: Verify the URL is the dashboard URL
Step 7: Verify the welcome message is displayed
✅ Expected Result: Test should proceed step-by-step controlled by browser state. After login button click, test waits for dashboard page load before verifying URL and welcome message. If page does not load, test should fail.
Automation Requirements - Selenium WebDriver with Java and TestNG
Assertions Needed:
Verify current URL matches expected dashboard URL
Verify welcome message element is visible and contains expected text
Best Practices:
Use explicit waits to wait for page elements or URL changes
Use By locators with IDs or stable attributes
Structure test steps sequentially to reflect browser state changes
Use TestNG assertions for validation
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 LoginFlowTest {
    WebDriver driver;
    WebDriverWait wait;
    String loginUrl = "https://example.com/login";
    String dashboardUrl = "https://example.com/dashboard";

    @BeforeClass
    public void setup() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test
    public void testLoginFlowControlsTestExecution() {
        // Step 1: Open login page
        driver.get(loginUrl);

        // Step 2: Enter email
        WebElement emailInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
        emailInput.sendKeys("user@example.com");

        // Step 3: Enter password
        WebElement passwordInput = driver.findElement(By.id("password"));
        passwordInput.sendKeys("Password123");

        // Step 4: Click login button
        WebElement loginButton = driver.findElement(By.id("loginBtn"));
        loginButton.click();

        // Step 5: Wait for dashboard page to load by URL
        wait.until(ExpectedConditions.urlToBe(dashboardUrl));

        // Step 6: Verify URL
        String currentUrl = driver.getCurrentUrl();
        Assert.assertEquals(currentUrl, dashboardUrl, "URL after login should be dashboard URL");

        // Step 7: Verify welcome message
        WebElement welcomeMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("welcomeMessage")));
        Assert.assertTrue(welcomeMsg.getText().contains("Welcome"), "Welcome message should be displayed");
    }

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

This test script uses Selenium WebDriver with Java and TestNG to automate the login flow.

We start by opening the login page URL. Then we locate the email and password fields by their IDs and enter the test credentials.

After clicking the login button, the test uses an explicit wait to pause execution until the browser URL changes to the dashboard URL. This shows how controlling the browser state drives the test flow step-by-step.

Once the dashboard page loads, the test asserts the URL is correct and that a welcome message is visible with expected text.

Explicit waits ensure the test does not proceed until the browser is ready, preventing flaky failures.

The @BeforeClass and @AfterClass methods setup and close the browser cleanly.

Common Mistakes - 3 Pitfalls
Using Thread.sleep() instead of explicit waits
Using brittle XPath locators that break easily
Not waiting for page load before asserting URL or elements
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid).

Show Hint