Best approach to declare mobile element in POM Appium automation

Hi all,

We have native app appium framework for android and iOS. Also, we are declaring element in POM as WebElement like below

For Android:
@AndroidFindBy(id = Constants.PACKAGE_PATH + “:id/guest_checkout_btn”)
public WebElement guestCheckOutBtn;

For iOS:
@FindBy(accessibilityId = “Delivery Addresses”)
public WebElement deliveryAddress;

Sometimes people are using this

public static By CANCEL_BTN=By.xpath("//XCUIElementTypeButton[@name='Cancel']");

Which is the best approach for this? Is there any article about this?

this is best

    @iOSXCUITFindBy(id = "iOS_ID")
    @AndroidFindBy(id = "guest_checkout_btn")
    private WebElement guestCheckOutBtn;
1 Like

a bit about the differences:
basically, the idea of POM (page object model) is to represent pages as objects. each page object will contain the “elements” displayed on the current page. in order to find the elements we use locators and selectors which are being represented by the BY class.

the first option is to hold BY instances with the locator strategies and then in each method call, run driver.finElement(myLocatorInstance) - no annotation is needed for this.
see: https://www.selenium.dev/documentation/test_practices/encouraged/page_object_models/

the second option which is simpler and easier is just to annotate the WebElement fields and to use AppiumFieldDecorator which “knows” to identify the different annotations (android, ios, windows) and wraps each field with a dynamic proxy (you must read and understand this concept if you are going to use it, it will solve you a lot of problems and you will understand your code better) and then, you can just call a method on the WebElement instance (which was decorated with the relevant annotations) and the proxy will get invoked and will return a “fresh” WebElement instance which will avoid StaleElementReferenceException.
see: https://github.com/appium/java-client/blob/master/docs/Page-objects.md (you can read on widgets as well, it is very useful).

1 Like