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;
}
}
}

Put Cucumber test results online quickly with Sinatra

Finding a way to make your test results readily available to those interested makes life easier for all involved. Doing this in an an always-on automated fashion is even better. For a long time I happily used Ruby on Rails to do this, but Ruby on Rails is designed for a lot more than this simple broadcast. Sinatra is a web “micro framework”, which makes it easy to do quick simple tasks, as well as build applications from the ground up without being forced to use MVC. Let’s use Sinatra to display a HTML file that has been output by a Cucumber test run.

To get Cucumber to to output results in a nicely formatted HTML page, use the following to run the test:

cucumber Blah.feature –format html –out Blah_result.html

Install sinatra with “gem install sinatra

Now in the same folder as your result file, create a file, lets call it sinatra.rb

Add the following lines:

require ‘rubygems’
require ‘sinatra’

get ‘/’ do

File.read(“Blah_result.html”)

end
On the command line run : ruby sinatra.rb
You should get :

== Sinatra/1.0 has taken the stage on 4567 for development with backup from WEBrick

Now browse to http://localhost:4567 to view your html file.
By replacing localhost with your ipaddress, anyone can view the latest test result.

Watir Webdriver tutorial

This is just a quick tutorial on the slight change in syntax required to use Webdriver with Watir for testing on multiple browsers. No longer do you need to use FireWatir, SafariWatir or ChromeWatir. The current drivers are ChromeDriver, InternetExplorerDriver, FirefoxDriver. Let’s get started.

So previously for IE you would have begun with:

require ‘watir’
@browser=Watir::IE.new

now all you do is:

require ‘watir-webdriver’
@browser = Watir::Browser.new(:ie)

or

@browser = Watir::Browser.new(:chrome)
@browser = Watir::Browser.new(:firefox)

The rest is the same as before.

That’s it!!
Have fun. try testing on three browsers at once….

Cucumber and Watir – A quick tutorial for testers

There tends to be a strong ongoing association between Agile and Open-Source automation tools today. Many make good inroads on the attempt to connect the elaboration of requirements with automated tests, step for step. This way, the test is performing as closely as possible, what the client actually asked for, or agreed in the elaboration process.
One such tool is Cucumber. It is designed to allow you to execute feature documentation written in plain text. In this article, I will detail a concise example, hopefully demonstrating the simplicity and effectiveness of this tool as well as value to the agile methodology.

Prerequisites: It is assumed that before using this example you have the following Installed:
• Ruby
• Watir gem for Ruby
• Cucumber gem for Ruby

I will use a simple example that uses the Google search engine in this case:
Let’s say the user story is something like:

‘When I go to the Google search page, and search for an item, I expect to see some reference to that item in the result summary.’

Cucumber uses the keywords “Given”, “When”, “And” “Then” to build this into useable syntax.

So I create for example, a file, called GoogleSearch.feature containing:

Feature:
‘When I go to the Google search page, and search for an item, I expect to see some reference to that item in the result summary.’

Scenario:
Given that I have gone to the Google page
When I add “cats” to the search box
And click the Search Button
Then “cats” should be mentioned in the results

Scenario:
Given that I have gone to the Google page
When I add “dogs” to the search box
And click the Search Button
Then “dogs” should be mentioned in the results

Now I create a file GoogleSearch.rb containing the following

require “rubygems”
require “watir”

Given /^that I have gone to the Google page$/ do
@browser=Watir::IE.new
@browser.goto(“www.google.com”)
end

When /^I add “(.*)” to the search box$/ do |item|
@browser.text_field(:name,”q”).set( item)

end

And /^click the Search Button$/ do
@browser.button(:name, “btnG”).click
end

Then /^”(.*)” should be mentioned in the results$/ do |item|
@browser.text.should include(item)
end

This gives us executable code for every Given, When and Then statement we have used.

To run it we issue the following command line

cucumber GoogleSearch.feature

or for a nice html formatted result

cucumber GoogleSearch.feature –format html –out result.html

Install Firewatir on Ubuntu – 5 easy steps

In a terminal window issue the following commands one by one

apt-get install ruby
sudo apt-get install ruby1.8-dev
sudo apt-get install rubygems
sudo gem install firewatir

Install Firefox ssh plugin

http://wiki.openqa.org/display/WTR/FireWatir+Installation#FireWatirInstallation-2%29InstalltheJSSHFirefoxExtension

Firewatir – Slow on Ubuntu – one answer

When you install Firewatir on Ubuntu you will notice that text entry takes way to long. I needed to solve this quickly or walk away from the idea of using Ubuntu for web testing. I found a quick fix after scouring forums. It worked. I am aware that the right way is to inform the makers and let them make improvements in time.

in var/lib/gems/1.X/gems/Firewatir1.6.x/lib
backup htmlelements.rb
edit htmlelements.rb

replace all of def doKeyPress with

def doKeyPress( value )
@o.value=value
fire_key_events
end

Dynamically build and run a QTP batch file using Ruby

I’ve used QTP a lot without Quality Centre. That means spending a lot of time working with the Multitest Manager. With long lists of tests to run, you either have to browse to and add test by test. You save your batches to files with the .mtm extension, so you could also edit these in notepad , adding lists of tests. Neither of these approaches is quite dynamic enough. I used a Ruby script to gather test names, then build and run a disposable batch file. Assume “jobarray” contains test names( as in the QTP test folder name). Try this script for size:

puts “Creating Multitest Batch File”
testlocation=”C:\\Regression\\tests\\”file=File.new(“batch.mtm”,”w”)
file.puts(“[Files]“)
file.puts(“NumberOfFiles=”+jobarray.length.to_s)
i=0 .. jobarray.length-1

i.each{|element|

file.puts(“File”+((element+1).to_s)+”=”+testlocation+jobarray[element]+”;1″)

}
file.puts(“[Report]“)
file.puts(“CreateReport=Yes”)
file.puts(“DefaultLocation=Yes”)
file.puts(“ViewReport=Yes”)
file.puts(“ReportHeader=Yes”)
file.puts(“RunStartedHeader=Yes”)
file.puts(“RunEndedHeader=Yes”)
file.puts(“ReportLocation=Yes”)
file.puts(“IterationsHeader=Yes”)
file.puts(“[Mail]“)
file.puts(“Subject=Test results from batch run.”)
file.puts(“Message=Test results will automatically be attached to this email if created.”)
file.close

exec(“\”C:\\Program Files\\Mercury Interactive\\QuickTest Professional\\bin\\MultiTestManager.exe\” /TB C:\\Regression\\batch.mtm /LOG /SUMMARY”)