PageFactory with MobileElement throws Exception with AppiumDriver and java-client 3.3.0


#1

Hi,

I am using java-client 3.3.0. and AppiumDriver. When I try to initialize PageFactory (using AppiumFieldDecorator) with MobileElements i am getting following error, but WebElement work fine.

java.lang.IllegalArgumentException: Can not set io.appium.java_client.MobileElement field com.page.holidays.LandingPage.tyHome to org.openqa.selenium.remote.RemoteWebElement$$EnhancerByCGLIB$$86f9ffce
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:741)
at org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:117)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:105)

I understant AppiumDriver is abstract class in this java-client version and both AndroidElement and IOSElement extend MobileElement. Also AppiumDriver has’t any specific converter.

But i want to club by IOS & Android pageobjects to eliminate code redundancy hence I have to use AppiumDriver.

One workaround i am working on is type cast WebElement to AndroidElement / IOSElement when ever required but that not the clean way.

Please let me know the root cause and how to fix this this problem. Any Help is much appreciated!!!

Thanks
Rajat


#2

Hi YadavRajat,

Can you please share snippet showing how you type-casted WebElement in PageFactory to AndroidElement ?

Thanks in Advance.


#3

you can refer https://github.com/priyankshah217/AppiumCucumberTest/blob/master/src/test/java/com/test/apidemo/app/screens/AbstractScreen.java


#4

I am using like this and it is working properly for me.
public CategoryPage(AppiumDriver driver) {
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), this);
}


#5

I there any updates on this ? I’m also facing the same issue.


#6

Hi, I am also facing same issue. Has anyone found any solution?


#7

Same here. Anyone has a workaround or solution?


#8

Same problem I am facing even I have tried with Appium & Android driver too but it wont work


#9

I was getting the same error using emulator

  • I checked the task manager , i could see lot of tasks for adb.exe
  • I restarted the emulator which terminated adb tasks and finally worked

#10

Hi @automason, I tried looking for multiple adb.exe, but there was only one adb.exe.
And I faced this issue.


#11

Hi all, I’m also getting same error, Is there any Updates ??

Any one has workaround??

Im using java-client 6.1.0

PageFactory.initElements(new AppiumFieldDecorator(driver,10,TimeUnit.SECONDS), this);

For the above line i’m getting compiler error as:

The constructor AppiumFieldDecorator(AppiumDriver, int, TimeUnit) is undefined


#12

Hi all, I am getting the same error . Did anyone managed to find a workaround to resolve this issue ? I am using Appium 1.9.1 and java client 6.1.0

Thank you


#14

maybe this will help
Implement Page Object Model using Appium & Java for Android Tests https://blog.testproject.io/2018/07/31/page-object-model-appium-java-android/