Fitnesse and Selenium Tutorial

For recent Proof of Concept work I needed to get a simple Fitnesse script to drive a browser using selenium. I was expecting to work with existing Selenium test code in Java, so I wanted to interact directly with the Java code. Here’s a very(very) simple example of how to do a a basic Google search using Fitnesse and Selenium.

Prerequisites:
Eclipse for Java.
Fitnesse(download, run, and browse to localhost:8980)
Selenium(I downloaded selenium 2.0a5)
Selenium Server(download and run server.)

I suggest getting a little familiar with Fitnesse first. Basics like creating new pages/subpages and tests. It’s very easy. I would also assume here that you can get Selenium server running. I created a test and added the following in edit mode:

!path C:\workspace\consoleapps\bin
!path C:/selenium-2.0a5/selenium-java-2.0a5.jar

!define TEST_SYSTEM {slim}

|script|Basic Search Test|
|search with searchterm|Cat|and expect| kitten| in results|

Now I will explain the reason behind the folder: C:\workspace\consoleapps\bin
In Eclipse I created a new Java Class called “BasicSearchTest”(The name of the Test in Fitnesse). In my case the project containing the class was called ”consoleapps”, so I would expect the corresponding .class file for my Java class to be found compiled there each time I save it.
When we run the test, the first thing it does is look for BasicSearchTest.class
Then it (quite cleverly) looks for a method called “searchWithSearchtermAndExpectInResults” that accepts two strings as input (in our case “Cat” and “kitten”).

Java code for BasicSearchTest.class as below:


import com.thoughtworks.selenium.*;

public class BasicSearchTest
{
public String searchTerm;
private String resultContains;
Selenium selenium;

public BasicSearchTest() {

selenium = new DefaultSelenium("localhost", 4444, "*firefox","http://www.google.com/");
selenium.start();
selenium.setTimeout("60000");
selenium.open("http://www.google.com/");
}

public boolean searchWithSearchtermAndExpectInResults(String searchTerm, String resultTerm)
{
this.searchTerm=searchTerm;
selenium.type("q",searchTerm);
selenium.click("//input[@value='Google Search']");
selenium.waitForPageToLoad("60000");
if (selenium.isTextPresent(resultTerm)){
selenium.close();
this.selenium.stop();
return true;

}
else
{
selenium.close();
this.selenium.stop();
return false;
}
}
}

2 thoughts on “Fitnesse and Selenium Tutorial

  1. it’s a very good example – although mine did suffer from (
    __EXCEPTION__:com.thoughtworks.selenium.SeleniumException: ERROR: Element //input[@value=’Google Search’] not found) so I changed your line selenium.click(“//input[@value=’Google Search’]”); to
    selenium.click(“btnG”);

Leave a comment