Archive for January, 2008

What to do in a Watir test if you don’t have a unique id

January 21, 2008

OK, you don’t have ids on the controls you need to access.  Now what do you do.  I was faced with this situation.  I needed to verify that one menu item followed another.  Neither of the menu items had an id.  I could simply use indicies on the links.  That would have made for a very brittle test (one that breaks easily).  I happened to notice that the parent cell (TD) had a unique class (sideNav).  If I could associate my test with that cell, it would be a bit more stable.

Within the cell was a list structure (UL), with each list item (LI) followed by a link that contained text.  I was supposed to make sure that the link with text of “Item-of-Interest” was followed by the link with the text “Following-Item”.  The structure of this list was very regular: list item followed by a link, followed by text, followed by the next list item.
<TD class=sideNav>
    <UL>
       <LI>
         <A>
            #text
       <LI>
         <A>
            #text
       <LI>
         <A>
            #text
       <LI>
         <A>
            #text
       <LI>
         <A>
            #text
       <LI>
         <A>
            #text     <– Item-of-Interest
       <LI>
         <A>
            #text     <– Following-Item
       <LI>
         <A>
            #text
If I just used indicies to locate the links, an additional link to Item-of-Interest in front of the list could break the test.  That is not an unusual situation on our website.  In addition, using the “sideNav” class provided some additional insurance that I was testing what I thought I was testing.  I decided I could locate the cell with class=sideNav, loop through the list until I found the first text I was looking for and test the next link to see that it had the “Following-Item” text.  The code below is a sample of how to implement the test.

i = 1 
while $br.cell(:class, "sideNav").ul(:index,1).li(:index, i).link(:index, 1).exists? 
   if $br.cell(:class, "sideNav").ul(:index,1).li(:index, i).link(:index, 1).text =~ /Item-of-Interest/i 
      if $br.cell(:class, "sideNav").ul(:index,1).li(:index, i + 1).link(:index, 1).exists? 
         assert($br.cell(:class, "sideNav").ul(:index,1).li(:index, i + 1).link(:index, 1).text =~ /Following-Item/i) 
      end 
   end 
   i += 1 
end

The statement that begins with “while” and ends with “.exists?” is there to prevent the test from failing with an exception if the combination does not exist.  I loop through the list items with an index so that once I find the first text, I will look for the “Following-Item” text at index+1.  Again, I do an “exists?” test before I do the assert to keep the code from throwing an exception.

It turns out that this worked out OK for testing this condition.  Although it is not the best situation, using the cell class to identify the area was better than just using indicies.

Advertisements

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

January 9, 2008

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, https://jimhmatthews.wordpress.com/2007/10/28/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’)
     end
and
     def checkout_button
       ie.button(:id, “checkout_button”)
     end
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.