Introduction to Selenium WebDriver

After we have finished this lesson, we

  • Know what Selenium WebDriver is.
  • Can identify the key components of our end-to-end tests.
  • Can get the required dependencies with Maven and Gradle.

What Is Selenium WebDriver?

Selenium WebDriver is an API that allows us to write automated tests for web applications. The automated tests that use Selenium WebDriver are run by using a web browser. In other words, Selenium WebDriver helps us to verify that our application is working as expected when it is used by a real user.

The key features of Selenium WebDriver are:

  • It supports many common programming languages such as C#, Java, JavaScript, Python, and so on.
  • It supports all common web browsers.
  • It supports headless browsers such as HtmlUnit and PhantomJS.

Next, we will take a closer look at the key components of our end-to-end tests.

The Key Components of Our End-to-End Tests

Selenium WebDriver is a quite complex beast, and this lesson doesn’t provide a comprehensive description of its architecture because it doesn’t belong to scope of this lesson.

However, before we write end-to-end tests that use Selenium WebDriver, we should recognize the key components of our end-to-end tests and understand how these components interact with each other.

The key components of our end-to-end tests are:

  • The WebDriver interface declares the methods that we use when we write our end-to-end tests. These methods help us to control the used web browser and select elements from the loaded HTML page. Naturally, our test class must use the browser specific implementation of this interface.
  • The test class controls the used web browser, selects elements from the loaded HTML page, and writes assertions for the data found from these elements.
  • The driver executable is a browser specific component that implements either the older Selenium WebDriver’s JSON wire protocol or the newer W3C WebDriver specification. It acts as a proxy between Selenium WebDriver and the used web browser.
  • The web browser provides native support for automated tests. It provides an API that allows us to remotely control the user interface of the tested web application. The driver executable uses this API when it forwards the requests send by Selenium WebDriver.

The following figure illustrates the relationship between these components:

Let’s move on and find out how we can get the required dependencies with Maven and Gradle.

Getting the Required Dependencies

We can get the required dependencies by declaring the selenium-java dependency in our build script.

If we are using Maven, we have to add the following snippet to our pom.xml file:

<dependency>
	<groupId>org.seleniumhq.selenium</groupId>
	<artifactId>selenium-java</artifactId>	
	<version>3.3.1</version>
	<scope>test</scope>
</dependency>

If we are using Gradle, we have to add the following snippet to our build.gradle file:

dependencies {
    endToEndTestCompile(
            'org.seleniumhq.selenium:selenium-java:3.3.1'
    )
}
If you know that you will use only one WebDriver implementation, you don’t need to depend on the selenium-java dependency. You can simply declare the dependency you need. This helps you avoid the unnecessary transitive dependencies of the selenium-java dependency.

Additional Reading:

Let’s summarize what we learned from this lesson.

Summary

This lesson has taught us four things:

  • Selenium WebDriver supports many common programming languages and all common web browsers.
  • The driver specific implementation of the WebDriver interface and the driver executable allows us to write automated tests that use the browser’s native test automation API.
  • We can get all required dependencies by declaring the selenium-java dependency in our build script.
  • If we want to use only one WebDriver implementation, we don’t need the selenium-java dependency. We can simply declare the dependency we want to use.