java-client icon indicating copy to clipboard operation
java-client copied to clipboard

AndroidDriver<T>.findElement always return AndroidElement ?

Open asvdw opened this issue 9 years ago • 5 comments

Description

I'm trying to extend MobileElement (up to now, AndroidElement, but IosElement is following...) to add custom functionalities. To achieve it, I'm using AndroidDriver<MyCustomAndroidElement> driver, hoping that driver.findElement(...) will return an instance of MyCustomAndroidElement. But I get always an AndroidElement:

pseudo-code

driver = new AndroidDriver<AndroidElement>
driver.findElement(By.id("id1").getClass() // return AndroidElement
driver = new AndroidDriver<WebElement>
driver.findElement(By.id("id1").getClass() // return AndroidElement. Expected: WebElement
driver = new AndroidDriver<RemoteWebElement>
driver.findElement(By.id("id1").getClass() // return AndroidElement. Expected: RemoteWebElement
driver = new AndroidDriver<CustomClassExtendingAndroidElement>
driver.findElement(By.id("id1").getClass() // return AndroidElement. Expected: CustomClassExtendingAndroidElement

CustomClassExtendingAndroidElement = driver.findElement(By.id("id1") // cast error

So:

  • is it working as expected ?
  • if so, How can I extend MobileElement and make findElement to return it ?

Environment

  • Appium 5.0.0 beta 6 (4.1.2 same effect)
  • Java 8 / IntelliJ latest

Thank you for your help

asvdw avatar Apr 04 '17 08:04 asvdw

I'd rather extend By class. For example:

public abstract class FBBy extends MobileBy {

    protected FBBy(String locatorString) {
        super(locatorString);
    }

    public static By AccessibilityId(String id) {
        if (id == null) {
            throw new IllegalArgumentException("Must supply a valid accessibility id string");
        } else {
            return new ByAccessibilityId(id);
        }
    }

    public static class ByAccessibilityId extends By implements Serializable {
        private final String id;

        public ByAccessibilityId(String uiautomatorText) {
            this.id = uiautomatorText;
        }

        @Override
        public List<WebElement> findElements(SearchContext context) {
            if (context instanceof FindsByFBAccessibilityId) {
                return ((FindsByFBAccessibilityId) context).findElementsByFBAccessibilityId(this.id);
            } else {
                throw new IllegalArgumentException(String.format("%s context is not supported for element search",
                        context.getClass().getSimpleName()));
            }
        }

        @Override
        public WebElement findElement(SearchContext context) {
            if (context instanceof FindsByFBAccessibilityId) {
                return ((FindsByFBAccessibilityId) context).findElementByFBAccessibilityId(this.id);
            } else {
                throw new IllegalArgumentException(String.format("%s context is not supported for element search",
                        context.getClass().getSimpleName()));
            }
        }

        @Override
        public String toString() {
            return "FBBy.AccessibilityId: " + this.id;
        }
    }
}

....

public interface FindsByFBAccessibilityId<T extends WebElement> {
    T findElementByFBAccessibilityId(String var1);

    List<T> findElementsByFBAccessibilityId(String var1);
}

.....

public class MyIOSDriver extends IOSDriver<WebElement> implements 
        FindsByFBAccessibilityId {
   @Override
    public FBElement findElementByFBAccessibilityId(String value) {
     ...
    }

    @Override
    public List<FBElement> findElementsByFBAccessibilityId(String value) {
     ...
    }
}

....

public class FBElement extends IOSElement implements FindsByFBAccessibilityId {
// Adding my cool stuff here
...
}

and then

driver.findElement(FBBy.AccessibilityId("id1")) instanceof FBElement == true

mykola-mokhnach avatar Apr 04 '17 09:04 mykola-mokhnach

Mmm... looks interesting. Thank you a lot.

(Even if I can't undestand what is the purpose of T parameter in AndroidElement<T> if findElement doesn't return T.)

asvdw avatar Apr 04 '17 12:04 asvdw

@asvdw @mykola-mokhnach

is it working as expected ?

Yes. But generic parameter just bounds the returning type in client code.

I was thinking about the using of certain elements. However it would be good if we were automating only native apps. When there is browser/hyprib app it is more useful to use RemoteWebElement as the generic parameter. Also there are some server-side issues which make the strict binding inconsistent for now.

Maybe in future it would be possible

if so, How can I extend MobileElement and make findElement to return it ?

I think you have to re-implement this class. Please take a look at the appium-specific implementation and how is it used

TikhomirovSergey avatar Apr 04 '17 20:04 TikhomirovSergey

Waouw... Great! Thank you for your help, I won't have found that by myself...

asvdw avatar Apr 05 '17 21:04 asvdw

I am closing it.

TikhomirovSergey avatar Aug 29 '17 12:08 TikhomirovSergey