Testing the web applications across browsers is called cross browser testing. Zerion products are available for use on Firefox and Chrome platform. Thus, it is important to test all our products on both of these platforms to ensure a better quality of the products. This process helps to detect any discrepancies in the application when it runs on different browser platforms.
To achieve the above goal we will mainly use 3 components — Selenium Grid, Docker and TestNG framework. In this blog I will try to provide an overview of these tools and frameworks in brief.
- Selenium Grid — allows to run the tests on different local/remote machines against different browsers.
- Docker — provides a container image which is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it.
- TestNG — a testing framework with parallel execution capabilities.
Selenium Grid Overview
As per Selenium-Grid documentation — “Selenium-Grid allows you run your tests on different machines against different browsers in parallel. That is, running multiple tests at the same time against different machines running different browsers and operating systems. Essentially, Selenium-Grid support distributed test execution. It allows for running your tests in a distributed test execution environment.”
For more explanation about Selenium Grid and how to install it please refer Selenium Grid 2 Wiki as well.
There are two main components of Selenium Grid:
- Hub: This is the central point of the grid network where we load the tests. This is the master node which assigns the test execution to slave nodes as per the desired capabilities like browser type, browser version etc.
- Node: This is a slave node in the grid network which is registered to the hub. The tests are run on this node.
Create a hub and then register nodes to that hub. The tests are run on the nodes and hub makes sure that the tests are run on appropriate node considering the desired capabilities of the webdriver. Instead of having actual machines with different browser types we can use docker containers as hub and nodes for our test execution.
As per Docker documentation — “Docker provides docker containers images which is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings. These are an abstraction at the app layer that packages code and dependencies together. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated processes in user space. Containers take up less space than VMs (container images are typically tens of MBs in size), and start almost instantly.”
We can create our own docker container images customized as per our requirements with the help of dockerfile. But docker also has docker hub where we can find many prebuilt container images. For selenium we already have multiple prebuilt images here with various options. For our goal we will be using selenium/hub, selenium/node-chrome-debug and selenium/node-firefox-debug.
In order to create the hub on our localhost, we need to pull and run container from Docker repository with selenium hub:
$ docker run -d ‐‐name selenium-hub -p 4444:4444 selenium/hub
This command will download and run hub container on our localhost. Now we will create two nodes, one with Firefox and second with Chrome. To download and run these container:
$ docker run -d -P ‐‐link selenium-hub:hub selenium/node-firefox-debug
$ docker run -d -P ‐‐link selenium-hub:hub selenium/node-chrome-debug
We should have three docker containers running on our local docker machine. We can check that with following command:
$ docker ps
You can check the grid console at http://localhost:4444/grid/console where you will be able to see the nodes currently registered to the hub.Grid Console
We can use docker stop container-id to stop individual containers. To perform all these operations we need to use multiple commands but with the help of docker-compose tool we can start or stop the whole network with only one command.
As per Docker-Compose documentation — “Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. Using Compose is basically a three-step process: Define your app’s environment with a Dockerfile so it can be reproduced anywhere, define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment and lastly, run docker-compose up and Compose will start and run your entire app.”
For our goal, the YAML file will look like this:docker-compose.yml
We can start the whole grid by using only one command. First navigate to the directory where docker-compose.yml file exists and run following command.
$ docker-compose up -d
Similarly the whole grid can be stopped by using following command:
$ docker-compose down
Parallel execution using TestNG
We can use TestNG framework for executing test cases in parallel on firefox and chrome browsers as shown below.testng.xml file is a configuration file in TestNG for test executions
Once we mention parameter values in testng.xml file we can then use appropriate DesiredCapabilities so that the Selenium runs the test as per requirement. Here, as we have told TestNG to run tests in parallel, it will start multi-threaded execution of the mentioned tests. While instantiating RemoteWebDriver, grid will automatically search for a node available with desired capabilities and then assigns those executions to the node.
We can use Extent Reports for reporting and logging these executions. Extent reports provides interactive and runtime reports for test executions. I hope this blog will be a good starting point for anyone who wants to develop test automation for Cross Browser Testing with parallel executions on various browsers.
Hi All! I’m Pranav, a Software Automation Engineer at Zerion Software. I have 4 years of experience in Test Automation. I will post here blogs about automation testing and the practices we follow at Zerion Software. Stay tuned for more interesting blogs coming from our engineering team.
Cross Browser Testing using Selenium Grid and Docker was originally published in ZerionEngineering on Medium, where people are continuing the conversation by highlighting and responding to this story.
Please sign in to leave a comment.