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.

Sunday, April 4, 2010

What is TestNG? And whats the difference between Junit and TestNG

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as:

  • Annotations.

  • Flexible test configuration.

  • Support for data-driven testing (with @DataProvider).

  • Support for parameters.

  • Allows distribution of tests on slave machines.

  • Powerful execution model (no more TestSuite).

  • Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).

  • Dependent methods for application server testing.

If you want to learn TestNG you can download it and see its documentation at “http://testng.org/doc/index.html”.

TestNG is extensively used in Selenium automation and Code testing instead of Junit due to its advantages over it.


Using TestNG Selenium automation becomes simplified and also the Parallel execution using Selenium Grid becomes easy.


TestNG have a lot of features in it and hence I wont be covering every thing in my blog. But will only talk about advantages of TestNG over Junit. I will be discussing about different ways of using TestNg functions in my coming blogs.


Advantages of TestNG over Junit

  1. In Junit we have to declare @BeforeClass and @AfterClass which is a constraint where as in TestNG there is no constraint like this.

  2. Additional Levels of setUp/tearDown level are available in TestNG like @Before/AfterSuite,@Before/AfterTest and @Before/AfterGroup

  3. No Need to extend any class in TestNG.

  4. There is no method name constraint in TestNG as in Junit. You can give any name to the test methods in TestNG

  5. In TestNG we can tell the test that one method is dependent on another method where as in Junit this is not possible. In Junit each test is independent of another test.

  6. Grouping of testcases is available in TestNG where as the same is not available in Junit.

  7. Execution can be done based on Groups. For ex. If you have defined many cases and segregated them by defining 2 groups as Sanity and Regression. Then if you only want to execute the “Sanity” cases then just tell TestNG to execute the “Sanity” and TestNG will automatically execute the cases belonging to the “Sanity” group.

  8. Also using TestNG your selenium testcase execution can be done in parallel.