Wednesday, February 25, 2009

Customizing watir-console

Watir comes with a really cool command-line tool called watir-console. To start it, just type watir-console at a prompt (assuming you have the ruby\bin directory in your path). It will bring up an irb prompt, but this isn't just any irb prompt. It has already required the watir module, and offers some cool features for interactively using Watir, such as tab completion of Watir commands.

One thing you can do to make watir-console even more useful is to write a startup script. Here are the steps:

  1. Create a helper script and put it in one of the Ruby library directories (such as C:\ruby\lib\ruby\1.8). Let's call it helper.rb.
  2. Create a .irbrc file (just a text file) and store it in \ruby\bin. This is actually a Ruby file, and can have any Ruby commands you want to start up when Ruby starts. For our purposes, your script should 'require' the script you built in step 1.

That's it! Now, helper.rb (and its methods and constants) will be available in watir-console whenever you run it.

"What should I put in helper.rb", you ask? I'm a believer in examples, so here are a few examples of methods I created:

  • A 'login()' method to start a browser, go to the login page for my web app and log in. It returns the IE object, so it can be called from watir-console as "instance = login()". This allows you to then access "instance" as the IE instance to be manipulated.
  • Now you can run different methods to do different things. For example, my application does queries, so I have a 'new_query()' method that simply navigates to the page where I can create a query. It is accessed by calling 'new_query(instance)' from watir-console.
This makes Watir and watir-console indispensable in interactively testing web applications.

2 comments:

  1. Thank you for the reminder about watir-console. Bret and Charley covered something like this at a tutorial at Agile 2007, and I promptly forgot about it. Thank you for the example, I am going to try it!

    ReplyDelete
  2. no problem lisa...i am sure all this stuff is in the docs somewhere too...but sometimes really cool features for testers can get lost in the great mass of words that is documentation :-).

    ReplyDelete