A student of mine really impressed me with a recent question.  I had tasked him with automating the front end of a music retailer website.  My student, Exuar, showed me some of the UI automation.  But he asked me about something he was trying to figure out.

The music retailer site had a “store locator” option.  He would automate the UI to pass in a zip code, but he wanted to verify that the top listing was closer then the second listing.   I thought this was pretty advanced and I was very pleased to see he was coming up with such great ideas!

Using Watir and some Ruby I was able to help him and get it validated.  Here’s how we got it working:

The store location data was listed as a Unordered List in a div.  Each store was in it’s own UL.  So here’s what I did:

store_location = @Browser.ul(:id=>”searchResultList”).li(:index=>0).div.a.text

I set a variable to be a Watir::Browser going to the UL of id searchResultList and picked the first LI (index 0).  Then we grab the div within it, and the a within that div.  Finally we use the .text method to capture the value there.

When I “puts store_location” I would get output like: “West LA (4.34mi)”

Awesome!  That was the first part, isolating the text itself.  Now we need to use RegEx to isolate the miles.  The code below is what I used to grab the miles.  I had to look up the Ruby RegEx syntax.  It’s a bit different from Groovy.  Matching in Ruby was a bit easier.  I just used the Ruby .match method to match the RegEx for the digits to the output.

miles_of_first_store = /([0-9]).([0-9])([0-9])/.match(store_location)

Now if we “puts miles_of_first_store” we get 4.32.  We repeated this process to get the value for the second store in the listing… just modifying the li index to equal 1.

At that point, we had two store distances.  We now need to assert that the first one is less than the second.  Here we have a problem.  The RegEx returned wouldn’t allow comparisons, as it was a string.  We needed to convert it to a decimal.

To do this, I added a step:

first_store_string = “#{miles_of_first_store}”
second_store_string = “#{miles_of_second_store}”

I formally made the result a string.  Wasn’t it already a string?  It was but wasn’t allowing a conversion to decimal.  By formally making a string with the values of miles_of_first_store and miles_of_second_store, it became a formal string that now allowed me to do a .to_f (convert to float.)

Then I could do this (which passed btw.):
assert first_store_string.to_f < second_store_string.to_f

Exuar, being the good student, also validated that assert first_store_string.to_f > second_store_string.to_f correctly failed.


Leave a Reply

Your email address will not be published. Required fields are marked *