0
0
Selenium Javatesting~15 mins

Why multi-browser testing ensures reach in Selenium Java - Automation Benefits in Action

Choose your learning style9 modes available
Verify website functionality on multiple browsers
Preconditions (2)
Step 1: Open Chrome browser
Step 2: Navigate to the test website URL
Step 3: Verify the homepage loads successfully
Step 4: Check that the main menu is visible
Step 5: Click on the 'Contact Us' link
Step 6: Verify the Contact Us page loads
Step 7: Close Chrome browser
Step 8: Repeat the above steps for Firefox browser
Step 9: Repeat the above steps for Edge browser
✅ Expected Result: The website loads and functions correctly on Chrome, Firefox, and Edge browsers without errors
Automation Requirements - Selenium WebDriver with TestNG
Assertions Needed:
Homepage title is correct
Main menu element is displayed
Contact Us page title is correct
Best Practices:
Use WebDriverManager to manage browser drivers
Use explicit waits for element visibility
Implement parameterized tests to run on multiple browsers
Use Page Object Model for maintainability
Automated Solution
Selenium Java
import io.github.bonigarcia.wdm.WebDriverManager;
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.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.time.Duration;

public class MultiBrowserTest {
    private WebDriver driver;
    private WebDriverWait wait;
    private final String baseUrl = "https://example.com";

    @Parameters({"browser"})
    @BeforeMethod
    public void setUp(String browser) {
        switch (browser.toLowerCase()) {
            case "chrome" -> {
                WebDriverManager.chromedriver().setup();
                driver = new ChromeDriver();
            }
            case "firefox" -> {
                WebDriverManager.firefoxdriver().setup();
                driver = new FirefoxDriver();
            }
            case "edge" -> {
                WebDriverManager.edgedriver().setup();
                driver = new EdgeDriver();
            }
            default -> throw new IllegalArgumentException("Unsupported browser: " + browser);
        }
        driver.manage().window().maximize();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test
    public void testWebsiteFunctionality() {
        driver.get(baseUrl);

        // Verify homepage title
        String expectedHomeTitle = "Example Domain";
        wait.until(ExpectedConditions.titleIs(expectedHomeTitle));
        Assert.assertEquals(driver.getTitle(), expectedHomeTitle, "Homepage title mismatch");

        // Verify main menu is visible
        By mainMenuLocator = By.id("main-menu");
        WebElement mainMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(mainMenuLocator));
        Assert.assertTrue(mainMenu.isDisplayed(), "Main menu is not visible");

        // Click Contact Us link
        By contactUsLinkLocator = By.linkText("Contact Us");
        WebElement contactUsLink = wait.until(ExpectedConditions.elementToBeClickable(contactUsLinkLocator));
        contactUsLink.click();

        // Verify Contact Us page title
        String expectedContactTitle = "Contact Us - Example Domain";
        wait.until(ExpectedConditions.titleIs(expectedContactTitle));
        Assert.assertEquals(driver.getTitle(), expectedContactTitle, "Contact Us page title mismatch");
    }

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

This test uses TestNG with Selenium WebDriver to automate the manual test case.

Setup: The @Parameters annotation allows running the test on different browsers by passing the browser name. WebDriverManager automatically downloads and sets up the correct driver for Chrome, Firefox, or Edge.

Test steps: The test opens the website URL, waits explicitly for the homepage title to confirm the page loaded, checks the main menu is visible, clicks the 'Contact Us' link, and verifies the Contact Us page title.

Assertions: We assert the page titles and visibility of the main menu to confirm correct loading and navigation.

Teardown: The browser closes after each test to clean up.

This approach ensures the same test runs on multiple browsers, verifying the website works correctly across them, which is why multi-browser testing ensures reach.

Common Mistakes - 4 Pitfalls
Hardcoding browser driver paths instead of using WebDriverManager
Using Thread.sleep() instead of explicit waits
Not parameterizing tests for multiple browsers
Using brittle locators like absolute XPaths
Bonus Challenge

Now add data-driven testing with 3 different URLs to verify multi-browser testing on multiple environments

Show Hint