How to extend MobileElement


#1

Hi all,

I’m trying to extend MobileElement, but I’m getting this error:

Error:(5, 8) java: findElements(org.openqa.selenium.By) in io.appium.java_client.MobileElement cannot implement findElements(org.openqa.selenium.By) in org.openqa.selenium.WebElement
return type java.util.List<io.appium.java_client.MobileElement> is not compatible with java.util.List<org.openqa.selenium.WebElement>

I tried setting “Eclipse compiler” in IDEA as suggested by TikhomirovSergey, but similar errors are displayed.

Has anyone extended the MobileElement and can point me in the right direction?

Thanks


#2

@Telmo_Cardoso want to try. but do not realize how to use new extended el…

Example of extend with some custom function and override function

package com.mobileTemplate.base;

import io.appium.java_client.MobileElement;
import org.openqa.selenium.Point;

public abstract class MyElement extends MobileElement {

    public MyElement() {
    }

    @Override
    public Point getCenter() {
        return super.getCenter();
    }


    public Point getMyCenter() {
        return this.getCenter();
    }

}

#3

Hi Aleksei,

my problem is not with the “way to extend”. Problem is that when I do it the compiler complains with the above error.

Have you tried compiling your code with the extends MobileElement


#4

Zero problem when i am compiling. Tried do e.g. assign some element. And just stopped on cast exception error.


#5

What IDE are you using? And what Java version.

I’m using Intellij and java 1.8.0_121


#6

I tried the same code didn’t got any compile time error

java version “1.8.0_60”
Intellij 2016.2 community Edition


#7

IntelliJ 2017.2.5
Project SDK: 1.8.0_121

Maven project


#8

Found it…

My pom.xml was getting selenium-server

<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-server</artifactId>
   <version>3.11.0</version>
</dependency>

I just need to comment this and let maven get it via java-client dependency.

Thanks :sweat_smile:


#9

I think even that should not be a problem, I have both of these in my pom and still it works.

   <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>6.0.0-BETA4</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.9.1</version>
    </dependency>

#10

Well, it was the only way I made it work :slight_smile: dunno why.


#11

Hey there,

were you able to successfully extend MobileElements?

Could you maybe give me some examples?

i am getting the following error when i make an instance of my page objects:

`java.lang.IllegalArgumentException: Can not set com.automation.appium.Helpers.MyElement field com.automation.appium.Pages.RegistrationPage.StagingSelector to io.appium.java_client.ios.IOSElement$$EnhancerByCGLIB$$d311658`

this is MyElement:

public class MyElement extends MobileElement{
  public MyElement() {}
  public void assertIsDisplayed() {
    Assert.isTrue(this.isDisplayed(), "not displayed");
  }
}

then i have this in my AbstractPage class:

public abstract class AbstractPage{
  public AbstractPage(AppiumDriver<MyElement> driver) {
    PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  }
}

and this is how i start my driver:

public AppiumDriver<GinloMobileElement> driver;
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), GetAndroidCapabilities());

it would be really great if someone would point out what i could do to make this work.

the general idea is to be able to add custom generic methods, which can be used on the elements, described in the pageobject structure. Any other suggestions how i can add methods, which i can use like:

Page.Element.myCustomMethod();

in my tests are very very welcome.