Selenium-Grid using Command Prompt with examples
Selenium grid system is a part of the selenium suite which allows the user to run multiple test cases simultaneously across different browsers, operating systems, and machines.
To implement the selenium grid, we need at least two systems. Among the two systems one will serve the purpose of hub and another one serves as node.
Hub System- It is a system in which you run your test scripts or a system in which you write your selenium scripts. There should be only one Hub system in a grid.
Node System- The script which you run from hub system will execute in the Node system. There can be single or multiple nodes in a grid depending on your requirements
Note : Both the Hub and Node systems should be in the same LAN segment i.e should be in the same LAN network.
Follow the following steps to execute the selenium grid system
1. Download the Latest version of Selenium Standalone
server from https://docs.seleniumhq.org/download/
2. Open the command prompt on the Hub system
3. Navigate to the folder where you kept the setup of selenium server and run the following command (I have kept the setup in the C drive)
C:\>java -jar selenium-server-standalone-3.14.0.jar -role hub
here selenium-server-standalone-3.14.0.jar is the name of the setup file and
-role hub defines the role of the system as Hub system
4. Verify that Hub is running by entering http://localhost:4444/grid/console in browser window. You will see the following image
5. Click on the view config link and note down the IP address of the Hub system and choose any free port no.(like 5576)
6. Go to the Node system (other pc) and open the command prompt
7. Navigate to the folder where selenium server and browser driver (like firefoxDriver.exe) is present in the node system and run the following command
java -Dwebdriver.gecko.driver=”C:\geckodriver.exe” -jar selenium-server-standalone-3.4.0.jar -role node -hub http://192.168.1.3:4444/grid/register -port 5566
8. Go to http://localhost:4444/grid/console, you will see all the browsers connected in the grid system
9. Write the script by using DesiredCapabilites and RemoteWebDriver objects
DesiredCapabilites is used to set the type of browser and OS that we will automate
and
RemoteWebDriver is used to set which node (or machine) that our test will run against.
10. Go to http://localhost:4444/grid/console and hover on the icon of the browsers and note down the values of platform and browserName
set noted values in the desiredCapibilities object like this
DesiredCapabilities capability = DesiredCapabilities.chrome(); capability.setBrowserName("chrome"); capability.setPlatform(Platform.WIN8);
11. Initialize the RemoteWebDriver object like this
driver = new RemoteWebDriver(new URL(“http://192.168.1.97:5566/wd/hub”), capability);
Here 192.168.1.97 is the IP address of the Node system and 5566 is the port Number
12. Run the script from the Hub system and see the output on the Node system
Complete code written in the hub system
package Leaning; import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.remote.RemoteWebDriver; public class SeleniumLearning { public static void main(String args[]) throws InterruptedException, MalformedURLException{ // declaration and instantiation of objects/variables WebDriver driver ; DesiredCapabilities capability = DesiredCapabilities.chrome(); capability.setBrowserName("chrome"); capability.setPlatform(Platform.LINUX); driver = new RemoteWebDriver(new URL("http://192.168.1.110:5567/wd/hub"), capability); driver.manage().window().maximize(); driver.get("https://www.google.com/"); System.out.println("closing window"); driver.close(); } }