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

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

Watir and centralised XML reports using ci_reporter and Ubuntu

After building a reliable( and free)  bunch of  regression test machines with Ubuntu and Firewatir I had to look at a way of collating reports from each. I was already using ci_reporter for single instances and I like the XML output. I had a windows machine nearby that was running a Ruby on Rails reporting page, so I set up an automatically mounted(on boot) samba share that had each Ubuntu box with a device called xmlreports that looked right at a share on the webserver.

cd media
sudo mkdir xmlreports

in etc/enviroment i added CI_REPORTS=/media/xmlreports

 

sudo apt-get install smbfs

in etc/fstab I added  //10.11.10.55/C/dash/public/reports  /media/xmlreports smbfs

My Watir Test template

I use this template for my current framework which uses rake to run the tests and ci_reporter to generate results:

require ‘rubygems’
require ‘test/unit’
require ‘watir/testcase’
require ‘watir’
require ‘ci/reporter/rake/test_unit_loader’

class Test_example <Watir::TestCase

def setup
      @Browser=Watir::IE.new
      @Browser.goto “http://www.site.com
 end

 def test_searching
 @Browser.text_field(:id, “searchTerms”).set(“blah”)
  @Browser.button(:id, “go”).click

#Check Results

assert(@Browser.text.include?(“blah blah”))
  end

 

def teardown
   @Browser.close
 end

 
end

My Firewatir on Ubuntu 9 setup

I’ve had to do this numerous times and absolutely had to create a list of steps. Without a list, there is always something I should have configured and lost time through trial, error and headscratching. Here’s my list of steps to build an Ubuntu Instance that runs test in the Test-Unit framework and creates XML result files using ci-reports:

1. Install Ruby:

sudo apt-get install ruby

sudo apt-get install ruby1.8-dev

2. Install RubyGems

sudo apt-get install rubygems

3. Install FireWatir

sudo gem install firewatir

4. Install Test Unit

sudo gem install test-unit

5. Install ci reports

sudo gem install ci_reporter

6. Install rake

sudo apt-get install rake

7. Install Firefox ssh plugin

get it here:

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

8. Turn off  browser.sessionstore in Firefox

This is the bit that asks you if you want to restore your last session when you start ff. Not good for an unattended automated test.

In the address bar type about:config, Enter, you’ll get a warning page, click through:

set browser.sessionstore.enabled to false

9.Turn off automatic updates in Firefox

Again, another potential interuption to an unattended test.

10. Turn off screen saver.

This can make it difficult to use the display for running the tests.

11. Set up a cron task
e.g. export DISPLAY=:0.0 && gnome-terminal –execute rake (provided rakefile is in the home folder- otherwise give path)

Firewatir on Ubuntu is slow to add text to text boxes. See my other post on how to make this faster.