Accessing Browser Objects Using Sahi

Apr 22, 2012

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:

_click(_link("Deng")); // opens Luol's Wikipedia page

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...

_click(_link(0, _under(_tableHeader("Last Name"))));

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...

function clickRandomPlayerLink() {
    // _count's index is NOT zero-based; starts at 1
    var $numPlayers = _count("_cell", "lastname")-1;
    // get random number between 0-$numPlayers (2)
    var $playerIndex = _random($numPlayers);
    _click(_link($playerIndex, _under(_tableHeader("Last Name"))));

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.