Validate JavaScript variables with WATIR

Sunday - 23 November 08

Recently I was required to add 3rd party JavaScript Tracking Tags to an order confirmation page. The JavaScript passed the quantities of products purchased onto the 3rd party.

I wanted to validate the tags using Watir. It is easy enough to access the DOM using Watir and previously I have validated tracking tags placed in an iFrame url using xpath:

Access the iframe element using xpath (returns an array):
frame = @ie.elements_by_xpath("//[@id='my_iframe_id']")

Access the src of the iframe:
frame[0].src

Then simply assert the src to ensure the right data was been passed via the URL.

This time I wanted to actually validate variables set up in the JavaScript. At first I was thinking about how I could access the script tag, then a much simpler idea came to me:

Retrieve the source code!
html = @ie.html

Assert the contents using a regular expression:
assert(html.match(/SOME_PRODUCT="\+escape\(1\)/), "SOME_PRODUCT quantity should be: 1")

Watir resources



Comments:

Want to comment?



simple_captcha.jpg
Enter the text of the image above: