Tuesday 3 January 2012

Web Driver Grid -forcing to run on a particular client node.



WebDriver is open source tool for automatic web application testing.

WebDriver Grid  concept is like a central hub will be started and the client machines will be
registered to the central hub.The client nodes will have different capabilities (capabilities here 
refer to browser version,os,platform etc).

Grid allows you to :
  • 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.

The central Webdriver hub will be instantiated using the remote webdriver  as
 webDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
and the  webDriver will be supplied with capability say for eg

capability.setBrowserName(“firefox ); 
capability.setPlatform(“LINUX”);  
capability.setVersion(“3.6”);
so ,the remote web driver will look for the nodes satisfying the supplied capability and run the tests on the particular node.So,if there are multiple nodes satisfying the capability and the pick is random

Let me brief how to set up a Gridstep 1: Start the hub using the following command on the central machine(say X).
java -jar selenium-server-standalone-2.14.0.jar -role hub
step 2: Register the node machine to the hub started in step 1
java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://X:4444/grid/register
The above are two steps to start a hub and register a client node.So there can be many client nodes listening to a single hub.
Step 3: Stepping to the test cases,the webDriver should be instantiated using the RemoteWebDriver on your hub.
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capability);
driver.get("www.google.com");
driver.findElement(By.id(.....)) and the test cases go on.
So,if there are n node machines listening to the hub and when you execute the test case,the hub will pick the node which satisfies the capability set in the step 3 and run the tests on the particular machine.The client nodes capability can be passed along with the registering command as follows
java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://X:4444/grid/register  -browser "browserName=firefox,version=7.0,platform=windows" 

I was needed to develop a web app using this grid concept,so the client can access a url and start the test on the particular machine.The trick here to solve was if there are n nodes registered to the hub with same capabilities ,the hub may delegate the test case execution to any of the clients.So,the real user who started the test may not see it running on his machine,but may be as a surprise to some other client machine where the tests get executed launching the browser.

The way I used, to solve this is by customizing the browser version to the ip of the client machine,so the capability from the client machine will be unique,so hub will have to delegate the execution of testcase to the same machine from where the request is from.
I have done little dos scripting to identify the ip config of the machine while registering to the hub.
SETLOCAL
set host=%~1
set ip=
if "%host%"=="" ( for /f "tokens=2,* delims=:. " %%a in ('"ipconfig|find "IPv4 Address""') do set ip=%%b
) ELSE ( for /f "tokens=2 delims=[]" %%a in ('"ping /a /n 1 %host%|find "Pinging" 2>NUL"') do set ip=%%a)

java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://X:4444/grid/register -Dwebdriver.chrome.driver="chromedriver.exe" -browser "browserName=firefox,version=%ip%,platform=windows" 
and while instantiating the remote webdriver in the hub,I will be setting the capability as 
below
capability.setBrowserName(“firefox ); 
capability.setPlatform(“windows”);  
capability.setVersion(request.getRemoteHost());
You can download the required  selenium-server-standalone-*.jar from http://code.google.com/p/selenium/downloads/list.
Cheers..thnx for Reading :)

No comments:

Post a Comment