Selenium Grid is a server that allows tests to use web browser instances running on remote machines. With Selenium Grid, one server acts as the hub. Tests contact the hub to obtain access to browser instances. The hub has a list of servers that provide access to browser instances (WebDriver nodes), and lets tests use these instances. Selenium Grid allows running tests in parallel on multiple machines, and to manage different browser versions and browser configurations centrally (instead of in each individual test).
Necessary of using Selenium Grid
- scale by distributing tests on several machines ( parallel execution )
- manage multiple environments from a central point, making it easy to run the tests against a vast combination of browsers / OS.
- minimize the maintenance time for the grid by allowing you to implement custom hooks to leverage virtual infrastructure for instance.
Setting up the HUB in the local machine
- Download Selenium server standalone jar click here.
- From the folder containing the Selenium server standalone jar file, Right click while holding shift button to open command prompt.
- Write command in cmd window
- The nodes should communicate with the hub through the port number 4444
java -jar selenium-server-standalone-2.53.0.jar -port 4444 -role hub
- Note the register url mentioned in the console logging to register the nodes to the HUB.
- The grid console can be viewed by typing localhost:4444 in any browser window.
Setting up NODEGo to machine which needs to be set as a node. Please ensure the availability of the Selenium server standalone and the driver file for different browsers in the machine.Open command prompt and type the following to register to HUB
java -jar D:\Selenium\selenium-server-standalone-2.53.0.jar -role node -hub http://10.123.74.64:4444/grid/register/ -browser browserName=firefox -port 5555
Open the HUB console window by typing localhost:4444 in browser. Click on console to view the nodes registered with HUB currently.Clickm on configuration tab here you will see individual browsers connected to hub with the node url. You have to do below changes in the selenium script, which needs to be run in the grid setup.
- Internet Explorer
DesiredCapabilities cap = DesiredCapabilities.internetExplorer(); cap.setBrowserName("internet explorer"); String Node ="http://10.138.16.48:5558/wd/hub"; driver = new RemoteWebDriver(new URL(Node), cap);
DesiredCapabilities cap1 = DesiredCapabilities.firefox(); cap1.setBrowserName("firefox"); String Node1 = "http://10.138.16.48:5555/wd/hub"; driver = new RemoteWebDriver(new URL(Node1), cap1);
- The RemoteWebdriver is used to instantiate the webdriver instance. The DesiredCapabilities object is used to set preferences about the browser.
- The execution which gets triggered in the HUB gets executed through the Node instances.