Extending WebTest for FitNesse and Selenium

WebTest is Selenium wrapper used in FitNesse test cases. It contains a number of methods that makes it easy to write GUI based test. However it only supports a limited number of methods (WebTest reference) and in most cases there is a need for tweaking these or simply adding new ones.

Extending the com.neuri.webfixture.WebTest class should be a simple thing to do if it had not been for the private Selenium instance attribute.

This short post will describe an approach for how to be able to extend the com.neuri.webfixture.WebTest class, the example is based on webtestfixtures version 2.01.

Using reflection to get the selenium instance

For all calls to the Selenium RC API to work the Selenium instance hidden in the super class needs to be used. This instance can only be fetched using reflection. Add the method below to your extending class and you will get access to the Selenium instance simply by calling getSeleniumInstance()


protected Selenium getSeleniumInstance() {
  try {
    Field privateInstance = com.neuri.webfixture.WebTest.class.getDeclaredField("instance");
    privateInstance.setAccessible(true);
    Selenium seleniumInstance = (Selenium) privateInstance.get(this);
    return seleniumInstance;
  } catch (NoSuchFieldException ndfe) {
    // Do nothing!
  } catch (IllegalAccessException iae) {
    // Do nothing!
  }
  return null;
}

Usage example
Retrieve the Selenium instance and you have access to the Selenium RC API, in the example below getTitle().


public class UnitTHWebTest extends com.neuri.webfixture.WebTest {
  protected Selenium getSeleniumInstance() {...}

  public boolean pageContainsTitleText(String text) {
    Selenium si = getSeleniumInstance();
    if (si.getTitle().equalsIgnoreCase(text)) {
      return true;
    } else {
      return false;
    }
  }
}

Download from:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: