Simple locators

Simple locators are those that have one attribute in the browser DOM or mobile page that makes them unique from other elements, and does not include any hierarchy such as a parent, child, sibling, or descendant. This includes id, name, className, tagName, linkText, and partialLinkText.

So for example, when we looked at the Google Mail login page, we saw that the first text field was defined as:

<input type="email" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="username" spellcheck="false" tabindex="0" aria-label="Email or phone" name="identifier" id="identifierId" dir="ltr" data-initial-dir="ltr" data-initial-value="" badinput="false">

Obviously, the id would be the first choice for defining the element in the page object class. But, if there is another element on the page that has the same ID, then the user could use the name or the className attribute. If those still did not yield a unique locator, the tagName could ultimately be used for the input field. For example:

@FindBy(id = "identifierId")
protected M email;

or

@FindBy(name = "identifier")
protected M email

or

@FindBy(className = "whsOnd")
protected M email

or

@FindBy(tagName = "input")
protected M email;

Notice that, when using the tagName, the input tag was used. If there were multiple input fields on the page, an index would be required to make it unique. XPath allows you to index fields sequentially within the DOM from top to bottom. They would be indexed as follows: input[1], input[2], input[3], and so on. XPath uses one-based numbering.

Finally, if the user wanted to access a link on the page, there are two locator types called linkText and partialLinkText that would allow them to define the locator by the entire link, or just a portion of it:

// google home page

<a class="gb_P" data-pid="23" href="https://mail.google.com/mail/?tab=wm">Gmail</a>

@FindBy(linkText = "Gmail")
protected M gmail;

or

@FindBy(partialLinkText = "mail")
protected M gmail;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.27.178