How to Setup Grid in Selenium?

FREE Online Courses: Dive into Knowledge for Free. Learn More!

Selenium Grid is a distributed test execution system that allows users to run Selenium tests on multiple machines simultaneously. It enables users to run tests in parallel on different operating systems, browsers, and versions, which can significantly reduce test execution time. In this article, we will discuss steps to setpu the Selenium Grid.

How to set up Selenium Grid?

Setting up Selenium Grid involves the following steps:

Step 1: Download Selenium Server

The first step is to download the Selenium Server from the official website of Selenium. The Selenium Server is a standalone server that provides the hub and node components of Selenium Grid.

Step 2: Configure the hub

The hub is the central point of control for Selenium Grid. To configure the hub, create a JSON configuration file with the following parameters:

{
"port": 4444,
"newSessionWaitTimeout": -1,
"timeout": 300000,
"browserTimeout": 0,
"hubConfig": {
"host": "localhost",
"port": 4444,
"maxSession": 5,
"timeout": 300,
"browserTimeout": 0
}
}

The above configuration file sets the port number to 4444 and the maximum number of sessions to 5.

Step 3: Start the hub

Open the command prompt, then go to the directory where the Selenium Server was downloaded to launch the hub. Then, execute the following command:

‘java -jar selenium-server-standalone-<version>.jar -role hub -hubConfig <configuration_file_path>’

Replace <version> with the version number of the Selenium Server and <configuration_file_path> with the path of the JSON configuration file created in Step 2.

Step 4: Configure the nodes

The next step is to configure the nodes. Nodes are the machines that will execute the tests. To configure the nodes, create another JSON configuration file with the following parameters:

{
 "capabilities": [
   {
     "browserName": "chrome",
     "maxInstances": 5,
     "seleniumProtocol": "WebDriver"
   },
   {
     "browserName": "firefox",
     "maxInstances": 5,
     "seleniumProtocol": "WebDriver"
   }
 ],
 "configuration": {
   "port": 5555,
   "register": true,
   "registerCycle": 5000,
   "hubHost": "localhost",
   "hubPort": 4444,
   "timeout": 300000,
   "maxSession": 5
 }
}

The above configuration file sets the capabilities for Chrome and Firefox browsers and registers the nodes with the hub.

Step 5: Start the nodes

To start the nodes, open the command prompt on each machine that will act as a node. Then navigate to the directory where the Selenium Server was downloaded. Then, execute the following command:

‘java -jar selenium-server-standalone-<version>.jar -role node -nodeConfig <configuration_file_path>’

Replace <version> with the version number of the Selenium Server and <configuration_file_path> with the path of the JSON configuration file created in Step 4.

Step 6: Run tests on Selenium Grid

To run tests on Selenium Grid, update the test scripts to use the hub URL instead of the local browser driver. The hub URL can be obtained from the hub console at http://localhost:4444/grid/console.

For example, in Java, the following code can be used to initialize the WebDriver object for Chrome:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

Conclusion

Selenium Grid is a powerful tool that allows users to run tests in parallel on multiple machines simultaneously. Setting up Selenium Grid may seem complex at first, but it is a straightforward process once you understand the steps involved. By following the steps outlined in this article, you can set up Selenium Grid and run tests in parallel. It reduces the time and effort required for testing web applications.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *