Talking testing, automation... and anything else.

Jan 13

Watir-Webdriver Sort Test

watir.logoWhile convalescing this past weekend from some kinda cold/funk that’s been making the rounds, I spent some time playing with Watir-Webdriver. I really like Watir and have heard good things about Webdriver, so I wanted to see for myself. As an exercise, I decided to port my Sahi sort test from a previous post… here’s what I came up with.

Do feel free to comment and/or make suggestions!

Jan 13

Selenium-WebDriver vs Sahi

selenium.logoJari Bakken has a couple of nice example tests that show some of the differences between a Selenium-WebDriver test and a Watir-Webdriver test. I thought I would create another example to show how the same code could be written in Sahi.

Both of the following scripts perform the same test. The test itself is simple:

  1. Go to the Google Translate page
  2. Click the Detect Language button
  3. Select Norwegian as your language
  4. Log the button’s text
  5. Enter the word “ost” into the text field
  6. Verify “cheese” is the returned translateion

Selenium-Webdriver Example…

Sahi Example…

The difference is pretty dramatic… at least in test size. Sahi handles all your waits for you so there’s no need to clutter up your tests with wait code. Accessor code is also less verbose in Sahi. Overall, I would argue the Sahi code is much more readable but code beautry, like beauty in general, is in the eye of the beholder. I.E. your millage may vary…

Note: I used an assert instead of just logging the returned translateion… same dif

UPDATE: I’ve also added Geb to the fray!

Jan 13

Verify Sorting With Sahi (or any tool really)

4800819674_3cf963deaa_bI recently found a bug when sorting table columns, while running IE9. Ignoring the obvious fix, I wrote up the bug and then as I’m a fan of doing, I wrote a failing automated test to test it (Defect Driven Development!).

Testing sort proved a bit tricky… I thought I would share the results to perhaps save others same pain. I would also not be surprised to find a more elegant solution out there. If you have one, do feel free to share!

My example is in Sahi but it should be easy to transfer to your tool of choice. The gist is:

  1. Sort your column
  2. Collect all the elements in the column in an array
  3. Copy the array and sort the copy using javascript’s sort()
  4. Compare the two arrays

And here’s the Sahi code…

First off, thanks to for providing an example for my example!

The script starts by navigating to and clicking on the Name table header to get our initial sort of that column.

Then we collect each element in the Name column, in table0 and store them in an array, $appSortedValues. To iterate through our loop, we get the number of rows in table0 by counting the number of rows and subtract 1 for each table header.

Now we need a copy of our $appSortedValues array but in Javascript, you can’t just set a new array from our existing array like so: $jsSortedValues = $appSortedValues; . This will actually create a reference to our original array; not what we want. Instead we use the slice() method to select elements 0 through the end of the array and put them all in our new array, thus copying it.

Finally, we use javascript’s sort() method to sort the copy of our array, $jsSortedValues. But the sort() method has a little wrinkle; by default, it sorts alphabetically and is case sensitive. This is unlikely to be how your application’s sort works… but luckily, you can roll your own sort filter by passing a function as an argument to the sort() method. In our case, we want it to be case-insensitive, hence our function caseInsensitiveSort.

Now we have two arrays and can simply use Sahi’s _assertEqual method to verify both arrays are sorted in the same order.

That’s it!

Apr 12

Accessing Browser Objects Using Sahi

Sahi is a great automation tool for testing web applications. While Similar to Selenium, Sahi offers many notable advantages over Selenium, including some that were deal breakers in my current situation (IE support for instance).

Sahi also offers some ways to access page objects that are just flat out awesome. I’ll discuss a few here, specifically strings, indexes and it’s built-in Browser Accessor API _under.

Consider the following HTML table…

Last Name First Name
Deng Luol
Rose Derrick
Gibson Taj

Suppose you had a story that called for you to click a link from the table above. Simple enough… in Sahi you could write something like this:

Yup. This code does indeed open Lu’s Wikipedia page as expected. It’s using the link text to access the page’s object (not an xpath). Handy for sure but what if you had other sites to test that contained different data in this same table (hopefully not any Miami Heat Players)? Or maybe these links are commonly deleted by other scripts you’re running? The code above would fail.

No problem. Sahi indexes all page objects which allows you to access the “nth something” on the page. In this example, we could just click the first link. And in case there are other links elsewhere above, we’ll be more specific and make use of one of Sahi’s fantastic relational accessors, _under. So…

Lu’s Wikipedia page again appears! What’s happening here is we’re clicking the first link (indexes are zero-based a thus start with zero) under the table header “Last Name”. Thanks to the indexing of the links, we could also iterate through all of the links via a simple for loop.

But now maybe we’d like to take this up a notch and inject some randomness that will help broaden our testing scope and perhaps improve our chances of finding a bug (the purpose of testing after all). And maybe we’ll also make it a function to help keep our automation more readable. So perhaps something like…

This function uses Sahi’s _count method to count the number of cells that have an id of lastname. (Note: because the results of _count are NOT zero-based (starts with 1) it returns “3” so we just subtract 1. We then use Sahi’s method _random to get a random number between 0 and $numPlayers (2). Now we call the same code as in our previous example to click one of the player’s links at random, under the table header “Last Name”.

And now any one of my Bulls player’s Wikipedia pages is displayed when running the script! These are just a few of the ways Sahi lets you get at page objects… there’s a bunch more including _leftOf and _rightOf, new to Sahi Pro 4.2.

Jan 12

Screencasting With Jing

The folks at TechSmith make a nice screencasting tool called Jing that I’ve been using for quick tutorials and various documentation. It comes in two flavors, free and pro, but interestingly, I’ve found the free versions limitations to almost be features.

For example, it only records 5 minutes of video. Personally I find that tutorials longer than 5 minutes to be too long anyway. If you’re rambling on for longer than 5 minutes, your listeners are gonna move on.

Which brings me to another limit/feature… you can’t edit the video. I also like this because I want to make quick tutorials… not monkey around with video editing. This forces the issue.

The free version only records video to swf (pro version does mp4) which again, is kinda nice. You can drop the resulting swf onto any modern browser (with flash installed) and the video will play. No additional apps required. It’s also a *snap to embed in a web page (see below). Additionally, it runs on Mac & Windows, has nice stingers and can also do screengrabs with markup.

Now, that said, if you’re looking to build a slick, more professional screencast, Jing will fall short. It’s strengths are agility and functionality… pretty is another apps job.

*Update… okay, so there are some issues playing back swf files on windows :) Here’s a link to the video.