Posts Tagged ‘watir’

Why Do You Use Watir?

April 15, 2008

Bret Pettichord, lead Watir developer just announced on the watir-general discussion list that he’s taken a new job “working full-time with Prototest and Pete Dignan (CEO of Prototest) to accelerate the development of new versions of Watir.”

One of his top priorities is getting FireWatir and Watir in sync with each other – something I’m very excited about.

He just asked for stories regarding “Why do you use Watir?

I haven’t blogged for a while, and I’ve meant to tell this story here for some time, so here goes:

I had just started a new job as the first dedicated tester at a company with an established product. In my first few months, the testing that made the most sense to do was hands-on, sapient testing…but I’d heard some buzz about a new open source tool under development, nearing a 1.0 release, and I was itching to try it.

One afternoon a report came in from the field: Someone had been attempting to import members to a project, and had seen “Permission Denied”…with someone else’s name up in the right hand corner! Had their members been imported into some stranger’s project? They tried again and succeeded, but were understandably spooked.

I asked around, “Have we ever heard reports like this before?”

“You know, now that you mention it, once or twice a year someone makes a similar complaint. Each time, a coder has poked about in the code, changed something, and said ‘I bet that’ll fix it.'” The problem was no one had ever succeeded at reproducing the problem, and so no one had a way to test if it was fixed.

I had a hunch that there was some concurrency / race condition going on, and that Watir might help me diagnose it. Watir turned out to be simple enough that in less than I day, and without any previous ruby experience, I wrote a script that performed these actions over and over, concurrently, as 5 different users. Lo and behold, I could consistently reproduce this issue in under 30 minutes.

The coders I worked with were thrilled to have a consistent repro case, and after a few false leads, they had a fix.

Since then, I’ve used Watir for tasks including concurrency testing (above), creating large data sets, and measuring end-to-end page performance across revisions, not to mention regression testing. Inspired by some of Harry Robinson‘s work doing high-volume, semi-random testing of Google directions using Watir, my latest project is venturing into using Watir for some automated model-based testing. Along the way I’ve learned a real language (not a vendorscript) and am part of a friendly and thriving community of users and contributors…to whom I’m very grateful.

Advertisements

Creating Methods On the Fly…And Bugs in Google Phonebook

January 27, 2007

I wrote the following Watir script as a demonstration of how to generate new methods on the fly in ruby.

See (at the time of this writing) there’s a bug in Google Phonebooks, where if you do a search that generates pages of results (e.g. “rphonebook: j smith, ny“) and then quickly click to some of the later pages, you will see:

Google Server Error

I wanted to write a script that would randomly generate search strings – some that would yield many results, like “j smith, ca” and others that would yield few if any, like “z glinkiewicz, ak”.

I wanted the script to be able to easily run an arbitrary user-defined number of iterations…and I wanted each iteration to have it’s own assertion, so that if one failed the rest would still run. In ruby’s test::unit (which Watir gets it’s assertions from) each assertion needs it’s own method…and that led me to generating methods on the fly.

My inspirations to play with semi-random automated tests were Chris McMahon and Paul Carvalho, and I got help with my syntax through a speedy answer to my question from Brett Pettichord on wtr-general.

Here’s the script I wrote. Feel free to question me about why I did what I did or to propose improvements (I consider myself a beginning automator). In the mean time, without further ado…here’s the code:

#
#   This script creates any number of randomized Google phonebook searches,
#   then quickly cycles through each page of results, looking for server errors.
#
#   Written to reproduce and explore a bug that I found in the Google phonebook,
#   and to show a watir script of mine that's completely non-proprietary and can
#   be run against publicly available software.
#

#$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..') if $0 == __FILE__
require 'test/unit'
require 'watir'

class TC_GooglePhoneBook < Test::Unit::TestCase
  include Watir
  $count = 15   # set number of iterations here

  def setup
    $ie = Watir::IE.new
    $ie.bring_to_front
    $ie.maximize
    $ie.set_fast_speed
  end

  # data arrays
  first_initial = ('a'..'z').to_a
  last_name = ["allen","brown","glinkiewicz","johnson","jones","mason","ross",
                    "sanchez","smith","wieczorek","williams","woo","wolfe"]
  state = ["ak","az","ca","fl","ma","mi","mt","nv","ny","wa"]

  $count.times do |count|

    fi = first_initial[rand(first_initial.length)]
    ln = last_name[rand(last_name.length)]
    st = state[rand(state.length)]
    method_name = :"test_#{count}_#{fi}_#{ln}_#{st}"  #dynamically create test method
    define_method method_name do
      search_string = fi +" "+ ln +" "+ st
      $ie.goto("http://www.google.com")
      $ie.form( :name, "f").text_field( :name, "q").set("rphonebook: #{search_string}")
      $ie.button( :name, "btnG").click
        i = 1
        while $ie.link( :text, 'Next').exists? do
          $ie.link( :text, 'Next').click
          i = i + 1
          assert_no_match( /Server Error/, $ie.text, "Page #{i} contains a server error." )
        end #do
    end #method
  end #N.times do count

  def teardown
    $ie.close
  end

end #TC_GooglePhoneBook