Common locators

When we talk about defining all the elements on a page, there is a specific syntax that Selenium PageFactory provides the user to define those elements. That syntax is @FindBy, plus the locator, @CacheLookup, and an attribute name and scope for the element. There are various "standards" for which locator to use: ID, tag, name, class, attribute, CSS, XPath, and so on. For now, those standards will not be covered. The following example shows how to define common elements in the base class that would apply to all the pages in an application. Subclasses would inherit them when the class is instantiated:

// common WebElement locators included in base class

@FindBy(css = "img[src*='myLogo.png']")
@CacheLookup
protected M companyLogo;

@FindBy(partialLinkText = "All Rights Reserved")
@CacheLookup
protected M copyright;
// common MobileElement locators included in base class

@AndroidFindBy
(className = "myLogo")
@iOSFindBy(className = "myLogo")
protected M companylogo;

@AndroidFindBy(id = "title")
@iOSFindBy(xpath = "//*[@name = 'title']")
protected M title;

Some things to note here: the @FindBy method can take any of the available locator formats to define the element. @CacheLookup can be used for static elements that do not change dynamically on the page. Using this annotation tells the WebDriver to store the locator rather than actioning a lookup in the DOM each time that element is referenced. Its use can make the scripts run faster by nature. It does not work with elements that change dynamically on the page.