Integration of Fixture and Selenium

Traditionally acceptance test was difficult to integrate with web. Now with combination of fitnesse and selenium you can do the integration easily.

Selenium is software testing tool for web application. It can run on all platforms and has got client drivers for Python, Ruby, .NET, Perl, Java, and PHP.

FitNesse is acceptance testing tool which allows test cases to be written is english language. It was initially written in java but its now also available in C++, Python, Ruby, Delphi, C#

Installing Selenium
download the selenium RC server from http://selenium-rc.seleniumhq.org.
To run the server use command 'java -jar selenium-server.jar'.
This will run selenium server on port 4444, the port can be changed by -port option during startup of selenium server.

Selenium Fixture class
You need to create a class say SeleniumFixture which extends the DoFixture class for creating the custom fixture.

public class SeleniumFixture extends DoFixture {

    ....
    ....
    public boolean startBrowser() {
        if (browser == null) {
            browser = new DefaultSelenium("localhost", 4444, "*iexplore", "http://localhost:8080/");
            browser.start();
        }
        return true;
}

This custom class will create instance of DefaultSelenium which is selenium interface reference. Next you need to expose methods of DefaultSelenium as following.

public boolean isTextPresent(String text) {
        return browser.isTextPresent(text);
    }

Exposing class methods to fitnesse wiki
You can call the above methods from fitnesse wiki as following.
|is text|put your text|present|

This will integrate the selenium with fitnesse.

Example code
Lets take an example walkthrough of integrating selenium with fitnesse.

Following is code of fitnesse test case that checks for remember me checkbox functionality at login page. Once the checkbox is checked the use name is remembered so next time login there is no need to fill the user name

!|com.xebia.xtime.fitnesse.selenium.SeleniumFixture|
|start browser|*iexplore|
|go to url|http://localhost:8080/xtime/|and wait|60000|
|write|ywoe|to text field|j_username|
|write|tomcat|to text field|j_password|
|click|rememberMe|
|click |//input[@name='login']|and wait|60000|
|click|link=Logout| and wait|60000|
|validate|ywoe|in field|j_username|
|write|tomcat|to text field|j_password|
|click |//input[@name='login']| and wait|60000|
|is text|Logged in as: Yuk Woe|present|
|is text|Time Sheet (Create / Update)|present|

Above example code uses methods in SeleniumFixture class as startBrowser, goToUrlAndWait, writeToTextField, click, clickAndWait, validateInField and isTextPresent.

Selenium Fixture code of above example.

public class SeleniumFixture extends DoFixture {

	   ...
	   ...
	   ...
    public boolean startBrowser(String browserStartCommand) {
        if (browser == null) {
            browser = new DefaultSelenium("localhost", 4444, browserStartCommand, "http://localhost:8080/");
            browser.start();
        }
        return true;
    }

    public boolean goToUrlAndWait(String url, String waitTime) {
        browser.open(url);
        browser.waitForPageToLoad(waitTime);
        return true;
    }

   public boolean writeToTextField(String text, String field) {
        browser.type(field, text);
        return true;
    }

   public boolean click(String link) {
        browser.click(link);
        return true;
    }

    public boolean clickAndWait(String link, String waitTime) {
        browser.click(link);
        browser.waitForPageToLoad(waitTime);
        return true;
    }

   public boolean validateInField(String value, String field) {
    	String thisValue = browser.getValue(field);
    	if (thisValue.equals(value)) {
    		return true;
    	} else {
    		return false;
    	}
    }

   public boolean isTextPresent(String text) {
        return browser.isTextPresent(text);
    }
}

When you integate and run the fitnesse it will open a new browser which is specified in startBrowser method call.

Conclusion
We have talked about how to integrate the fitnesse with selenium. We covered the changes that needs to be done to create a java class which wraps around DefaultSelenium object and we talked about the way the methods created in java class are exposed to fitnesse world. Towards the end we have taken a real example to understand the integration better.

Comments (2)

  1. Shery - Reply

    May 29, 2009 at 11:41 pm

    Hey, I was trying to do the same but selenium server is not responding to the fitnesse.

    I have downloaded the Selenium RC and started the RC server
    Downloaded the fitnesse on my local machine in the path c:\fitnesse and start the port at 8080
    create a SeleniumFixture class.

    I gave all the files in the class path but still there is no reponse between these two , can you please help me

  2. Rahul - Reply

    October 28, 2010 at 7:45 am

    Hi Shery....you need to start the Fitnesse server first. Then start the selenium server as well.
    For first time user, you need to configure the Fitnesse,
    e.g. Once you start the fitnesse server & open http://localhost:8080 at the bottom of the page you can see a link 'Root' with a '?' next to it. Click on the '?', then you need to pass the path where you have placed your seleniumserver.jar file & your Fitnesse.jar file address & the path of your script working folder.
    Then go to edit option and paas the package & test script name.

    Now you are ready to write your own fixtures 🙂

Add a Comment