Top Rounds
| We are Hiring! |
[Hide Navigation]

Sahi API Examples ·

Please refer to Browser Accessor APIs for full documentation. This section explains some of the usage with examples.

==


TitleIn stockCostAdd quantity to cartRecommendAlready own
Core Java5Rs. 300
Ruby for Rails12Rs. 200
Python Cookbook7Rs. 350

==

_near and _under

In the table above, all text boxes have name=“q”.
So Sahi’s accessor spy will identify them by indexes (_textbox("q"), _textbox("q[1]") and _textbox("q[2]")).

Let us look at the second textbox, which is near Ruby For Rails:
This is identified as _textbox("q[1]"). This can be a problem, because if a new row is added before Ruby, the index of this textbox would change. To fix this, Sahi uses an _near API. The same textbox can be identified as

_textbox("q", _near(_cell("Ruby for Rails")))

This accessor will not break even if you add another row before Ruby or another column before the textbox.

Like wise, to get to the “Recommend” checkbox near “Ruby for Rails”, we can use
_checkbox(0, _near(_cell("Ruby for Rails")), _under(_cell("Recommend")))

And for the “Already own” checkbox
_checkbox(0, _near(_cell("Ruby for Rails")), _under(_cell("Already own")))

These APIs make scripts simple, readable and robust.

To compare with XPaths, the “Recommend” checkbox is identified by XPather as
/html/body/div[4]/div/div[@id='container']/table[@id='listing']/tbody/tr[3]/td[5]/input[@id='z_awew12']

This can (and should) be simplified by a programmer tester, but it is evident how much uglier and brittle these are as compared to _near and _under

Using _cell(tableEl, rowText, colText)

If we want to verify the cost of Ruby for Rails book, we need to first look at that cell which is in the “Ruby for Rails” row and under the “Cost” column.

One way to do it, is to use _near and _under

_cell(0, _near(_cell("Ruby for Rails")), _under(_cell("Cost")))

Another is to find the table accessor, and then use it with some unique text in a row and unique text in the column.

_cell(_table("listing"), "Ruby for Rails", "Cost")

(The alternatives on the Controller show identifiers of encapsulating table and row too)




---


Top Rounds