Do yourself a favor (use id’s for Watir testing)

When you use Watir to test web pages, the best way to identify buttons and text fields is with ids.  The problem may be that the controls you want to test do not have ids.  The solution to this may be political rather than technical.  I know that developers and testers usually want to stay out of politics.  I have some ideas that may help you deal with the situation.

The first soultion involves finding a developer or developers who are willing to help you make it easier to test code.  There are some out there.  Usually they have worked in an environment where there is an emphasis on testing like Test Driven Development (TDD).  It could be that it is a forward thinking developer that realizes the advantages of having a good set of test suites for code they are developing.  If you can find developers who share this interest with you, it is the best way to procede.  Getting things done informally is usually the easiest and fastest way to getting things done.

If that does not work, the next thing to try is to sell your manager that it is to their benefit to get the developers to help you out.  Now I know you don’t like to “sell” anything.  The truth is that managers like things that save them money, make them look more productive, or make them look smarter to their managers.  Conviencing your manager to get the developers to add ids to the web pages will do this in the following three ways.

First, it will make the tests cheaper to develop.  You can change “cheaper” around to whatever your manager’s hot buttons are.  Basically it means that you can create more tests in the same amount of time or that you will not have to hire more testers. You will probably be able to start testing earlier because tests are easier to write.  You get the picture. You will have more tests earlier.

Second, tests will be less brittle.  If you are new to the testing world, a brittle test is one that breaks easily.  In web testing, this usually happens because you have to rely on tag structure based on position rather than an id that stays with a tag regardless of where it moves on the page.  If you end up testing while developers are still coding and you can use ids, your tests will not break as much.

Third, it will be cheaper to maintain your tests.  Undoubtedly there will be bug fixes or new features added to the original application.  Your tests will have to be modified. It will be easier for someone else to understand and maintain the tests if you use ids.
I really cannot stress enough how much time you will save and how much easier your life will be as a tester if you can get ids on the controls.  It would certainly be worth buying a developer a lunch or two if you can convence them to add ids for you.

If you look back at my blog post How to start writing web test scripts with Ruby and Watir,, you will notice that I had text_fields, ie.text_field(:id, “billing_name”) and a button, ie.button(:id, “checkout_button”) that were identified with a unique id.  Now if these move around on the page, my tests will not break because I can still find them with the id.
     def enter_billing_information
       assert(ie.text_field(:id, “billing_name).value == ”)
       assert(ie.text_field(:id, “billing_address”).value == ”)
       assert(ie.text_field(:id, “billing_city”).value == ”)
       assert(ie.text_field(:id, “billing_state”).value == ”)
       assert(ie.text_field(:id, “billing_zipcode”.value == ”)
       ie.text_field(:id, “billing_name).set’Joe Tester’
       ie.text_field(:id, “billing_address”).set’123 Address’
       ie.text_field(:id, “billing_city”).set’Austin”
       ie.text_field(:id, “billing_state”).set’TX’
       ie.text_field(:id, “billing_zipcode”).set’12345′
       assert(ie.text_field(:id, “billing_name).value == ‘Joe Tester’)
       assert(ie.text_field(:id, “billing_address”).value == ‘123 Address’)
       assert(ie.text_field(:id, “billing_city”).value == ‘Austin’)
       assert(ie.text_field(:id, “billing_state”).value == ‘TX’)
       assert(ie.text_field(:id, “billing_zipcode”).value == ‘12345’)
     def checkout_button
       ie.button(:id, “checkout_button”)
If you are going to the trouble to add unique ids, you might as well make them descriptive about what they reference.  Hence I chose “billing_name” and “checkout_button.”

Next time I will talk about what to do if you have to test a page that does not have ids and you cannot get them on there anytime soon if at all.


Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: