What is Sahi Relational API
Some times, elements are not uniquely identifiable by themselves. Identifying them in relation to either some element near them or by the element in which they are contained helps. Sahi Relational APIs help you solve that problem. The Relational APIs can be used even on applications with dynamic ids to easily locate one element with respect to another.
Please list the different APIs (markers) under DOM and Positional Relational APIs
There are 2 kinds of relation APIs: DOM Relation APIs and Positional Relation APIs. DOM relation markers like _near, _in specify that the element should be searched near or within another element. Position relations like under, rightOf, leftOf etc. relate one element to another via their position. The list is as follows:
DOM Relation APIs:
_near, _in, _startLookInside, _stopLookInside
Positional Relation APIs:
_rightOf, _leftOf, _leftOrRightOf, _under, _above, _aboveOrUnder
_parentNode, _parentCell, _parentRow, _parentTable
How to identify elements using relational APIs?
Identify the main element or the element which is easily identifiable. Then, anchor it and identify the (hard to identify) element in relation to the main element.
Let us take an example to highlight this. In the sample application, we have three books Core Java, Ruby for Rails and Python Cookbook. We also have a text field against each of them.
Suppose, we want to identify the text field against the Ruby for Rails. Notice the accessor of the element when no relational API is used. It reads _textbox(“q”).
First, identify the main element. In this case, it is the label?-?Ruby for Rails.
Anchor the main element by clicking on the Anchor icon.
Identify the secondary element now. Observe the accessor field.
It reads: _textbox(“q”, _near(_cell(“Ruby for Rails”))).
What is the advantage of using _rightOf/_leftOf compared to _near
Using _near where the element to be found may or may not exist can give you unexpected results. For example, in the table below
Let us say we are looking to check if a delete link exists against all users.
_assertExists(_link(“delete”, _near(_cell(“User Two”)))) may be expected to fail, but it passes.
_link(“delete”, _near(_cell(“User Two”))) actually points to the above highlighted link.
This is because, _near is a DOM relation API and finds an element within 7 ancestors (parent nodes) of the given anchor. In our case this link was found within 2 ancestors of the cell in the next row.
The correct way of asserting this would be
_assertExists(_link(“delete”, _rightOf(_cell(“User Two”)))).
_rightOf forces location only along the same line.
Does Sahi Flex Library (SFL) support the Relational APIs?
Sahi Flex Library (SFL) supports near, inside, leftOf, rightOf and under APIs. The usage of these APIs is a little different from the Sahi relation APIs.
The relation APIs are called as member functions.
They can be chained to use multiple relations.
Any other questions in your mind?
Feel free to comment or ask your question(s) and we will get back to you.