In this tutorial, we will learn about Selenium-Grid.
Another part of selenium test suite is Selenium-Grid, which allow us to run our test on the different machines, different browser, and operating systems parallel, which means that we can run the multiple tests at the same time on the different browsers and the operating systems.
Generally, the Selenium-Grid supports the distributed test execution, which allows us to run our tests in a distributed test execution environment.
We have multiple test cases, and our local system is running on the various applications, so we want to run our test cases in the remote machine, for that we need to configure the remote server to execute our test case there.
In the larger organization, we have multiple servers to run the multiple different cases, which is not possible to run in the same machine, so for that our local machine distributes the test case across the various devices and servers.
Selenium-Grid allows us to run the test case in parallel in multiple machines to speed up the execution of a test pass.
The reason why we use Selenium-Grid?
Some of the following reasons to use the Selenium-Grid:
- Parallel Execution: Selenium-Grid is very flexible for parallel execution, and support multiple browsers types and versions simultaneously to save time while running the test scripts. And Selenium-Grid proceeds from the Hub-Node Architecture to achieve parallel execution of test scripts.
- Platform-Independent: Selenium-Grid is platform-independent because we can run our test case in the different window platforms.
we have a machine 1 which has the Chrome 76, device 2 has latest the Firefox, and machine 3 has the Internet Explorer 9, so while running the test suite in Selenium-Grid, it receives each test browse combination and assigns each test to run against its require browser, because grid is a virtual machine that can be set up with each different supporting browser.
- Selenium-Grid is used to reduce the time which it takes for the test suites to complete a test pass.
- It is used to run our tests against multiple browsers, multiple versions of the browsers, and the browser running on the different operating systems.
Selenium-Grid has two different versions available in the Selenium tool suite which is as follows,
Grid-1: Grid-1 introduced by Thought Works Company in 2004.
Grid 2: Google Company introduced -it in 2008.
Now, Selenium-Grid 1 is deprecated by the Selenium community so that we will focus only on the Selenium-Grid 2.
Selenium-Grid latest version is Selenium-Grid 2.0, which is quite different from Selenium-Grid 1.
Selenium-Grid 2.0 combined with Selenium RC server, but now we only need to download a single .jar file to get the remote Selenium RC server and Selenium-Grid all in one package.
Let us see the difference between Selenium-Grid 1 and Selenium-Grid 2:
|Selenium-Grid 1||Selenium-Grid 2|
|To use Selenium-Grid1 first, we need to install and configure the apache ant.||There is no need to install apache ant to use Selenium-Grid 2|
|Selenium-Grid 1 can only automate one browser per control.||In Selenium-Grid 2, one remote control can automate multiple browsers.|
|Selenium-Grid 1 only supports Selenium RC commands.||Selenium-Grid-2 supports both selenium RC and web driver scripts.|
|Selenium-Grid1 has a remote control, which is different from the Selenium RC server.||Selenium-Grid-2 is now combined with Selenium server .jar files.|
The Architecture of Selenium-Grid
Selenium grid follows the Hub and Node architecture, where means that it has one single hub and more than one nodes, andboth will use the selenium-server.jar executable files.
- The central point or a server is called a hub, which can load all our test request and divided them to the correct nodes.
- Selenium-Grid has only one hub, which is the master of the network.
- The device will contain the hub where the tests will be executed, but the browser will be automated on the nodes.
- The hub launched only on the single machine, which means that a computer whose operating system is Window10 and browser is Chrome.
- The node is used to run the browser, and execute the test request, which is loaded on the hub.
- Selenium-Grid can have one or more nodes.
- Nodes can be launched on multiple devices with different platforms and browser.
- The device which runs the nodes is not required to be at the same platform as the hub.
Installation steps for Selenium-Grid
To install the selenium-grid,
- Go the selenium download page and download the Selenium Standalone Server to run Remote Selenium web driver.
- Once we clicked on the link and downloaded the jar file, we need to store it in the appropriate directory.
- For my reference, I placed it in the sele folder in the C drive.
- Now, we will try to configure the hub; for this, we need to open the command prompt.
- And come to the desired folder, which is present in the C driver.
Follow the below command to reach the desired folder.
C:\Users\JTP>cd.. C:\User>cd.. C:\> cd sele
After reaching that folder, type the below command to register the selenium grid hub.
C:\sele>java –jar “selenium-server-standalone-3.141.59 (1).jar” –role hub
Where, the jar name is selenium-server-standalone-3.141.59 (1).jar
-roleflag = it is used to set the particular host machine as the hub.
Note: as in below snapshot it will raise one error that port 4444 is busy, please choose a free port for the hub and specify if using –port option.
Because 4444 is the default port number on which selenium Grid gets hosted and listens for request.
To resolve this error, we will use the –port option to change the port number.
After that, hit the enter button, the below output screen will be shown.
In the above screenshot, you can see that the hub is http://10.0.0.7:3333/grid/register configured and now we register the node and connect them to the hub.
Open the given link, http:// 10.0.0.7:3333/grid/console where the Hub server is registered as you can see the below screenshot,
10.0.0.7: is the IP address of the hub, and each node should be connected.
3333: is the port number on the Selenium-Gird.
The above screenshot indicates that the hub has been created, but it is not registered with the nodes.
Now we are going to register the hub with the nodes, I will remotely connect my machine to another machine through TeamViewer.
For this, we can do it from the node machine only not from our machine but another machine also, and I connected my machine to another machine.
So for this open one more command prompt and then run the below command:
Java –jar “selenium-server-standalone-3.141.59 (1).jar” –role node –hub http://10.0.0.7:3333/grid/register.
In the below screenshot, that node is register to the hub and ready to use.
And also notice that the hub command prompt also shows a message that,
Registered a node http://10.0.0.7:43889
If you launch the hub with the help of following URL http://10.0.0.7:3333/grid/console
We will get to see the status of the nodes connected to the hub as shown in the below image,
Now, if you want to run the test cases in Google Chrome or the Firefox browser, we need to download the chrome driver or gecko driver in a node machine.
Follow the below command to
run the test cases in a different browser.
"java -Dwebdriver.chrome.driver="D:\chromedriver.exe" -jar selenium-server-standalone-3.141.59.jar -role webdriver -hub >IP address>/grid/register =port 3333"
D =It is the parameter from the command line.
We should compulsory have the .exe file of a browser because without exe file, we are not able to run the selenium test cases.
Note: the Selenium Standalone server and exe file must be on the same path while running the test case.