Using SAHI locators

SAHI lookups

In twist tests, we see that Xpath lookup of Selenium locators are used, fe:

_click(_byXPath("//*[@id='loginInfo']/a"))

Xpath lookup will not work in a number of browsers, so we can rewrite the above to:

browser.link( "Login" ).in( browser.listItem( "loginInfo" ) ).click();

It wil work faster and in more browsers.

You must name specific elements as a method (see list of possible elements).

In case you did not know: SAHI often checks multiple fields based on the argument.

In the above it will look for an A element with:

  • OR the text Login
  • OR id attribute Login
  • OR title attribute Login
  • OR class attribute Login

Be aware that you don't know which value will win.

Supplement: the order of lookups is determined by the configuration in the concat.js of Sahi. This is also where you can extend Sahi to go look at other attributes. More info in the Sahi documentation.

Class matching

Class matching: default SAHI will match on the full class value, so if you have multiple classes it should be “class1 class2 class3”.

You can work around this with a regex:

// Example lookup for: <a class="class1 class2">Login</a>
browser.link("class1")			// no match
browser.link("/class1/")		// ok
browser.link("/CLASS1/")		// no match
browser.link("/CLASS1/i")		// ok
browser.link("login")		// no match
browser.link("/login/i")	// ok

Combine with indexer:

browser.link("login[1]")	// 2e link!
browser.link("/login/i[1]")	// idem

The use of a regex can be a way of text matching in an hyphenation piece. (example code available upon request

Text matching

SAHI will filter html tags within an element and still match the text.

// <a href="blabla">this is <strong>text</strong></a>
browser.link("this is text")
// <label>Name<span class="required">*</span></label>
browser.label("Name*") 			// mind the spaces!
// <tr><td>first cell</td><td class="active">ja</td></tr>
browser.row("/first cell/")

Happy testing...