04 November 2012

Adding Randomness in Tests

Why Randomness?


When testing a UI and having a large selection of dynamic links, it can be helpful to get an idea of break points with the tests, as well as the code.  In other words, if you only write tests for specific points, you may miss some situations and scenarios that were not thought through.

My Google Example

I used Google.  I created some tests that:
  • go to google.com
  • enter a search term
  • click a random element on the page
To help with this example, I created a few tests in  order of complexity.   The first test simply goes to google, enters a term from a table and then gets results.  At the results list, it clicks the first result in the list.

Google's results tend to be hard to directly grab.  They have a list of results.  But to get to them (using WATIR notation) I came up with this (I also had to put a wait_for_present before this to wait for the element ol#rso to load.)  ol#rso is the ordered list of results, so I target it like this:
 @browser.ol(:id, "rso").li(:index=>0).div.h3.a.click

I'm grabbing all the li's and treating them like a list or array.  I use index 0 (WATIR notation) to find the first result row.  Then after getting it, I grab the chain of: div.h3.a and click it.

Adding Randomness

So far there's no randomness here, but the first test gets us going with results and clicking through on them.  Now for the randomness.

It's easy.  I counted the results per page, and came up with trying to get 16 choices.  My solution was to do this:
  @results = rand(15)
  @browser.ol(:id, "rso").li(:index=>@results).div.h3.a.click


Basically it's the same thing as before, except I added a instance variable of @results.  It just comes up with a random number up to 15. 

So now when @results is passed in as a index value it just picks the random row to click.

More Complexity

But lets say we want to take it further and randomly pick a search result page, and then pick a random result on the page. 

  @nav = rand(1..7)
  @browser.table(:id, "nav").tbody.tr.td(:index=>@nav).a.click


In this case I created a instance variable that picks a random result from a range of 1 to 7.  0 is not wanted, because by default, the user is already on the first result page (0.)  So no reason to click on it.  Just want to click from 1 to 7. 

After the results page is loaded, then I rerun the logic to pick a random result from the page.

Issues

I ran into some issues.  The first issue was that the rerunning of the random result on the page.  When I tried calling that again, it wouldn't wait properly.  So I had to recreate the same logic and call it again.

No comments:

Post a Comment