Saturday, April 17, 2010

Integrate Selenium-RC to Selenium Grid

Most of us develop our cases in Selenium RC and test it . If we want to run our cases in parallel we need to use Selenium Grid. Selenium Grid gives us the advantage of running the cases in parallel and in different environments.

I will tell in this blog what you need to do for setting up Selenium Grid For Parallel Execution or for changing your existing code to be able to be used with Selenium Grid.First you need to get the “selenium-grid-tools-standalone-1.0.4.jar” from the lib folder under the Selenium Grid folder that you had downloaded. Import this jar to the reference Libraries under you selenium RC code.

For java you need to change the way you create the selenium object. Most of us extends the SeleneseTestCase class or create the object using "new DefaultSelenium", for Grid we need to change that with following methods.


First import the following classes into your code.


import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.closeSeleniumSession;

import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.session;

import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.startSeleniumSession;


This will import the Selenium Grid class.

Then you need to define methods as mentioned below.


protected void startSession(String seleniumHost, int seleniumPort, String browser, String webSite) throws Exception {

startSeleniumSession(seleniumHost, seleniumPort, browser, webSite);

session().setTimeout(TIMEOUT);

}


protected void closeSession() throws Exception {

closeSeleniumSession();

}


The method “startSession” will create a session using the “seleniumHost”, “seleniumPort” & “browser” value onto the Selenium remote control using the Selenium Grid.

This session object is to be used for executing your selenium commands.

Following is an example on how to use the “session()” for test execution in Selenium Grid.


public void login() {

session().open("/webpage/");

session().type("txtUserName", "admin");

session().type("txtPassword", "admin");

session().click("Submit");

}

A simplest way to use the “session()” can be,by assigning it to a Selenium object and using the selenium object in your test execution. For ex.


Selenium selenium = session();


This way you don't have to replace the “selenium” object in your Selenium RC code with “session()” as mentioned in the “login()” method example.


Also for parallel execution you need to integrate TestNG with your Selenium Code. TestNG supports multi-threaded execution. I will be writing about how to use TestNG with Selenium in my coming blogs.

3 comments :

eicp said...

Hi am waiting for how to use TestNG with selenium

Naga said...

Hi I installed selenium grid...i did not understand how to run my scripts using selenium grid.. can u give me an example how to run selenium RC scripts using grid....i am using eclipse...thanks in advance

Unknown said...

I m trying to do automation testing using "Selenium Remote Webdriver" but getting an exception when try to connect with remote server.

**The exception is:**

Unexpected error. System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:4444
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\HttpCommandExecutor.cs:line 109
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebDriver.cs:line 836


**The code I have written is:**

DesiredCapabilities capabilities = DesiredCapabilities.Firefox();
capabilities.Platform = new Platform(PlatformType.Vista);
capabilities.SetCapability("api_key", "REPLACE_API_KEY");
capabilities.SetCapability("api_secret", "REPLACE_API_SECRET");


var remoteAddress = new Uri("http://122.176.112.91:4444/wd/hub");

// ... but only if it supports javascript
capabilities.IsJavaScriptEnabled = true;


// Get a handle to the driver. This will throw an exception
// if a matching driver cannot be located
IWebDriver driver = new RemoteWebDriver(remoteAddress, capabilities);
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));


// Query the driver to find out more information
ICapabilities actualCapabilities = ((RemoteWebDriver)driver).Capabilities;

Please help me to sort out this prolem.

Thanks in advance.

Post a Comment