Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests
Cross-browser testing ensures that your web application works consistently across different web browsers. Selenium WebDriver is a popular tool for automating web application testing, and it supports multiple browsers like Chrome, Firefox, Internet Explorer, Safari, and Edge. This guide will describe how to write scripts for cross-browser testing using Selenium WebDriver in Java, including examples.

Setup

  1. Download WebDriver Binaries: Ensure you have the WebDriver binaries for the browsers you want to test.

2. Include Selenium in Your Project: If you’re using Maven, add the following dependencies to your ‘pom.xml‘:

				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.0</version>
    </dependency>
</dependencies>

				
			

Writing the Script

  1. Setup WebDriver for Different Browsers:
				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class CrossBrowserTesting {

    public static void main(String[] args) {
        // Define the browser type
        String browser = "chrome"; // This can be parameterized or read from a config file

        // Create WebDriver instance based on the browser type
        WebDriver driver = null;

        switch (browser.toLowerCase()) {
            case "chrome":
                WebDriverManager.chromedriver().setup();
                driver = new ChromeDriver();
                break;
            case "firefox":
                WebDriverManager.firefoxdriver().setup();
                driver = new FirefoxDriver();
                break;
            case "ie":
                WebDriverManager.iedriver().setup();
                driver = new InternetExplorerDriver();
                break;
            case "edge":
                WebDriverManager.edgedriver().setup();
                driver = new EdgeDriver();
                break;
            default:
                System.out.println("Browser not supported");
                return;
        }

        // Run the test
        runTest(driver);

        // Close the browser
        if (driver != null) {
            driver.quit();
        }
    }

    public static void runTest(WebDriver driver) {
        driver.get("https://www.example.com");
        System.out.println("Title: " + driver.getTitle());
        // Add more test steps as needed
    }
}

				
			

2. Parameterizing Browser Type: You can pass the browser type as a parameter to make the script more flexible.

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class CrossBrowserTesting {

    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Please specify the browser type as a parameter.");
            return;
        }
        
        String browser = args[0]; // Read the browser type from the command-line arguments

        // Create WebDriver instance based on the browser type
        WebDriver driver = getWebDriver(browser);

        if (driver == null) {
            System.out.println("Unsupported browser!");
            return;
        }

        // Run the test
        runTest(driver);

        // Close the browser
        driver.quit();
    }

    public static WebDriver getWebDriver(String browser) {
        WebDriver driver = null;

        switch (browser.toLowerCase()) {
            case "chrome":
                WebDriverManager.chromedriver().setup();
                driver = new ChromeDriver();
                break;
            case "firefox":
                WebDriverManager.firefoxdriver().setup();
                driver = new FirefoxDriver();
                break;
            case "ie":
                WebDriverManager.iedriver().setup();
                driver = new InternetExplorerDriver();
                break;
            case "edge":
                WebDriverManager.edgedriver().setup();
                driver = new EdgeDriver();
                break;
        }

        return driver;
    }

    public static void runTest(WebDriver driver) {
        driver.get("https://www.example.com");
        System.out.println("Title: " + driver.getTitle());
        // Add more test steps as needed
    }
}

				
			

Example Test Cases

  1. Add your test cases within the ‘runTest‘ method. Here’s an example:
				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public static void runTest(WebDriver driver) {
    driver.get("https://www.example.com");
    
    // Print the title of the page
    System.out.println("Title: " + driver.getTitle());
    
    // Example: Find a specific element and interact with it
    WebElement exampleElement = driver.findElement(By.id("exampleId"));
    exampleElement.click();

    // Validate the result
    WebElement resultElement = driver.findElement(By.id("resultId"));
    if (resultElement.getText().equals("Expected Text")) {
        System.out.println("Test Passed!");
    } else {
        System.out.println("Test Failed!");
    }
}

				
			

Running the Script

You can run your script from the command line or an IDE:

				
					java -cp path/to/your/project.jar CrossBrowserTesting chrome
java -cp path/to/your/project.jar CrossBrowserTesting firefox
java -cp path/to/your/project.jar CrossBrowserTesting edge
java -cp path/to/your/project.jar CrossBrowserTesting ie

				
			

Cross-browser testing using Selenium WebDriver in Java involves setting up WebDriver instances for different browsers, parameterizing the browser type, and writing flexible test scripts that can run across multiple browsers. By following the examples provided, you can ensure your web application behaves consistently across different browser environments.

Scroll to Top