Subscribe to Rahul's Blog        RSS Feed

Avoid using indexes in Watir

Icon 2 Comments
Watir, which stands for Web Application Testing in Ruby, is an open source library for doing exactly what its name says - testing web apps. I've been using it for a couple of months now and I'm mighty impressed with it. Two thumbs up for the development team :^: . However like in any programming language and/or library, its easy to use bad habits, same case being applicable to Watir indexes. They just seem so easy, so tempting - but the hard fact remains that if you take the easy path now you're going to pay for it in the end. Let me elaborate on the point a bit.

Suppose you're testing the user-registeration page for your company's web application. From the front-end of the page, you'll immediately see that the first two text boxes are 'First Name' and 'Last Name'. Without diggin deeper into the HTML source of the form, you quickly whip out the following lines in your Watir script to put dummy values in the registeration page,

$ie.text_field(:index, 1).set("Monty")
$ie.text_field(:index, 2).set("Python")

Of course it'll work. Since you know that the first field is for taking the first name of the user, you simply use its index to access the text field. Likewise for the last name. Imagine however, your not-so-smart architect wants to put the field 'Middle Name' after RC1, what do ya do? One way is to see where does the text field for middle name exists (most probably between the first name and the last name fields) and update your Watir scripts accordingly. This is tolerable if you used indexes for only the two fields mentioned above. But if you did this for 50 text boxes, you'd have to increment the indexes for 50-1=49 text fields. Not very appealing I know. Rather than pulling your hair out at a later stage, you should have used some identifying attribute for the text fields. For example,

$ie.text_field(:name, "first_name").set("Monty")
$ie.text_field(:name, "last_name").set("Python")

Now even if you put another text field in between the above two, all you will need to do is add another line to your script, say something like,

$ie.text_field(:name, "middle_name").set("TheSnake")

Sure it requires you to actually go through the HTML source of the page, but hey if you're testing it you might as well do it right. Plus it'll save you a lot of headaches later. The above tip applies to all form fields and not just text boxes.

2 Comments On This Entry

Page 1 of 1

knownasilya Icon

19 September 2007 - 12:40 PM
I first started off that way but I have learned this technique since then. Have you heard if the firefox version of watir is out yet?

rahulbatra Icon

20 September 2007 - 12:57 AM
Well, the 1.1 gem of Firewatir is out now. Was released around mid-June if I recall correctly, and seems to be fairly active. The lead developer, Angrez, is always available on the mailing list.
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)