Selenium webdriver and Grid Integration

In this post we will see Selenium webdriver and Grid Integration, you will get the answers of the following questions:
What is Selenium Grid?
How it is useful?
Requirements of Selenium Grid
How to Setup Selenium Grid?

Selenium grid is a way using which we can run our automated script on other system (prerequisite: they should be in LAN).

You need the selenium standalone server on both the machines i.e. on hub machine and on node machine.
Machine on which the selenium framework or script is actually present is the hub machine and the other machine on which we are intended to execute the script are the node machine.

you can download the selenium standalone server from http://docs.seleniumhq.org/download/

After downloading follow the following steps:

Step – 1: open command prompt on the hub machine or on the machine which you want to make hub.
Step – 2: Goto the folder where you put the selenium standalone server, then execute the following command:
java -jar selenium-server-standalone-2.41.0.jar -role hub

once this command executed your hub is ready to for the node to connect.
Step – 3: You can check hub is running by clicking the view config of the URL: http://localhost:4444/grid/console

Grid console

Now go to another system which you want to make node, you just need the selenium standalone server and browser specific driver on this machine but not the complete framework as like you have on hub machine.
Put both the jars in same folder.

Open command prompt and go to the respective folder and execute the following command (By using below command we are registering chrome driver)


java -jar selenium-server-standalone-2.41.0.jar \ -role node -port 5555 -hub http://IP_OF_HUB:
4444/grid/register \ -browser browserName=chrome, version=33, platform=WINDOWS -Dwebdriver.chrome.driver=\chromedriver.exe

start grid node

once you executed the node command, open the http://localhost:4444/grid/console
you will see node details, see below image

grid console, registered node

Now you have to tell selenium framework on hub machine to run the test script on the registered node.
Below is the code snippet:

NOTE: p3selenium framework already grid enabled you just have to set use_grid=true in config.properties (https://github.com/AbhishekAggarwal/p3selenium/blob/master/src/test/resources/config.properties) and p3selenium framework works in grid mode.


---------------------------------------------------------------------------------------------------
private static String SELENIUM_HUB_URL;
private static String TARGET_SERVER_URL;
---------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------
private static String getConfigurationProperty(String envKey,
String sysKey, String defValue) {
String retValue = defValue;
String envValue = System.getenv(envKey);
String sysValue = System.getProperty(sysKey);
// system property prevails over environment variable
if (sysValue != null) {
retValue = sysValue;
} else if (envValue != null) {
retValue = envValue;
}
return retValue;
---------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------
SELENIUM_HUB_URL = getConfigurationProperty("SELENIUM_HUB_URL",
"test.selenium.hub.url", "http://localhost:4444/wd/hub");
logger.info("using Selenium hub at: " + SELENIUM_HUB_URL);
TARGET_SERVER_URL = getConfigurationProperty("TARGET_SERVER_URL",
"test.target.server.url", "http://domain_name.com");
logger.info("using target server at: " + TARGET_SERVER_URL);
---------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------
capability = DesiredCapabilities.chrome();
System.setProperty("webdriver.chrome.driver", project_path
+ "\\src\\test\\resources\\driver\\chromedriver_v32.exe");
capability.setBrowserName(browser);
capability.setPlatform(org.openqa.selenium.Platform.ANY);
//setDriver(new ChromeDriver(capability));
setDriver(new RemoteWebDriver(new URL(
SELENIUM_HUB_URL), capability));
getDriver().get(TARGET_SERVER_URL + "/");
---------------------------------------------------------------------------------------------------

Now open Run/Debug Settings

Run/Debug Settings

click edit then go to arguments tab and paste the following:

-Dtest.selenium.hub.url=http://localhost:4444/wd/hub
-Dtest.target.server.url=http://domain_name.com

Set parameters for Grid

Its done.
now you can run the project and it will execute the test script on node machine