Dashboard > Watir > ... > Simple Examples > Dynamic Method Creation
Watir Log In View a printable version of the current page.
Dynamic Method Creation
Added by Jeff Fry, last edited by Alister Scott on Sep 29, 2008  (view change) show comment
Labels: 
(None)

#
#   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.
#

#$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

Site running on a free Atlassian Confluence Community License granted to OpenQA. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.6 Build:#812 Aug 06, 2007) - Bug/feature request - Contact Administrators