Test automation with Selenium — basics and a first test

iteo
7 min readJun 2, 2021

--

This article’s goal is introducing the Selenium tool, its pros and functionalities, and showing how easy it is to create your first test using the C# language. Selenium is one of the most recognizable open-source tools. It has an expanded set of advanced test automation functionalities for web applications, and thanks to its universal architecture it enables creating scripts in languages such as: C#, Java, PHP, Ruby, Python or Javascript.

When do we use Selenium?

Selenium comes in handy especially while working with large and complex projects in which regression tests turn out to be extremely time consuming and require engaging many people.

Speaking about its advantages, it’s worth focusing on two areas: saving time and the quality of work.

Saving time

Although automated tests in the initial stage of software development require a lot of work, it will enhance and speed up test performance in its later phases and allow a more efficient use of time in a project. A huge advantage of automated tests is that, when implemented properly to the CI/CD process, they can be launched in a random, defined moment.

Quality of work

Automated tests are more repeatable — allowing recreation of an identical test in unchanged conditions and performing regression tests and retests in a shorter period of time. However, the condition is high quality of prepared scripts. In most cases, it’s nearly impossible to test the whole large project manually with each alteration or update in the system without engaging a significant number of testers. Here’s where testing scripts help — they allow quick retest performance and regression tests.

A tester who uses automated tests is able to provide a large amount of test data with any changes to the software, system, or environment, and automatically detect bugs that could be omitted during manual tests due to human error or time restrictions. A tester’s job is especially essential and becomes a significant part of the software development process when working on complex, large-scale projects.

You can find more information on when it’s worth automating the tests in the article Tests automation in a project — when and why?

Selenium libraries you should know

To use Selenium to its fullest potential, you need to know three libraries that it comprises: IDE, WebDriver and Grid.

Selenium IDE

It’s a Firefox or Chrome plugin which allows recording and recreating an action in a browser. Its advantages are:

  • Friendly user interface
  • Intuitive operation without software experience
  • Debugging possibility

However, the tool’s not perfect either:

  • It can’t be connected to a database (e.g. CSV files import or an Excel table)
  • It can’t generate a report from tests

Selenium IDE is a tool which is regularly enhanced. For example, a developer or a tester can use it for easy completing contact forms automation.

Here’s a simple instruction of Selenium IDE installation:

First, you need to launch the Mozilla Firefox browser and install the addon.

For a convenient use, it’s worth installing an additional Selenium IDE Button plugin which enables a quick Selenium IDE launch.

The last thing that’ll streamline the work is downloading an XPath Checker. It’s an interactive XPath expressions’ editor that allows a simple localization of web application elements.

After installing the addons, you need to restart the browser and check Menu -> Addons to see if they appear on the list. If so, go back to Menu and choose Adjust. Then drag the Selenium IDE Button’s icon to the upper speed dial bar and press the Esc key.

Selenium WebDriver

It’s the most popular tool from the Selenium family which provides a user with a ready-made API allowing an interaction with the browser. As an internet browser client, using an HTTP protocol, it sends a JSON file created in one of the programming languages. The code then navigates the browser through a dedicated Driver. Moreover, WebDriver supports tests in a mobile version. Its great advantage is that, with well-chosen scenarios and application’s automated tests maintenance, it allows a regular verification of the critical areas in the system. It provides a better control over potential defects, too. Regular retesting and regression tests allow verifying the correctness of the most essential app functionalities. Apart from that, using Page Object Model, you can easily implement corrections in the test scenarios.

The Selenium WebDriver operation is displayed in the following image:

source: laptrinhx.com

After learning the theoretical basics, it’s time to move along to practice.

To start working with Selenium WebDriver, you should install an IDE, i.e. an integrated programming environment, e.g. Rider or Visual Studio.

After the installation, you can start working on the first test with Selenium WebDriver (the following test will be created in the Rider environment).

  1. Creating a new Unit Test project using a C# language.

2. Downloading proper NuGet packages.

3. Creating class (FirstTestSelenium) and defining _driver and _wait fields.

4. Creating a Setup() method which, thanks to the [SetUp] attribute, will be performed before every test. Within this method the _driver and _wait should be initialized and the browser window maximized.

5. Creating a test method with a [Test] attribute.

Inside the method, giving an order to the driver to upload the indicated website, find an element and assign it to an actualLogo variable.

In order for the test to be fully valuable, you need to use the assertion and affirm that the found element is the one that’s been expected.

6. The last element of the test is to create a method with a [TearDown] attribute which will run after each performed test, close the browser’s window and finish the WebDriver’s session.

7. The following image shows that the test has been positively performed.

_driver did its job, the assertion was correct, and you can say that the operation was successful.

8. The whole class, along with the performed test, looks as shown below (it needs to be stressed that it’s the simplest test that uses Page Object Pattern)

It’s worth noting that the test has been performed in the Chrome browser, in its specific version, and on the hardware with the Windows system.

Performing the same test in different infrastructure becomes much simpler thanks to using the latest Selenium library — Grid.

Selenium Grid

The tool allows creating infrastructure which enables launching tests on different machines, operating systems, environments and browsers (varied versions). Using the Selenium server, it also makes the concurrent testing easier (several tests at the same time).

Its operation is illustrated with the following image:

source: testelka.pl

What’s a hub?

A hub is a machine that is the Grid’s central point. It receives requests and distributes them among Nodes (test machines with various combinations of operating systems, browsers and other parameters). When a hub receives a request to perform a test, e.g. on Windows 10 and Chrome browser, it looks for a Node that refers to this specification and it will launch tests on this Node (as far as it’s found).

Launching a hub’s architecture

A hub can be launched in two ways. First one is uploading and launching the JAR file for every Node (which is problematic when the tests require launching it on different versions of browsers). The second one is using Docker on which we can install Nodes for different versions of browsers and use them simultaneously according to defined needs. Docker’s infrastructure is also easier to manage and less encumbering for the system than launching a virtual machine.

That’s why Docker is a better choice. The instructions on how to install and configure it are available here.

Summing up

Selenium is not the only tool for open-source automation, but it’s still the most popular one. The range of possibilities it brings is huge, so it’s worth taking a minute or two, get to know the tool, and learn about its functionalities.

--

--

iteo
iteo

Written by iteo

iteo is an international digital product studio founded in Poland, that helps businesses benefit from technology better. Visit us on www.iteo.com

No responses yet