appium/java-client

Change default strategy for @HowToUseLocators programatically

itkhanz opened this issue · 5 comments

Description

Appium provides a way to use multiple locator startegies via @HowToUseLocators annotation. The default starategy is set to LocatorGroupStrategy.CHAIN, so if we want to use the alternative strategy i.e. LocatorGroupStrategy.ALL_POSSIBLE, then we need to annotate our WebElements via

@HowToUseLocators(androidAutomation = ALL_POSSIBLE, iOSAutomation = ALL_POSSIBLE)
@FindAll{@FindBy(someStrategy1), @FindBy(someStrategy2)}) 
@AndroidFindBy(someStrategy1) @AndroidFindBy(someStrategy2) 
@iOSFindBy(someStrategy1) @iOSFindBy(someStrategy2) 
RemoteWebElement someElement;

More on this here:
https://github.com/appium/java-client/blob/master/docs/Page-objects.md#--any-possible
https://www.javadoc.io/doc/io.appium/java-client/latest/io/appium/java_client/pagefactory/HowToUseLocators.html

The problem is that we have many WebElements and almost all of them use the LocatorGroupStrategy.ALL_POSSIBLE. This means that for each of the WebElement we have to write an additional line to annotate it with @HowToUseLocators(androidAutomation = ALL_POSSIBLE, iOSAutomation = ALL_POSSIBLE). This causes alot of repetitive code which could be avoided by changing the default strategy in one place.

I am looking for a way to set the default strategy at single place, so it can be applied to all the WebElements unless it is annotated differently.

Is there a way to achieve this currently? If not, would you be interested in adding this functionality to page objects?

Environment

  • Appium Java client 9.1.0

Details

Perhaps we can set this via following ways by passing an extra parameter which changes the default strategy for web elements of page:

PageFactory.initElements(new AppiumFieldDecorator(searchContext, 
	    new TimeOutDuration(15, TimeUnit.SECONDS),
            HowToUseLocators(androidAutomation = ALL_POSSIBLE, iOSAutomation = ALL_POSSIBLE)
            ), 
           pageObject
);

Code To Reproduce Issue [ Good To Have ]

Please remember: it's easier to reproduce and fix the bug with sample code.
You can git clone https://github.com/appium/appium/tree/master/sample-code or https://github.com/appium/appium/tree/master/sample-code/apps and reproduce an issue using Java and sample apps.
Also you can create a gist with pasted java code sample or put it here using markdown. About markdown please read Mastering markdown and
Writing on GitHub

Exception Stacktraces

Please create a gist with the pasted stacktrace of the exception thrown by java.

Link To Appium Logs

Please create a gist which is a paste of your full Appium logs, and link them here. Do not paste your full Appium logs here, as it will make this issue very long and hard to read!
If you are reporting a bug, always include Appium logs as linked gists! It helps to define the problem correctly and clearly.

I personally find the page object implementation complicated, hard to debug and to read/understand. So it is rather the question to you, if you would be interested in taking care of it and adding/documenting/testing features there. Of course we are happy to help with PR reviews or advices, but keep in mind this is a piece of a fairly complicated code that requires a decent level of Java programming expertise.

I personally find the page object implementation complicated, hard to debug and to read/understand. So it is rather the question to you, if you would be interested in taking care of it and adding/documenting/testing features there. Of course we are happy to help with PR reviews or advices, but keep in mind this is a piece of a fairly complicated code that requires a decent level of Java programming expertise.

I can take a look at implementation. It would be helpful if you could provide me with some hints and starting points for investigation.

why not use

    @iOSXCUITFindAll(value = {
            @iOSXCUITBy(id = "addContact"), 
            @iOSXCUITBy(id = "addNewButton")
    })
    @AndroidFindAll(value = {
            @AndroidBy(id = "add_new_payee_button_constraint_layout"), // new in 5.18 release
            @AndroidBy(id = "payments_add_new_floating_button"),
            @AndroidBy(id = "tv_new")
    })
    private WebElement addContactButton;

you can use even more complex

    @AndroidFindBy(id = "mac_first_name")
    @AndroidFindAll(value = {
            @AndroidBy(id = "auto_complete"),
            @AndroidBy(className = "android.widget.EditText")
    }, priority = 1)
    @iOSXCUITFindBys(value = {
            @iOSXCUITBy(id = "firstName"),
            @iOSXCUITBy(className = "XCUIElementTypeTextField")
    })
    private WebElement firstNameInput;

why not use

    @iOSXCUITFindAll(value = {
            @iOSXCUITBy(id = "addContact"), 
            @iOSXCUITBy(id = "addNewButton")
    })
    @AndroidFindAll(value = {
            @AndroidBy(id = "add_new_payee_button_constraint_layout"), // new in 5.18 release
            @AndroidBy(id = "payments_add_new_floating_button"),
            @AndroidBy(id = "tv_new")
    })
    private WebElement addContactButton;

you can use even more complex

    @AndroidFindBy(id = "mac_first_name")
    @AndroidFindAll(value = {
            @AndroidBy(id = "auto_complete"),
            @AndroidBy(className = "android.widget.EditText")
    }, priority = 1)
    @iOSXCUITFindBys(value = {
            @iOSXCUITBy(id = "firstName"),
            @iOSXCUITBy(className = "XCUIElementTypeTextField")
    })
    private WebElement firstNameInput;

Thank you, I agree this is one of the useful option available. Although it is certainly useful for more advanced use cases, but I feel it requires more lines of code and kind of over-do for simple use cases like where you are only concerned about using FIND_ALL strategy as default without having to wrap them in additional annotation at all places.

This is how most of locators in our codebase look like and almost all use FINA_ALL

@iOSXCUITFindBy(id = "submit") @iOSXCUITFindBy(id = "done")
@AndroidFindBy(id = "submit") @AndroidFindBy(id = "done")
private WebElement submitButton;

so therefore if there is a way to change the default strategy at one place, maybe during PageFactory initialization then this will be very handy.