Search code examples
javaandroidseleniumannotationsappium

How to change the package name in AndroidFindBy annotation


Problem : I want to run an Appium script on the same application, but different builds.

All resource-Id in the application is appended with the package name

Eg: For build A , username text field is - alpha.beta.charlie.delta:id/user_name and

For build B username field alpha.beta.charlie/user_name , delta is removed from the package name.

I use AndroidFindBy from the page factory to locate the elements

    private static final PACKAGE = "alpha.beta.charlie.delta"

    @AndroidFindBy(id = PACKAGE + "/user_name")
    private MobileElement username;

Where PACKAGE is declared as constant .

Now if I want to run the script on build B, I have to change the package name every time before I trigger the execution. Which is not efficient.

I know we can't add variables to pagefactory annotations.

The only workaround I see is using https://developer.android.com/reference/android/support/test/uiautomator/UiSelector.html#resourceidmatches

Is there any other workaround where I can add the package name at compile time to bypass changing package name manually.


Solution

  • You don't need package name as Appium can find element without package name in resource id so you can use value after ":id/". I went through this problem when I get package name change for QA build and Prod build. so try below code :

    @AndroidFindBy(id = "user_name")
    private MobileElement username;
    

    Now it is independent from package name and can be run on any build if id is user_name in all builds.