appium / appium/java-client

Page factory list element not initialized when parameterized by generic type

Open
#1,150 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.3k
Forks
752
Avg merge
6d 21h
Merged PRs (30d)
8

Description

Description

Given the following class (We have a base screen class that is used for Android and iOS that each specify T as AndroidElement and IOSElement, respectively, where we want to retain the specific platform element type):

    public class ScreenWithLists<T extends MobileElement> {
        public List<MobileElement> listME;
        public List<T> listT;
    }

and initialization with:

    PageFactory.initElements(new AppiumFieldDecorator(driver),
            new ScreenWithLists<MobileElement>()); // Or AndroidElement or IOSElement

The "listT" field does not get initialized as it fails the isDecoratableList check in AppiumFieldDecorator's DefaultFieldDecorator implementation when checking the list's type is one of "availableElementClasses".

Environment

  • appium java client build version: 6.1.0
  • selenium client build version: 3.12

Details

This generic type may be able to be checked with an additional bounds check in AppiumFieldDecorator's isDecoratableList:

    if ((listType instanceof TypeVariable) &&
            Arrays.asList(((TypeVariable<?>) listType).getBounds())
            .stream().anyMatch(item -> availableElementClasses.contains(item))) {
        return true;
    }

Testing

https://github.com/appium/java-client/blob/master/src/test/java/io/appium/java_client/pagefactory_tests/GenericTest.java
could update the Supplier to:

    TempGenericPage<?> page = new TempGenericPage<>();
    PageFactory
            .initElements(new AppiumFieldDecorator(new MockWebDriver()),
                    page);
    return null != page.getItems();

Related issue:

https://github.com/appium/java-client/issues/368

Code To Reproduce Issue [ Good To Have ]

public class ListParameterReproductionTest {
    public static class ScreenWithLists<T> {
        public List<MobileElement> listME;
        public List<T> listT;
    }

    public static void main(String[] args) {
        SearchContext mockSearchContext = new SearchContext() {
            @Override
            public <T extends WebElement> List<T> findElements(By by) {
                return null;
            }

            @Override
            public <T extends WebElement> T findElement(By by) {
                return null;
            }
        };

        ScreenWithLists<MobileElement> screen = new ScreenWithLists<MobileElement>();
        PageFactory.initElements(new AppiumFieldDecorator(mockSearchContext), screen);
        assertNotNull(screen.listME);
        assertNotNull(screen.listT);
    }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in AppiumFieldDecorator's DefaultFieldDecorator, especially isDecoratableList and its available element class check. Review src/test/java/io/appium/java_client/pagefactory_tests/GenericTest.java and the supplied ListParameterReproductionTest. Done means a parameterized list field such as listT is initialized alongside listME, with the relevant test passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
mobile, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.