Blog from October, 2014

New step: exists

Analog to the visible/not visible step, there is a new step added for exists/does not exist.
Where the visible step also perform a css check (except for visibility hidden and display none), the new step only checks if the elements are available in the DOM structure.

New step: wait until X

In some cases a test must wait on a AJAX or Javascript action. Often, the common i wait for X ms step is used.  

The disavantage of this step is that it will always wait for as long as declared in the X and so the total test time will be longer then maybe necessary.

If we wait for an element to appear/disappear we can now use this new step:

  • Wait until a certain element is visible/not visible
  • Wait until a certain element exists in the DOM structure
  • Give the maximum wait time (if not specified, it's 10 seconds.)

When the maximum wait time is achieved the test sill fail.

Examples:

  • i wait until div "X" is visible
  • wait for 500 ms until link "A" in div 2 exists

Example code: http://stash.foreach.be/projects/FE/repos/cucumber-test-framework/browse/ctf-lib-sahi/features/01%20-%20Basic%20actions.feature

The Language Reference is updated.

Just a pointer for Cucumber tests:

Avoid the i should see “TEXT” step, it checks only if a certain string is found in document.body. It is easy but it is not reliable to check if this text is really visible. Choose to see if a specific element is visible or contains a certain text.

AVOID:

  • i should see “there is an error”

USE:

  1. i should see div “there is an error” <- check on the text AND check on the visibility of the div!
  2. div “feedback” should contain “there is an error” <- check on the text but NO check on the visibility of the div!

More info on alternatif steps: Language Reference 

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