Thursday, July 31, 2008

Geeking Out

Okay... I'm so damn pleased with myself, I can't help posting about it. I've had a little downtime at work for the past few days, so I spent some time writing up regression tests using the Selenium IDE for my websites's tracking tags. I didn't want to rely on just a static set of pages/urls to use in my tests, so it was a little tricksier than just saying "this page should use the following values, assert that they're there." Instead, I needed to pull the values off of the page I hit, and then, depending on the page type, compare what was in our tracking tags, and verify they matched.

... but wait, it was worse than that, because of two things:
1. The format for the tracking tags doesn't always match the format for the data on the page (i.e., Rock & Alternative on the page = rock_n_alternative in the tag).
2. In some cases, there was nowhere in the html source of the page where I could find the expected data. For this, I needed to actually pull the data out of the database, store it in a variable, and then compare the variable to the tag.

So... for number 1, I used some javascript expressions to transform the data. If, as an example, Rock & Alternative was in the h4 tag on the page, my script went like this:

Command: storeText
Target: //h4
Value: navGenre

Command: storeExpression
Target: javascript{storedVars['navGenre'].toLowerCase().replace(/( |\-)/g,'_').replace('&','n')}
Value: genre

That got me the expected format for the genre in a 'genre' variable that I could compare against the tag in the tracking code.

For number 2, the record ID for each item on the page is the last piece of the url for the page. I'm in luck that my db can be queried using an http request, so using the following regular expression, I was able to cut everything but the last piece of the url from the page to get the record ID I needed, and store it in a variable:

Command: storeLocation
Target: curPage

Command: storeExpression
Target: javascript{storedVars['curPage'].replace(/.*\/([^\/]{0})/,'')}
Value: assetID

Then, I plugged that variable into an url, like:

Command: open
Target: http://my.db.serverurl/select/?q=select+something+from+mytable+where+assetID%3D+${assetID}

(this is obviously not the actual url, because I think I'd get in trouble if I just passed that out.)
Once I hit that query, my results are in an html table, so it's not hard to construct an xpath to pull the cell from the table I wanted and store it in another variable, then go back to my test page and verify that the data matched in the tag.

Ta-da! I can now do lookups in my db for anything I want, store the data in variables, and then compare that data to the values on my page. Of course, it'd be easier if they stored that data in the source of the page somewhere, but I'd rather have dev pushing new code than rewriting existing pages just to make my regression tests easier.

No comments: