Parallel Execution using Selenium Grid
Selenium Grid is one of the components of Selenium. It is used to execute the web driver scripts remotely across different browsers, operating systems, and machines in parallel. We can scale and distribute scripts across many environments. Selenium Grid is available in 2 versions. The older version is Grid 1 and the latest version is Grid 2.
These speeds up the test execution time. Assume 200 test scripts take 2 hours to execute sequentially. We can reduce it by splitting 20 test scripts run across 10 machines. Eventually this reduces execution time and effort.
Selenium Grid Components
Selenium Grid works on 2 components.
1. Hub – It acts as a server. It is the central point that will receive all the test requests and distribute them to the right nodes. There is only one hub and it is like a lead. It has the following components. Once the hub is started, it automatically starts all the components.
a. Router – It sends the request to the node
b. Distributor – It decides which node is to be executed
c. Session Map – It stores the session id and node’s physical IP address
d. New Session Queue – When we send multiple requests, multiple sessions will be waiting in Session Queue and waiting for the Distributor to be picked it up.
e. Event Bus – It enables the communication between components.
2. Node – It is an individual machine; it can be either a physical or virtual machine that registers to the node for test execution.
Selenium Grid also has a console page that shows the details of which nodes are connected, IP addresses, browsers, their maximum instances, and execution status.
When we execute our code in our local system, first Selenium Hub will be reached. Hub will decide how to distribute the tests to different nodes. Assume that Chrome installed in node 1, Firefox installed in node 2, Edge installed in node. If my test demands for Chrome, then Hub directs to node 1. Hub reads the properties during run time, and decides where and how to distribute the workloads.
Selenium Grid Roles
There are different Grid Roles available in Selenium Grid.
1. Selenium Standalone: It is a collection of all the components. After starting in the standalone mode, a fully functional grid is available.
2. Hub and Nodes: This is suitable for small and medium sized grids. Here the hub and node setup are enabled.
3. Distributed : This is suitable for large grids. Each node needs to be started on its own.
To download Selenium Server
1. Go to the url https://www.selenium.dev/downloads/
2. Click on the latest stable version
3. Once the download is completed, we can find the jar file in the downloads folder.
To add selenium-server-4.8.1.jar to Project
1. File - > New -> Java Project -> Enter the name for your project -> Next
2.Click on Finish
3. Click on Don’t Create
4.Right click on your project -> Properties -> Java Build Path -> Class Path -> Add External JAR file
5. Next, Navigate to the folder where you stored selenium-server-4.8.1.jar file -> Open -> Apply and Close.
6. You can see Selenium-server-4.8.1.jar file is added under Referenced Libraries.
Steps to set up Selenium Grid using Hub and Node
1. Go to the location where selenium-server-4.8.1.jar is downloaded.
2. Type cmd on the bar
3. Type java -jar selenium-server-4.8.1.jar hub in the command prompt to start the hub which in turn starts all other components too. By default, the Selenium hub starts on port number 4444.
Here the selenium-server-4.8.1.jar is acting as a hub. If we want to run on many machines, try giving a different port number for the others.
To start the Selenium Server in Command Prompt
java -jar selenium-server-4.8.1.jar hub
URL to see the Console
4. Open a chrome browser and type the following url to view the console.
We can see that no nodes are attached to our hub.
5. To start a node in the same system and register to the hub
Go to another command prompt and type the following command.
java -jar selenium-server-4.8.1.jar node --detect-drivers true
Note: Hub and Node can be on another system or the same system. By default, node starts on port number 5555.
6. We can find a node is added in the console. The node is capable of running 8 Chrome browsers, 8 Firefox browsers 8 Edge and 1 Explorer browser and 1 Edge browser.
7. Go to Eclipse. Create 2 simple Test cases. Initialize the driver with RemoteWebDriver class with 2 arguments, one is the IP address of the hub and port number. We designed to run one test case in Chrome and another in Firefox. DesiredCapabilities are used to define the capabilities of the browser and OS.
DesiredCapabilities c=new DesiredCapabilities();
WebDriver driver=new RemoteWebDriver(new URL(http://192.168.1.175:4444,c);
8. Then convert the test cases into TestNG.
Right-click on our project -> TestNG -> ConvertToTestNG
9. Select Parallel mode : classes. Click on Next -> Finish.
10. These 2 classes are going to be executed in parallel. One in the chrome browser and another in the firefox browser.
11. Google.java is expected to run in the chrome browser and NumpyNinja.java is expected to run in the firefox browser.
Right click on testing.xml -> Run As -> TestNG Suite.
In this blog, we have covered how to configure Selenium Grid in our system and how to do parallel testing using Selenium Grid. This helps to distribute the tests across multiple physical or virtual machines and reduces the execution time.