Thursday 24 January 2013

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 staticcom.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.closeSeleniumSession;
import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.session;
import staticcom.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.

No comments:

Post a Comment