- Selenium Grid is a feature in Selenium that allows you to run test cases in different machines across different platforms. The control of triggering the test cases is on the local machine, and when the test cases are triggered, they are automatically executed by the remote machine. Suppose you have 5 test cases. Your local machine is running multiple applications, so you want to run your test cases in a remote machine. You need to configure the remote server so that the test cases can be executed there.
- It supports distributed test execution. Initially, you have a local machine where you write the test cases and executes on the same machine. However, in a big organization, you have multiple test cases, and it's not possible to run all the test cases in the same machine. In a large organization, you have multiple servers, so local machine distributes the test cases across different machines/servers.
- You can also run the test cases in parallel in multiple machines on Selenium Grid.
Architecture of Selenium Grid
- A Hub is a central point or a local machine that receives all the test requests and distributes them to the right nodes. The machine which actually triggers the test case known as Hub
- There can be only one hub in a selenium grid.
- The machine which is containing the hub triggers the test case, but you will see the browser being automated on other machines.
Nodes are the selenium instances which will execute the test cases that you loaded on the hub. Nodes can be launched on multiple machines with different platforms and browsers.
Note: When you are configuring nodes, you are running the test cases in different platforms. There is no rule that if your hub is running the test in windows, then all nodes must be executing the test cases in windows only. The nodes can have different platforms such as Safari on MAC, Firefox on Ubuntu, Internet Explorer on WinXP, Android.
Why Selenium Grid?
Selenium Grid is useful because of the following reasons:
- Run on different platforms: You can run the test cases in different platforms, so it is a platform independent. For example, you have a hub which has internet explorer 9. Hub has internet explorer 9 as many older websites support IE 9. Now we want to run the test cases in different platform such as Internet Explorer. As we know that only one version of a browser can be installed on a computer. You need to configure the node to which you want to send your test cases.
- Parallel execution: If you set up the Selenium Grid, you can run the multiple cases at the same time. This saves time in running the test suites.
How to set up Selenium Grid
- Download the Selenium Standalone Server to run Remote Selenium webdriver. It is available in a single jar file.
- Store the jar file at any of the drive.
- Open the cmd.
- Register the hub through cmd. Enter the command java -jar selenium-server-standalone-3.8.1.jar -role hub. This command will treat the machine as a hub.
From the above screen, we observe that the address of hub is http://192.168.1.12:4444/grid/register. The port number is 4444 where hub resides and we need to register the hub with these nodes.
The above screen shows that the hub has been created, but it does not create anything as a hub is not registered with the nodes. Now we need to register the nodes with the hub.
- Log in to another machine and register it as a node for a hub. I will remotely connect my machine to another machine through Teamviewer. To register the node with your hub, you can do it from the node machine only not from your machine, so I connect my machine to another machine. In a node machine, run the command "java -jar selenium-server-standalone-3.141.59.jar role webdriver -hub >ipaddress>/grid/register -port 5566".
- Now if we want to run the test cases in a Google chrome or Firefox browser, then we need to download the chrome driver or geekodriver in a node machine. In order to achieve this, we need to run the following command in a node machine:
"java -Dwebdriver.chrome.driver="D:\chromedriver.exe" -jar selenium-server-standalone-3.141.59.jar role webdriver -hub >ipaddress>/grid/register -port 5566"
Where, D stands for parameters from the command line. If you place D, then command assumes that it is given a single parameter and need to take care while running the test cases. It is mandatory to have exe file of a browser. Without exe file, we cannot run the selenium test cases.
Note: Both the Selenium Standalone server and exe file must be in the same path.