Test Automation Processes with Selenium

Wouldn’t you like to automate your regression tests where you spend hours manually testing your web pages? Selenium is precisely the tool you are looking for. With the wide range of programming languages it supports, Selenium has libraries for Java, Python, C#, Ruby, JavaScript, and Kotlin.
In this blog post, I explain how you can run your tests in three different browsers on a Linux-based operating system using an example in Python.
Setup
Firstly, we need to install the pip package manager for Python to our system with the following commands.[1]
$ sudo apt update $ sudo apt install python-pip
After installing pip, we download the Selenium libraries for Python.[2]
$ pip install selenium
We are ready to write the tests in Python. Now, we need to install the driver for the browser on which we would like to test. Many prominent and popular browsers have drivers called WebDriver, which support Selenium tests. At this step, I go through the setup process using the Chrome browser. Please note the setup process is the same for all browsers. Later in this blog post, we run the test with three different browsers. You can find drivers and related download links for the browsers here.
After downloading the chromedriver file required for Linux, we need to add the directory containing this file to the system path variable.[3] We can do this with the command below. (I assume the driver we download is under /opt/WebDriver/bin directory.)
$ export PATH=$PATH:/opt/WebDriver/bin >> ~/.profile
A test example
We finished the setup, now let’s write a simple test.
Although Selenium is a tool used for automation purposes, it does not include a test tool. We will use the built-in Python unittest module to make the tests more organized and controllable.
Let’s review the test below that I have prepared for you:
#!/usr/bin/env python # -*- coding: UTF-8 -*- from selenium import webdriver import time import unittest class KartacaTest(unittest.TestCase): def setUp(self): self.browser = webdriver.Chrome() # Defines the Chrome browser self.browser.implicitly_wait(1) # Each browser request waits 1 second before generating error def test001(self): print "\n-- Kartaca Homepage --" self.browser.get("https://kartaca.com/en") # Goes to the given address time.sleep(1) print "Clicking on 'Blog' Link..." self.browser.find_element_by_link_text("Blog").click() # Clicks on Blog page time.sleep(1) print "Opening the latest blog content..." self.browser.find_element_by_class_name("blog-item").click() # Clicks on the first item with "blog-item" class title = self.browser.find_element_by_tag_name("h1").text.encode('utf8') # Gets the text in the "h1" tag and converts to utf8 format print "Title of the latest blog post: " + title time.sleep(1) for i in range(3, 0, -1): print "Ending test in " + str(i) # Countdown timer time.sleep(1) def tearDown(self): print "Closing browser..." self.browser.close() # Closes browser if __name__ == "__main__": unittest.main()
- First of all, we add
webdriver
,time
, andunittest
modules. - We create a class called KartacaTest. This class contains all the test cases we write.
- We have a function called
setUp(self)
. This function is included in unittest, and the lines we specified in it run at the beginning of each test function written. Here we have defined the Chrome browser and general waiting time for requests. -
test001(self)
function is, as the name suggests, a test case function. All test case functions you write must begin with the test string. Then it is run in alphabetical order, based on the characters you typed. Here are the next steps;- We go to the address https://kartaca.com.
- We click on the “Blog” page.
- We click on the first item with the “blog-item” class and open the latest blog content.
- We get the text in the “h1” tag and convert to utf8 format to eliminate the Turkish character problem.
-
tearDown(self)
function is also a function included in the unittest. The lines specified in it run at the end of each test function written. Here we have written a line that closes the browser.
When you save the above code with the name kartaca-blog-eng.py and run it with the python kartaca-blog-eng.py
command, you will get the output shown in the video below. Congratulations! You have written your first test.

Multi-Browser Tests with Selenium Grid
When you write simple web automation like the above or need to test in a single browser, you just need to download the appropriate browser and WebDriver driver for you. However, what if you have to run your tests sequentially, or even parallel, on many browsers? This time, the tool you are looking for is the Selenium Grid. In this section, I explain how to easily install a Selenium Grid environment on your computer or a remote server via Docker and docker-compose.
I do not talk about Docker installation as it is out of context; however, you can install it with the documentation available here. Since we have installed the pip package manager for Python, we can also install docker-compose with the below command.[4]
$ pip install docker-compose
If everything is ok, let’s examine the below selenium-hub.yml file together.
This file downloads selenium-hub, node-chrome, node-firefox, node-opera docker images one by one and runs them per the specified configuration. We connect all three browsers to the selenium-hub container we install and make these three browsers run from a single place. We run it with the below command in the directory where we download this file.
$ docker-compose -f selenium-hub.yml up -d
Once you see that the containers are running successfully, you can go to http://localhost:4444/grid/console and check it yourself. You will see the below screen:

Now, let’s connect our test to the Selenium Grid environment. First of all, we add the import os
line to the beginning of our Python code. Next, we arrange the inside of our setUp(self)
function as follows:
def setUp(self): capabilities = {'browserName': os.getenv('BROWSER', 'chrome')} # Set BROWSER environment variable to browserName, default is chrome self.browser = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities=capabilities) self.browser.implicitly_wait(1) # Each browser request waits 1 second before generating error
Thanks to the change we made, we can run tests in the browser according to the name we gave to the “BROWSER” environment variable. If not provided, the default browser is set to be “chrome”. Please note that we have given our localhost address as the server where Selenium Grid is running. If you install on a remote server, you need to change this address accordingly.
When you run the code with the commands below; it means you run and test it in “chrome”, “firefox” and “opera” browsers, respectively. You can also see the output in the screenshot below.
$ export BROWSER=chrome && python kartaca-blog-eng.py $ export BROWSER=firefox && python kartaca-blog-eng.py $ export BROWSER=operablink && python kartaca-blog-eng.py

You can make your job even easier by writing a script that runs these commands, sequentially or in parallel. Here are some relevant resources on this topic.
Conclusion
In summary, I created a simple example to show you how you can automate your tests as an end-user using Selenium and how you can run tests in multiple browsers with the help of the Selenium Grid setup.
If you would like to write your tests in Python like me, you can find answers to all your questions from here.
I hope you have many days with automation! 😃
Author: Mert Yerlikaya
Date Published: Oct 19, 2020
