Friday, April 9, 2010

WATIR:IRB: How to work on IRB in ruby scripting

Use Ruby's Interactive Command Interpreter to test out script ideas. You can try out test script ideas interactively with irb.
To start IRB on Windows, select start > Run... and type irb in the Open field. Click OK, and a DOS console should open with a command prompt that looks like this:
irb(main):001:0>
Use the Watir library and enter commands to start driving a web browser interactively:
irb(main):001:0> require 'watir'
=> true
irb(main):002:0> ie = Watir::IE.start "http://www.google.com/"
Or, attach IRB to an existing browser instance using IE.attach or IE.find:
ie = Watir::IE.find(:title, "Google")                 #find by window title
ie = Watir::IE.find(:url, "http://www.google.com")    #find by url
ie = Watir::IE.find(:title,/REGEX/)                   #find by title matching REGEX
ie = Watir::IE.find(:title,//)                        #attach to any current IE instance

Use IRB to Find Page Objects

To find out what objects are on a page you are writing a test script for, use IRB to get instant feedback.
The show_all_objects method is a useful way to identify the attributes of objects you will need to use in a test script.
irb(main):003:0> ie.show_all_objects
-----------Objects in page -------------
text name=test_text id= 11 value= alt= src=
submit name=test_button id= 12 value=Click Me alt= click src=
The objects on the page are shown with their attributes such as name, id, value, alt, src. You can use these attributes to uniquely identify the objects needed for a test script. In our example above, we have two objects. A text field named test_text and a button (submit) named test_button.
The flash method can be used to test whether you have identified the object correctly. The flash method causes an object in the web browser to flash yellow ten times.
irb(main):004:0> ie.text_field(:name, "test_text").flash
=> nil
This would cause the text field with the name attribute test_text to flash yellow ten times. This is an effective way of testing whether you have identified the right object, and if you have used the right attribute. Using this in IRB is a quick way of finding out what objects can be used in a test script before commiting the script to a file.

Why does my code work in IRB but not in my .rb script?

If your code works when you enter it by hand in IRB but fails when you run your ruby script, the usual suspect is timing - perhaps the next element you want to manipulate is not manipulable yet when your snappy script moves on to the next step. Try putting a sleep in to see if that's the problem. If it is, which is often the case, then you can use wait_until{} to verify the presence of a control, text, etc.

How can I save typing in IRB?

If your scripts use classes you can load them in IRB using the "load" command. Consider the following class:
class Dog
  def bark
    puts "woof woof!"
  end
end
Then, in order to use this in IRB, type the following:
irb(main):001:0> load 'dog_class.rb'
=> true
irb(main):002:0> puppy = Dog.new
=> #
irb(main):003:0> puppy.bark
woof woof!
=> nil

No comments:

Post a Comment