What Is Selenium WebDriver?

The market for web applications is vast and highly competitive. Delivering a high-quality product at a reasonable cost often depends on using efficient test-automation tools, preferably those that are open source. Among the many browser-automation solutions, the open-source Selenium suite quickly gained traction. Selenium consists of several parts, and Selenium WebDriver is the most recent addition.

This article examines Selenium WebDriver, covering its architecture and how to write test scripts with it.

What is Selenium?

First, let’s define Selenium. It is a framework for automating browsers, meaning it can drive a browser to perform actions defined in a test scenario. While it is ideal for web-application testing, its use is not limited to that alone.

Selenium actually comprises four tools: Selenium IDE, Selenium WebDriver, Selenium Grid, and Selenium Standalone Server. In this piece we concentrate on Selenium WebDriver – also called Selenium 2.0 or simply WebDriver – a widely adopted and powerful automation engine for web apps.

What is Selenium WebDriver?

Selenium WebDriver is a free, open-source, cross-platform testing framework for web applications. It offers a unified application programming interface (API) that enables browser automation.

Essentially, WebDriver is a library that your code calls; the library then translates your commands into actions performed in the browser of your choice.

WebDriver runs on Windows, Linux and macOS. Test scripts can be written in any language supported by Selenium, such as Java, C#, Ruby, Python, or JavaScript (often referred to as bindings).

How Does Selenium WebDriver Work?

An analogy can help clarify WebDriver’s operation. Imagine a taxi service with three participants:

The passenger, who tells the driver where to go. The driver, who manipulates the steering wheel, pedals, etc., based on the passenger’s directions. The car, which carries out the driver’s inputs.

In testing, the engineer (passenger) writes code that uses the WebDriver API (driver) to issue commands. WebDriver then forwards those commands to the browser (car), which executes them and returns results.

Selenium WebDriver Architecture

The architecture consists of a language-specific client, a Selenium Standalone Server, and a browser-specific driver. Users typically do not see these layers.

  1. The tester writes test code in an IDE using the client library for a chosen language (Java, C#, Python, Ruby, etc.).
  2. The test code communicates with the Selenium Standalone Server.
  3. The server passes the commands to the appropriate browser driver – e.g., ChromeDriver for Google Chrome or GeckoDriver for Firefox.
  4. The driver launches the browser, carries out the actions, and sends back responses.

Writing Scripts with Selenium WebDriver

When you create UI tests, you rely on the WebDriver library. Below are some of the most common commands you will use:

Browser commands: get, getTitle, getCurrentUrl Navigation commands: back, forward, to, refresh Element commands: clear, click, getText, sendKeys Element-search commands: findElement() with a locator or query object

These commands are sufficient to automate many typical web-application scenarios. For illustration, consider a login page with a username field (class="username"), a password field (class="pass"), and a login button (id="login"). The following test verifies that a successful login leads to a page titled “Success”.

@Test
public void checkLoggedInPageTitle() {
    WebDriver driver = new FirefoxDriver();
    driver.get("[http://somepage.com/login"](http://somepage.com/login"));
    driver.findElement(By.className("username")).sendKeys("blaze");
    driver.findElement(By.className("pass")).sendKeys("meter");
    driver.findElement(By.id("login")).click();
    String loggedInPageTitle = driver.getTitle();
    Assert.assertEquals("Success", loggedInPageTitle);
    driver.close();
}

Explanation: driver.get opens the login URL. findElement with className locates the username and password fields and fills them. click() presses the login button. getTitle() retrieves the title of the resulting page. Assert.assertEquals checks that the title matches the expected value.

After execution, Selenium generates a test report, typically an HTML file, that details the outcome.

Selenium WebDriver Pros and Cons

Advantages

Free and open source. Language bindings for Java, C#, Ruby, Python, Groovy, Node.js, and others. Works on all major operating systems. Easy integration with build tools such as Gradle and Maven. Compatible with continuous-integration servers like Jenkins. Backed by a large, active community for support.

Disadvantages

Dynamic elements such as CAPTCHAs cannot be reliably automated. Test writers must be proficient in a supported programming language. Limited to web-application testing; it does not handle native mobile or desktop apps. No built-in reporting or file-upload reporting features.

  • Large test suites may run slowly unless optimized in code.

To simplify script creation, you can use a free Chrome-extension recorder that captures actions and generates corresponding WebDriver code.