How does Sahi identify elements?
Sahi can identify elements in various ways.Standard attributes
Sahi has access to the DOM of the html page. Thus it can identify elements using their attributes. Depending upon the type of the element, Sahi uses the visible text, name, id, class, associatedLabel, index etc to identify the element. More details can be found in the Accessor APIs section.Adding a custom element
To add a custom element, add relevent properties to one of the json files.Example: To identify a polymer
CORE-ICON-BUTTON
, add
{"tag": "CORE-ICON-BUTTON", "type": null, "event":"click", "name": "_coreIconButton",
"attributes": ["aria-label", "sahiText", "id", "className", "index"],
"action": "_click", "value": "sahiText"},
You can add it in custom_html.json file. (Note: this element is already defined in polymer.json.)
Identifying elements by an attribute not available with Sahi
It is possible that Sahi does not use your desired element attribute while identifying elements. For example one of our users wished to use the "title" attribute of a button<input type="button" title="findMe">
To add title as one of the attributes, do the following:
- Open
sahi/config/accessors_metadata/html.json
- Search for
- Copy this into
userdata/config/accessors_metadata/custom_html.json
Add "title" to the list of attributes so that - Save the file, restart Sahi and clear browser cache.
{tag: "INPUT", type: "button", event:"click", name: "_button",
attributes: ["value", "name", "id", "index", "className"],
action: "_click", value: "value"};
attributes: ["value", "name", "id", "index", "className"]
becomes
attributes: ["value", "name", "id", "index", "title", "className"]
The full line looks like this after the change:
{tag: "INPUT", type: "button", event:"click", name: "_button",
attributes: ["value", "name", "id", "index", "title", "className"],
action: "_click", value: "value"};
NOTES:
- Any attribute of the HTML element can be used for identification. So if you are using a framework which introduces something like widgetID="myId", you can add "widgetID" in the above attributes list.
- The order of the attributes is significant. So if you wish to move "title" to be the default attribute, make it
- When an attribute is added, regular expression matching, adding of index for duplicates etc. are handled automatically by Sahi.
attributes: ["title", "value", "name", "id", "index", "className"]
infoNOTE: title is now added by default to Sahi