Web Browser Automation with Selenium

Introduction to Selenium and Selenium tools

Selenium is an open-source browser automation tool that works across different browsers and platforms. Its main purpose was for automating web applications testing but currently, it is capable of much more. One major advantage of the Selenium software is its open source code, so it is free and there is no licensing cost involved. It can be integrated with Jenkins, Maven to achieve continuous integration, also with testing tools as JUnit for managing test cases. Selenium tests can be carried out in any OS(Linux, Mac, Windows) and also in many browsers(Mozilla Firefox, Google Chrome, Safari, Opera, Internet Explorer). 

Selenium is composed of a suite of tools that can help you with different roles:

  • Selenium WebDriver
  • Selenium RC (currently depreciated)
  • Selenium IDE
  • Selenium Grid

Selenium RC

Selenium RC is deprecated starting from Selenium version 3 and has been moved to the legacy package. You can still download and work with it but Selenium WebDriver is more efficient and you should use it.

Selenium WebDriver (Selenium 1.0 + WebDriver = Selenium 2.0)

Selenium WebDriver offers a programming interface for creating and executing test cases. Its main goal is to offer object-oriented API for advanced web applications testing. In the latest Selenium 3 version WebDriver and Selenium RC are merged. How to install, setup and use Selenium WebDriver depends on your development environment. Selenium WebDriver helps you to use different programming languages so you can write your own Selenium project.

Python

Install Selenium with pip:

pip install selenium

Java

Best and easiest way to setup Selenium with Java is to use Maven since it will download all java bindings and its dependencies. Simply import maven project into your IDE, create a folder and add Maven pom.xml file.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>MySel20Proj</groupId>

<artifactId>MySel20Proj</artifactId>

<version>1.0</version>

<dependencies>

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-server</artifactId>

<version>3.0.1</version>

</dependency>

</dependencies>

</project>

After adding pom file inside the file directory run:

mvn clean install

This command will download Selenium and its dependencies.

C#

Download latest .net Selenium zip file from Selenium storage, unzip the file and reference dlls to your project in your IDE( Visual Studio etc). 

Also, you can get the official NuGet package from NuGet.

Ruby

To add Selenium install selenium-webdriver gem from the command-line:

gem install selenium-webdriver

JavaScript

If you are using NodeJS install the Javascript bindings with npm:

npm install selenium-webdriver


For different languages as PHP or Perl check out official project setup documentation.

When your Selenium tests use the WebDriver API and all of the tests run on the same machine, WebDriver will run the browser for you so you don't need to setup Selenium-server. WebDriver sends commands to a browser through browser specific driver. There are different available drivers for most of the browsers:

  • Firefox Driver (Gecko Driver)
  • Chrome Driver
  • Internet Explorer Driver
  • Opera Driver
  • Safari Driver and
  • HTM Unit Driver

Selenium IDE

Selenium Integrated Development Environment (IDE) is a Firefox plugin that helps you build test scripts. It has a recording feature which records user actions and exports them as a script in different programming languages. Selenium IDE is very easy to use and install, it doesn't need previous programming experience to use it, so it should only be used as a prototyping tool or as a tool to get familiar with Selenium commands.

Selenium Grid

There are a few reasons why to use Selenium-Grid:

  1. Having tests that you should run on different browsers, different browsers versions on different operating systems.
  2. Reduce the time for completing a test case.
  3. Running tests in a distributed test execution environment.

Running tests in parallel by using multiple machines could speed up the execution of a test pass. Setting up Selenium-Grid to support 2 machines could save you half the time as it would run on a single machine. This is very useful when you have long run tests with a lot of data validation. 

Selenium-Grid could help you with setting up different machines with different browsers e.g (IE, Chrome, Firefox) and run the same test on each of the browsers. Since Selenium-Grid is very flexible you can combine multiple instances with a different browser, browser version and run test simultaneously. One important thing to note is that Selenium Grid doesn't support tests design, it is only used for tests execution.

Which Selenium tool to choose?

Selenium IDE

  • Learn about concepts of automated testing as Selenium commands(type, open, assert, clickAndWaity etc).
  • To create tests without previous knowledge in software development.
  • To export tests in different formats.
  • Record test cases on Mozilla Firefox.

Selenium Grid

  • To execute the test suite with hundreds of test cases in less time.
  • To run test suites in multiple browsers, different browsers version and operating systems simultaneously.

Selenium WebDriver

  • To write more advanced test cases that work on all platforms.
  • To choose your own language and write the test cases.
  • To interact directly with browsers instead of setting your own server.

Summary

Selenium offers options for parallel testing capability, writing test cases for different browser types and versions and different operating systems. It is open source,  free for download, and easy to implement. Also, Selenium provides an option for users that don't have programming background to record and playback Selenium tests without the need to learn a scripting language. It is used by Facebook and Google and has a strong open source community. With all of this benefits, Selenium is the best option for you as your test tool.