Wednesday 8 October 2008

Getting started with FitNesse

A few other colleagues have been trying FitNesse, and hitting the same problems we did.

Lets be frank, the documentation that comes with FitNesse inst great for a beginner, so i have written a brief "how to" (see below) that should get you up and running.

This isn't a step by step hold your hands tutorial (and i haven't found one on the internet either), it assumes you have some level of technical Schwartz and i assume you are using windows (if you are using linux, you should be able to translate without problem). I am alos talking about testing in java here, i wont be covering the .NET tests.

Some Handy Links:

The guy (gojko) who wrote WebTest and Dbfit fixtures, writes a blog which contains a wealth of info.

The Webtest Reference is a must read if you are going to use that fixture, but it is better to print out and pin up.

Steps to create your first test suite.



Installing:

Do you have java installed?
FitNesse is written in Java so you need to have Java installed on your PC to be able to use Fitnesse. If you don't know if you have Java installed, open a command prompt, and tytpe the following

java -version


If you have java installed you should see a message similar to


H:\>java -version
java version "1.5.0_15"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_15-b04)
Java HotSpot(TM) Client VM (build 1.5.0_15-b04, mixed mode)


it doesn't matter too much if the message isn't exactly the same, but if you get the following message


'java' is not recognized as an internal or external command,
operable program or batch file.


then you don't have java installed (or you might have but its not in your path, either way fitnesse isn't going to run until java works for you).


The download of fitnesse will give you a zip file fitnesse.zip.
You should unpack it anywhere you like. But c:\ would be best. if you extract it with folder names it will create a fitnesse directory for you.
Go to that directory and double click on run.bat
You should get a message saying that FitNesse started.
You can then start up your browser and go to http://localhost

Note: when you clicked on the run.bat you have got a warning from windows firewall you should allow or unblock fitnesse as it runs a webserver so it needs to have access.

If you get an error, you may have a web server running on your machine already, using port 80. If this is the case you can specify a different port for fitnesse to run on, like 8080, you can achieve this by going to the fitnesse directory and typing:

java -cp fitnesse.jar fitnesse.Fitnesse -p 8080


Then navigate to it in your web browser. (http://127.0.0.1:8080/)

if this works for you, simple edit the run.bat file and add your port preference in there.

Beefing it up

We now need to install the webfixture. This is the thing that will save us from having to write java code, its taxonomy is also a little warmer than raw selenese.

visit http://www.fitnesse.info/webtest and download the java binaries only package. It will be a jar file.

once downloaded you need to navigate to your fitnesse folder, again i'm assuming its c:\fitnesse and create a new folder in there called lib

inside lib place the downloaded webfixture jar file.

Now get a copy of selenium from http://selenium-rc.seleniumhq.org/download.jsp

i should point out here that i still use version 092 because there are a few flaky things with the newer versions that could leave you pulling out your hair.

Again, place the jar ....

Okay, so i should mention here that if you are behind a proxy, you need to specify that in your selenium start up command.
Creating a test:

Now you have fitnesse up and running you can browse its pages. As you can see there is some content in there already.

To create a new test suite just type the name of the suite in CamelCase in the address bar your browser, e.g lets say i'm going to call my suite MyFirstSuite, i would type
http://127.0.0.1:8080/MyFirstSuite

into the address bar of my browser.

Fitnesse will tell you the page doesn’t exist and give you the option of creating it (the newer version just gives you the edit window, in which case just paste in the path info below).

Before we can write any tests we have to tell fitnesse where we will be storing our libraries by putting the path information into the suite page. In this example I assume you are putting all your jars (selenium, webtest, dbfit) into a folder called lib inside the fitnesse folder.

In the newly created page (type into the edit box) i would type:

!path lib\*.jar


Then i add the fitnesse wiki mark-up command to generate a contents list automatically (this is just me being lazy).

!contents -R


and now save the page (click the save button).

Note: If you want your SetUp and TearDown pages to be collapsed on your test pages you can define the variables:


!define COLLAPSE_SETUP {true}
!define COLLAPSE_TEARDOWN {true}

in the same page too. If you dont know what that means, just move on, dont worry.


Now we have a suite page we need to tell fitnesse that the page is to be treated as a suite. Click the properties button on the left menu. If the properties button is not available on the left menu, then type ?properties at the end of the url in your browser:

http://127.0.0.1:8080/MyFirstSuite?properties


From within the properties page make sure the "suite" checkbox is checked and uncheck the buttons you don’t want to appear in your left hand menu (I never use the files, refactor, search or versions button, so I turn these off but I enable edit if its not already).

Now we need to create the setup and teardown pages for our suite, again we can just type the names at the end of the url in the browser preceded with a '.' period:

http://127.0.0.1:8080/MyFirstSuite.SetUp


Within the SetUp page we declare the fixture we are using for the test, e.g the WebTest fixture

!|com.neuri.webfixture.WebTest|


The exclamation mark tells the wiki not to parse the text in the cell, otherwise is would look for a page called WebTest and think you want to create that page.

You can then put the startup command for selenium here, so for webtest (refer to the reference you printed out above) we use:


|Start Browser|firefox|With Selenium Console On|localhost|At Port|4444|And Scripts At|http://www.bbc.co.uk/|

Note: you would substitute the values for the selenium console (localhost) and its port (4444) depending on what machine you are driving and how you set selenium up. Here I'm assuming you are running the selenium server on your own machine, and it started on port 4444 (the default). But if you have a bit of savvy you should have just realise that you can drive any selenium instance remotely.

You can now save the SetUp page and create the TearDown page in the same way as we created the SetUp page, by typing the page name into the browser:

http://127.0.0.1:8080/MyFirstSuite.TearDown


We only need to put one thing in here, the instruction to shutdown the browser we opened in the SetUp page. again refer to the reference.

|shutdown browser|


Okay, now we have an empty suite.

Ok. Now lets create the test page, in the same way we created the other pages above, by typing the url:

http://127.0.0.1:8080/MyFirstSuite.TestOne


In here we will add a basic test using the WebTest reference to guide us:

Navigate to a URL during the test (change active page)
|user opens url|http://www.bbc.co.uk/radio2|


Check that the page loads fully within given time (use after user opens url)
|page reloads in less than|10|seconds|


Check current browser URL
|page url is|http://www.bbc.co.uk/radio2/|


Check that page contains some text (regardless of whether text is visible or not)
|element|title|contains text|BBC - Radio 2 - Home|


Save the page.


Edit the pages properties, either by the properties button on the left, or (if the button isn't available) by typing ?properties at the end of the url in the browsers address bar:

http://127.0.0.1:8080/MyFirstSuite.TestOne?properties


You can select the option you want, but make sure "Suite" is not checked, and "Test" is checked. Save the page.

You should now find that your test has the SetUp page and TearDown page included.

If selenium is running you should be able to run the test by clicking "test" on the menu.

There you have it, your first test suite. Try now to add TestTwo to your test suite.

7 comments:

  1. Hi Stuart,
    I am following your tutorial to start up with selenium and fitnesse with java. I have everything in place the only difference is i put the classpath of the jar files with a fixed path: M:\SCHOLION\WEBTESTINGFRAMWORK\fitnesse\lib\*.jar

    I get the following error:

    Missing methods: public TypeOfResult userOpensUrl(Type1 arg1) { } in class com.neuri.webfixture.WebTest



    com.thoughtworks.selenium.SeleniumException: Internal Server Error at com.thoughtworks.selenium.HttpCommandProcessor.getCommandResponse(HttpCommandProcessor.java:124) at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:82) at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:68) at com.thoughtworks.selenium.DefaultSelenium.waitForPageToLoad(DefaultSelenium.java:510) at com.neuri.webfixture.WebTest.pageReloadsInLessThanSeconds(WebTest.java:139) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at fitlibrary.closure.MethodClosure.invoke(MethodClosure.java:31) at fitlibrary.closure.MethodClosure.invokeTyped(MethodClosure.java:24) at fitlibrary.closure.CalledMethodTarget.invokeTyped(CalledMethodTarget.java:67) at fitlibrary.closure.CalledMethodTarget.invokeTyped(CalledMethodTarget.java:82) at fitlibrary.closure.CalledMethodTarget.invokeAndWrap(CalledMethodTarget.java:242) at fitlibrary.traverse.workflow.caller.ActionCaller.run(ActionCaller.java:25) at fitlibrary.traverse.workflow.DoTraverseInterpreter.interpretRow(DoTraverseInterpreter.java:152) at fitlibrary.traverse.workflow.DoTraverseInterpreter.interpretAfterFirstRow(DoTraverseInterpreter.java:64) at fitlibrary.traverse.workflow.DoTraverseInterpreter.interpretInFlow(DoTraverseInterpreter.java:132) at fitlibrary.traverse.workflow.DoTraverseInterpreter.interpretWholeTable(DoTraverseInterpreter.java:101) at fitlibrary.traverse.workflow.DoTraverseInterpreter.interpretWholeTable(DoTraverseInterpreter.java:87) at fitlibrary.DoFixture.interpretWholeTable(DoFixture.java:69) at fitlibrary.suite.InFlowPageRunner.run(InFlowPageRunner.java:34) at fitlibrary.DoFixture.interpretTables(DoFixture.java:42) at fit.Fixture.doTables(Fixture.java:74) at fit.FitServer.process(FitServer.java:68) at fit.FitServer.run(FitServer.java:49) at fit.FitServer.main(FitServer.java:40)

    ReplyDelete
  2. any idea where i am making the mistake.

    regards,
    choe

    ReplyDelete
  3. Hi,

    if you dont include your email address, i cant reply to you :-/

    Stuart.

    ReplyDelete
  4. its tchoesang@gmail.com

    regards,
    choe

    ReplyDelete
  5. I'm getting the same error as the "anonymous" above. Can the solution be posted here?

    ReplyDelete
  6. Hi You are getting the error for one of two reasons.

    1. You have used the method wrongly in your test table cell. it should look like

    |user Opens Url|http://my.server.com/uri|

    The method is a standard WebTest method and navigates to the given URL during the test.

    2. The Webtest jar is not on your classpath, double check.

    regards,

    Stuart.

    ReplyDelete
  7. Don't think this test case works any longer.
    --- Re: errors above - Web Test commands seem to be case sensitive, so make sure the string 'URL' in the two commands are upper cased.

    --- I get an exception that the <title> element cannot be found. Looking at the DOM it is there. Maybe WebTest has changed its locator functionality. Adding the |page contains text| as shown below works fine.

    --- FireFox cannot open the URL. Had to run in IE. Any ideas here?


    |user Opens URL|http://www.bbc.co.uk/radio2|
    |page reloads in less than|10|seconds|
    |page URL is|http://www.bbc.co.uk/radio2/|
    |element|title|contains text|BBC - Radio 2|
    |page contains text|On Radio 2 Now|

    Thanks,

    ReplyDelete