Introduction to Selenium

Selenium is a popular open-source framework for automating browser actions, helping testers and developers validate web applications quickly and reliably.


  • Selenium automates web browsers to perform actions like clicking buttons, entering text, selecting options, navigating pages, and validating results.

  • It is mainly used for web application testing, but can also help with repetitive browser tasks and lightweight web scraping.

What is Selenium WebDriver?

Selenium WebDriver is the Selenium component that communicates directly with browsers and automates user actions by sending commands to browser drivers such as ChromeDriver, GeckoDriver, and EdgeDriver.

Unlike the older Selenium RC (Remote Control), WebDriver does not rely on JavaScript injection. It speaks directly to the browser, which makes it faster and more reliable.

Definition of Selenium

Selenium is an open-source suite of tools used for automating web browsers and testing web applications across different browsers and operating systems.

Why Selenium is Used

  • Automates repetitive testing tasks
  • Saves time and effort
  • Supports multiple browsers
  • Supports multiple programming languages
  • Supports multiple operating systems
  • Integrates with testing frameworks
  • Supports parallel execution
  • Supports CI/CD pipelines

Real-Life Example

Without Selenium
  • Open browser manually
  • Enter username
  • Enter password
  • Click login
  • Verify output
  • Repeat 100 times
With Selenium

A script automatically performs all actions in seconds, including opening the browser, entering test data, clicking buttons, and verifying results.

Features of Selenium

1. Open Source

Selenium is free and does not require purchasing licenses.

2. Cross Browser Support

Supports Google Chrome, Mozilla Firefox, Microsoft Edge, and Safari.

3. Cross Platform Support

Works on Windows, Linux, and macOS.

4. Multiple Programming Language Support

Supports Java, Python, C#, JavaScript, Ruby, and Kotlin.

5. Framework Integration

Integrates with TestNG, JUnit, PyTest, Maven, Jenkins, and Cucumber.

6. Parallel Execution

Supports running multiple test cases simultaneously.

Components of Selenium Suite

1. Selenium IDE

Selenium IDE is a browser extension that records and plays back test cases.

Features:

  • No coding required
  • Record and playback functionality
  • Useful for beginners

Advantages:

  • Easy to learn
  • Quick script creation

Disadvantages:

  • Limited customization
  • Not suitable for complex projects
2. Selenium WebDriver

WebDriver directly controls the browser and automates user actions.

Features:

  • Faster execution
  • Supports dynamic web applications
  • Supports multiple browsers
Example Copy Code
from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://www.google.com")

print(driver.title)

driver.quit()

Example Copy Code
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumExample {

    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();

        driver.get("https://www.google.com");

        System.out.println(driver.getTitle());

        driver.quit();
    }
}  
3. Selenium Grid

Selenium Grid allows tests to run on multiple machines and browsers at the same time.

Purpose:

  • Parallel testing
  • Distributed execution
  • Cross-browser testing

Example: Run Chrome on Windows, Firefox on Linux, and Edge on macOS simultaneously.

4. Selenium RC (Remote Control)

Selenium RC was the older Selenium tool that required a server startup and used JavaScript injection.

Limitations:

  • Slow execution
  • Browser restrictions

Selenium RC is now deprecated and replaced by WebDriver.

Selenium Architecture

Selenium WebDriver follows this flow:

Test Script
     ↓
WebDriver API
     ↓
Browser Driver
     ↓
Browser

Explanation:

  • Test Script: Automation code written in Java, Python, or another supported language.
  • WebDriver API: Receives commands from the script.
  • Browser Driver: Converts commands into browser-specific actions (ChromeDriver, GeckoDriver, EdgeDriver).
  • Browser: Performs the actual actions.

Advantages of Selenium

Free and Open Source

No licensing cost.

Supports Multiple Browsers

One script can run on different browsers.

Language Flexibility

Choose the programming language you prefer.

Easy Integration

Works with Jenkins, Docker, Maven, TestNG, and more.

Parallel Testing

Reduces execution time by running tests simultaneously.

Community Support

Large online community and extensive documentation.

Disadvantages of Selenium

  • Supports only web applications, not desktop apps directly.
  • No built-in reporting; external tools are required.
  • No built-in object repository; custom implementation is needed.
  • No official technical support; relies on community support.

Applications of Selenium

Functional Testing

Verifies application functionality such as login and registration forms.

Regression Testing

Checks that existing functionality still works after updates.

Cross Browser Testing

Tests applications on different browsers to ensure compatibility.

Data Driven Testing

Runs the same script with multiple datasets.

Continuous Integration

Integrates Selenium tests into CI/CD pipelines.

Companies Using Selenium

Many organizations use Selenium, including:

  • Google
  • Microsoft
  • Amazon
  • Netflix
  • Facebook
  • IBM

Interview Questions

What is Selenium?

Selenium is an open-source framework used for automating web browsers.

What are Selenium components?

Selenium components include Selenium IDE, Selenium WebDriver, Selenium Grid, and Selenium RC.

Why is WebDriver preferred over Selenium RC?

WebDriver communicates directly with browsers, making it faster and more efficient than Selenium RC.

Does Selenium support mobile application testing?

Not directly. Mobile testing usually uses Appium with Selenium concepts.

Can Selenium automate desktop applications?

No, Selenium mainly automates web applications.


Next: Environment Setup