Tweaking Sahi APIs

Accessor metadata explained

Sahi's Accessors are defined via json files.

Sahi uses userdata/config/accessor_metadata/metadata.properties file to determine which jsons to use. For example, to determine what accessors to use for html components, you will see:
html=custom_html.json,polymer.json,html.json


Here, custom_html.json is in userdata/config/accessor_metadata and the others are in config/accessor_metadata

Default json files are in config/accessor_metadata Custom json files can be added in userdata/config/accessor_metadata. If the file names are same in both folders, the userdata one is used.

When an accessor is defined in multiple json files, the first definition is used. If you want to override any existing html accessor, redefine them in custom_html.json and use
html=custom_html.json,polymer.json,html.json


accessor_metadata JSON explained

The json files contain an array of objects. A typical entry may look like
{"tag": "A", "type": null, "event":"click", "name": "_link", "attributes": ["sahiText", "title|alt", "id", "index", "href", "className"], "action": "_click", "value": "sahiText"}
This defines an _link API which helps identify an "A" html tag by one of the "attributes". During recording, the "event" (click) will be recorded by default.

If it was a form input element, the "type" and "value" attributes will also be specified.
{"tag": "INPUT", "type": "text", "event":"change", "name": "_textbox", "attributes": ["aria-label", "name", "id", "_sahi.getAssociatedLabel", "index", "className"], "action": "_setValue", "value": "value"}
Note also that the action has been specified as "_setValue", so _setValue will be recorded.

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:
  1. Open sahi/config/accessors_metadata/html.json
  2. Search for
  3. {tag: "INPUT", type: "button", event:"click", name: "_button",
    attributes: ["value", "name", "id", "index", "className"],
    action: "_click", value: "value"};
  4. Copy this into userdata/config/accessors_metadata/custom_html.json Add "title" to the list of attributes so that
  5. 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"};
  6. Save the file, restart Sahi and clear browser cache.
From now on Sahi will identify buttons by their title also.

NOTES:
  1. 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.
  2. The order of the attributes is significant. So if you wish to move "title" to be the default attribute, make it
  3. attributes: ["title", "value", "name", "id", "index", "className"]
  4. When an attribute is added, regular expression matching, adding of index for duplicates etc. are handled automatically by Sahi.
infoNOTE: title is now added by default to Sahi