In this blog, we would discuss a locator strategy that would enable us to efficiently write common test script for multiple OS (in case of mobile app) or browsers (in case of web app)
Mobile app testing:
appium.java_client annotations namely @AndroidFindBy and @iOSXCUITFindBy as locator strategy enables us to use single variable for both Android and iOS.
Best practice is to use a single locator that works in both Android and iOS.
Example:
@AndroidFindBy(xpath = "//android.widget.TextView[@text='xxxx']")
@iOSXCUITFindBy(xpath = "//XCUIElementTypeStaticText[@name='xxxx'][@label='xxxx']")
private MobileElement screenTitle;
However, sometimes we need to use multiple locators to cover various device models and OS version. In this case use @HowToUseLocators annotation from appium.java_client to define multiple locators for same mobile element.
Example:
@HowToUseLocators(androidAutomation = LocatorGroupStrategy.ALL_POSSIBLE,
iOSXCUITAutomation = LocatorGroupStrategy.ALL_POSSIBLE)
@AndroidFindBy(xpath = "//android.widget.TextView[@text='xxxx']")
@AndroidFindBy(accessibility = "xxxx")
@iOSXCUITFindBy(xpath = "//XCUIElementTypeStaticText[@name='xxxx']")
@iOSXCUITFindBy(accessibility = "xxxx")
private MobileElement appName;
Web app testing
In Web app, sometimes the UI layout is slightly different in different browsers (like Chrome vs Firefox). In such cases it is a good idea to have multiple locators as part of the same Web Element to cover multiple browsers. Use Selenium’s native @FindAll annotation in combination with @FindBy annotation in this case.
Example:
@FindAll({
@FindBy(xpath = "//input[@id='twotabsearchtextbox']"),
@FindBy(xpath = "//input[@id='nav-bb-search']")
})
private WebElement searchField;