Selenium Grid is a smart proxy server that is used to execute test cases parallelly in multiple machines against different browsers. It is used to run tests against multiple browsers, multiple versions of browsers, and browsers running on different operating systems. And it reduces the time it takes for the test suite to complete a test pass.
The two major components of the Selenium Grid architecture are:
Hub: Hub is the central point to the entire GRID Architecture which receives all requests. There is only one hub in the selenium grid. Hub distributes all the test cases across each node.
Node: There can be multiple nodes in Grid. Tests will run in nodes. Each node communicates with the Hub and performs test cases assigned to it.
Figure 1.1
Step 1: Installation
Download the Selenium Server jar file from Selenium’s official website and save it at any location on the local disk.
Step 2: Start Hub
Open the command prompt and navigate to a folder where the server is located. Run the server by using the below command
java -jar selenium-server-4.3.0.jar hub
By default, the hub will use port 4444. This port can be changed by passing the different port numbers in the command prompt provided the port is open and has not been already assigned a task.
The Command Prompt will be displayed like below.
Figure 1.2
You can view the status of the hub by opening a browser and navigating to http://localhost:4444/grid/console and the Screenshot for the same is shown below.
Figure 1.3
Step 3: Start Node
Now to register the Nodes in the same machine, Open the command prompt and navigate to a folder where the server is located. Run the server by using below command.
java -jar selenium-server-4.3.0.jar node --detect-drivers true
Detect drivers will detect all the drivers in the local drive.
Figure 1.4
To register the Nodes in a different machine, Download the selenium server jar and the drivers in that machine and Run the below command.
java -jar selenium-server-4.3.0.jar node --detect-drivers true --publish-events tcp://<ipaddressofhub> --subscribe-events tcp://<ipaddressofhub>
XPUB and XSUB are the two sockets. In Figure 1.2, XPUB indicates the Publish events and XSUB indicates the Subscribe events, so the IP address of publish events will be http://192.168.1.156:4442 and the IP address of subscribe events will be http://192.168.1.156:4443. This will register the node to the hub on another machine.
Figure 1.5
In Figure 1.5, we can see that the Node has been added and it has the capability of running 8 Chrome browsers and 1 Safari browser.
Step 4: Run Tests using Selenium Grid
Once the Selenium Grid setup is done by following the above 3 steps, testers can run tests by accessing Grid.
Now Create RemoteWebDriver and Desired Capabilities objects to define the browser and platform. Then pass the set of browser capabilities into the RemoteWebDriver object as shown in Figure 1.6
Figure 1.6
If these capabilities do not exist on the Grid, the code returns no match and thus the tests fail to run.
The Output of the above test cases is shown below.
Figure 1.7