Introduction to Selenium WebDriver

Selenium WebDriver is a powerful automation tool used to interact with web applications just like a real user. It allows automation of browsers by simulating actions like clicking buttons, entering text, navigating between pages, handling alerts, and more.

Selenium WebDriver is widely used for web application testing, providing support for multiple browsers like Chrome, Firefox, Edge, Safari, and operating systems like Windows, macOS, Linux.


1. Features of Selenium WebDriver

βœ… Cross-Browser Testing – Supports Chrome, Firefox, Edge, Safari, etc.
βœ… Cross-Platform Testing – Runs on Windows, Linux, macOS
βœ… Supports Multiple Programming Languages – Java, Python, C#, JavaScript, etc.
βœ… Headless Browser Testing – Runs tests in the background without opening the browser
βœ… Integration with Testing Frameworks – Works with TestNG, JUnit, PyTest, etc.
βœ… Handles Web Elements Easily – Can interact with elements using XPath, CSS selectors, etc.
βœ… Supports Parallel Execution – Can run multiple tests simultaneously


2. Architecture of Selenium WebDriver

Selenium WebDriver works on a client-server model and interacts with browsers through browser-specific drivers.

πŸ“Œ Components of Selenium WebDriver Architecture

  1. Selenium Client Library – Provides WebDriver APIs in Java, Python, C#, etc.
  2. JSON Wire Protocol – Transfers data between client libraries and browser drivers
  3. Browser Drivers – ChromeDriver, GeckoDriver, EdgeDriver, SafariDriver
  4. Real Browsers – Chrome, Firefox, Edge, Safari

πŸ“Œ How Selenium WebDriver Works

1️⃣ Test script sends a request to the Selenium Client Library
2️⃣ Request is forwarded to the WebDriver using JSON Wire Protocol
3️⃣ Browser Driver interprets the request and communicates with the real browser
4️⃣ Browser executes the requested action and sends the response back


3. Setting Up Selenium WebDriver

Step 1: Install Java and IDE (Eclipse/IntelliJ)

  • Download and install Java JDK
  • Install an IDE like Eclipse or IntelliJ IDEA

Step 2: Add Selenium WebDriver JAR Files

Download Selenium WebDriver JARs from Selenium Official Website and add them to your project.

Step 3: Download Browser Drivers

Each browser needs a specific driver executable:

Example: Setting ChromeDriver Path in Java

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");

4. Writing First Selenium WebDriver Script

Let's write a basic Selenium WebDriver test in Java that opens a website and prints its title.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FirstSeleniumTest {
    public static void main(String[] args) {
        // Set the path of ChromeDriver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
        
        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();
        
        // Open a website
        driver.get("https://www.google.com");
        
        // Print the title of the page
        System.out.println("Page Title: " + driver.getTitle());
        
        // Close the browser
        driver.quit();
    }
}

πŸ“Œ Explanation:
βœ”οΈ Launches Chrome browser
βœ”οΈ Opens Google website
βœ”οΈ Prints the page title
βœ”οΈ Closes the browser


5. Locating Web Elements in Selenium

To interact with web elements like buttons, text boxes, and links, Selenium provides locator strategies:

Locator Example Usage
ID driver.findElement(By.id("username")) Fastest and most reliable
Name driver.findElement(By.name("password")) Used when ID is not available
Class Name driver.findElement(By.className("login-button")) Selects elements with a specific class
Tag Name driver.findElement(By.tagName("input")) Finds elements by tag
Link Text driver.findElement(By.linkText("Click Here")) Finds links using exact text
Partial Link Text driver.findElement(By.partialLinkText("Click")) Finds links using partial text
CSS Selector driver.findElement(By.cssSelector("#login")) Uses CSS syntax for locating elements
XPath driver.findElement(By.xpath("//input[@name='username']")) Most powerful method for complex locators

6. Performing Actions on Web Elements

Selenium WebDriver allows interacting with elements using methods like:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebElementInteractions {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com");

        // Find elements
        WebElement username = driver.findElement(By.id("username"));
        WebElement loginButton = driver.findElement(By.id("login"));

        // Enter text in the input field
        username.sendKeys("testuser");

        // Click the login button
        loginButton.click();

        // Retrieve text
        String message = driver.findElement(By.id("welcomeMessage")).getText();
        System.out.println("Welcome Message: " + message);

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

βœ”οΈ sendKeys("text") – Enters text into input fields
βœ”οΈ click() – Clicks buttons, links, checkboxes
βœ”οΈ getText() – Retrieves text from an element


7. Handling Pop-ups, Alerts, and Frames

Handling Alerts

driver.switchTo().alert().accept(); // Click OK
driver.switchTo().alert().dismiss(); // Click Cancel
driver.switchTo().alert().sendKeys("text"); // Enter text

Handling Frames

driver.switchTo().frame("frameID");
driver.switchTo().defaultContent(); // Exit frame

Handling Windows

String mainWindow = driver.getWindowHandle();
for (String window : driver.getWindowHandles()) {
    driver.switchTo().window(window);
}
driver.switchTo().window(mainWindow); // Switch back to main window

8. Waits in Selenium WebDriver

Selenium provides implicit and explicit waits to handle dynamic elements.

Implicit Wait

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

Explicit Wait

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login")));

9. Running Tests in Headless Mode

Headless testing allows running tests without opening a visible browser.

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);

10. Best Practices for Selenium WebDriver

βœ… Use Explicit Waits instead of Thread.sleep()
βœ… Use Page Object Model (POM) for better test structure
βœ… Use CSS Selector/XPath wisely for stable locators
βœ… Run tests in headless mode for faster execution
βœ… Use parallel execution to speed up testing


Conclusion

Selenium WebDriver is a powerful tool for automating web applications. It supports multiple browsers, programming languages, and testing frameworks. By following best practices and using explicit waits, POM, and parallel execution, you can make Selenium WebDriver tests efficient and scalable.