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

cannot be cast to class io.appium.java_client.android.AndroidDriver while using EventFiringDecorator

Open ChinmayPandaZee opened this issue 4 years ago • 45 comments

Description

I'm trying to use the decorator mechanism provided by selenium 4. But following the doc to decorate the android driver, I get the exception

java.lang.ClassCastException: class jdk.proxy2.$Proxy22 cannot be cast to class io.appium.java_client.android.AndroidDriver (jdk.proxy2.$Proxy22 is in module jdk.proxy2 of loader 'app'; io.appium.java_client.android.AndroidDriver is in unnamed module of loader 'app')

Environment

Java client build version or git revision if you use some snapshot: 8.0.0 Appium server version or git revision if you use some snapshot: 1.22.3 Desktop OS/version used to run Appium if necessary: Mac Monterey Node.js version (unless using Appium.app|exe) or Appium CLI or Appium.app|exe: Appium.exe Mobile platform/version under test: android Real device or emulator/simulator: Both

Details

Please provide more details, if necessary.

Code To Reproduce Issue [ Good To Have ]

AndroidDriver driver = new AndroidDriver(url, driverCapabilities); driver=(AndroidDriver) new EventFiringDecorator(new MyLIstner()).decorate(driver);

SImilar to Cannot decorate AndroidDriver (probably the same for IOSDriver #1643

ChinmayPandaZee avatar May 27 '22 12:05 ChinmayPandaZee

It is simillar to Cannot decorate AndroidDriver (probably the same for IOSDriver #1643 @mykola-mokhnach @SrinivasanTarget

ChinmayPandaZee avatar May 27 '22 12:05 ChinmayPandaZee

this is fixed in master although not yet published

mykola-mokhnach avatar May 27 '22 12:05 mykola-mokhnach

Is there any workaround for the time being?

I am getting below exception which need to be handled from listeners. org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: 'POST /element' cannot be proxied to UiAutomator2 server because the instrumentation process is not running (probably crashed). Check the server log and/or the logcat output for more details @mykola-mokhnach

ChinmayPandaZee avatar May 27 '22 12:05 ChinmayPandaZee

Fixed and released: io.appium:java-client:8.1.0

valfirst avatar May 31 '22 08:05 valfirst

I think the below line still not working after the fix as well. AndroidDriver driver = new AndroidDriver(url, driverCapabilities); driver=(AndroidDriver) new EventFiringDecorator(new MyLIstner()).decorate(driver); Error: ava.lang.ClassCastException: class net.bytebuddy.renamed.java.lang.Object$ByteBuddy$ulOI0ra9 cannot be cast to class io.appium.java_client.android.AndroidDriver (net.bytebuddy.renamed.java.lang.Object$ByteBuddy$ulOI0ra9 is in unnamed module of loader net.bytebuddy.dynamic.loading.ByteArrayClassLoader @42b6d0cc; io.appium.java_client.android.AndroidDriver is in unnamed module of loader 'app')

at runners.installApk.setUp(installApk.java:47)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:385)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:321)
at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:700)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:527)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.testng.TestRunner.privateRun(TestRunner.java:794)
at org.testng.TestRunner.run(TestRunner.java:596)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
at org.testng.SuiteRunner.run(SuiteRunner.java:276)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)

code: WebDriver original = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities); AppiumListener listener = AppiumListener.getAppiumListener(); AndroidDriver decorated = (AndroidDriver)new EventFiringDecorator(listener).decorate(original); System.out.println(decorated);

Rabindra184 avatar Jun 04 '22 17:06 Rabindra184

@Rabindra184 yes, I also observed the same issue after the latest fix. Please look into this.@valfirst @mykola-mokhnach

ChinmayPandaZee avatar Jun 06 '22 04:06 ChinmayPandaZee

The decorator code is completely inherited from Selenium API. @diemol @titusfortner Do you know about any changes that happened recently and may cause the above error?

mykola-mokhnach avatar Jun 06 '22 06:06 mykola-mokhnach

It seems this issue is similar to https://github.com/SeleniumHQ/selenium/issues/9416

valfirst avatar Jun 06 '22 13:06 valfirst

Also i meet this issue in Appium:java-client:8.1.1,the following is calling code:

WebDriverListener webDriverListener = new CustomListener(); appiumDriver = (AppiumDriver) new EventFiringDecorator(webDriverListener).decorate(appiumDriver);

Caused by: java.lang.ClassCastException: class net.bytebuddy.renamed.java.lang.Object$ByteBuddy$AuHk1O9o cannot be cast to class io.appium.java_client.AppiumDriver (net.bytebuddy.renamed.java.lang.Object$ByteBuddy$AuHk1O9o is in unnamed module of loader net.bytebuddy.dynamic.loading.ByteArrayClassLoader @6af310c7; io.appium.java_client.AppiumDriver is in unnamed module of loader 'app')

WillingSasi avatar Jul 05 '22 01:07 WillingSasi

@valfirst @mykola-mokhnach @ChinmayPandaZee @WillingSasi @Rabindra184 Have this issue fixed for you. Please update i am still facing this issue. java.lang.ClassCastException: net.bytebuddy.renamed.java.lang.Object$ByteBuddy$F6Ow9062 cannot be cast to io.appium.java_client.android.AndroidDriver

org.seleniumhq.selenium :4.3.0 java-client :8.1.1 appium version : 1.22.3 node version : v16.16.0

kirannailwal1 avatar Jul 19 '22 09:07 kirannailwal1

@mykola-mokhnach @valfirst Can you please check the code implementation i have done, as i am still getting error.

My exception listener class :

  1. public class ExceptionListener implements WebDriverListener {
  2. @Override
  3. public void onError(Object target, Method method, Object[] args, InvocationTargetException e) {
  4. System.out.println("------------------------- Inside Exception -----------------------------");
  5. ...
  6. }
  7. }

Driver creation class:

  1. public AndroidDriver startDriver(String deviceID, String platformVersion, URL url, int systemPort) {
  2. AndroidDriver driver = new AndroidDriver(url, capabilities, systemPort));
  3. System.out.println("Android driver successful");
  4. driver= (AndroidDriver) new EventFiringDecorator(new ExceptionListener()).decorate(driver);
  5. return driver;
  6. }

it working fine till line 3, android driver is creating successfully, but at line 4 getting error. java.lang.ClassCastException: net.bytebuddy.renamed.java.lang.Object$ByteBuddy$6AyRD6Er cannot be cast to io.appium.java_client.android.AndroidDriver at com.one97.paytm.appautomation.android.GenericFunctionsAndroid.startDriver(GenericFunctionsAndroid.java:492)

org.seleniumhq.selenium :4.4.0 java-client :8.1.1 appium version : 1.22.3 node version : v16.16.0

kirannailwal1 avatar Aug 11 '22 06:08 kirannailwal1

driver= (AndroidDriver) new EventFiringDecorator(new ExceptionListener()).decorate(driver);

This won't work. You can only cast the decorator to a particular interface rather than a class

mykola-mokhnach avatar Aug 11 '22 07:08 mykola-mokhnach

@mykola-mokhnach Can you please share how i can do this casting.

kirannailwal1 avatar Aug 11 '22 08:08 kirannailwal1

@mykola-mokhnach @valfirst Could you please help me with this. Am I doing it wrong? I am also getting the same java.lang.ClassCastException.

Environment org.seleniumhq.selenium :4.4.0 java-client :8.1.1 node version : v18.4.0 java: 11

TestListener is listener class which implements ITestListener,WebDriverListener.

Implementation : AndroidDriver driver = new AndroidDriver(url, capabilities); WebDriverListener listener = new TestListener(); AppiumDriver driver1 = (AppiumDriver) new EventFiringDecorator(listener).decorate(driver);

got below exception for "AppiumDriver driver1 = (AppiumDriver) new EventFiringDecorator(listener).decorate(driver);".

java.lang.ClassCastException: class net.bytebuddy.renamed.java.lang.Object$ByteBuddy$bnI49y2m cannot be cast to class io.appium.java_client.AppiumDriver (net.bytebuddy.renamed.java.lang.Object$ByteBuddy$bnI49y2m is in unnamed module of loader net.bytebuddy.dynamic.loading.ByteArrayClassLoader @4a2e1e52; io.appium.java_client.AppiumDriver is in unnamed module of loader 'app')

Skumar489 avatar Aug 16 '22 12:08 Skumar489

@mykola-mokhnach @valfirst Are there any updates? Still facing this issue.

AppiumDriver driver = new AndroidDriver(AddressConfigurator.getService(), CapabilitiesConfigurator.getAndroidLocalOptions(udid, deviceName, platformVersion)); WebDriverListener listener = new EventLogListenerNew(); driver = (AppiumDriver) new EventFiringDecorator(listener).decorate(driver);

Error: java.lang.ClassCastException: class net.bytebuddy.renamed.java.lang.Object$ByteBuddy$QE9PJjHx cannot be cast to class io.appium.java_client.android.AndroidDriver (net.bytebuddy.renamed.java.lang.Object$ByteBuddy$QE9PJjHx is in unnamed module of loader net.bytebuddy.dynamic.loading.ByteArrayClassLoader @444e6416; io.appium.java_client.android.AndroidDriver is in unnamed module of loader 'app')

org.seleniumhq.selenium :4.4.0 java-client :8.1.1 appium version : 2.0.0-beta.43 node version : v18.8.0

davudmurtazin avatar Aug 29 '22 08:08 davudmurtazin

@mykola-mokhnach we've made a few changes in the past year, honestly not sure what would have caused the issue: https://github.com/SeleniumHQ/selenium/commits/trunk/java/src/org/openqa/selenium/support/decorators/WebDriverDecorator.java

@pujagani might know more.

titusfortner avatar Aug 29 '22 21:08 titusfortner

@mykola-mokhnach @pujagani Could you please help me with this. I am getting the same java.lang.ClassCastException.

Environment org.seleniumhq.selenium :4.4.0 java-client :8.1.1 node version : v18.4.0 java: 11

TestListener is listener class which implements ITestListener,WebDriverListener.

Implementation : AndroidDriver driver = new AndroidDriver(url, capabilities); WebDriverListener listener = new TestListener(); AppiumDriver driver1 = (AppiumDriver) new EventFiringDecorator(listener).decorate(driver);

got below exception for "AppiumDriver driver1 = (AppiumDriver) new EventFiringDecorator(listener).decorate(driver);".

java.lang.ClassCastException: class net.bytebuddy.renamed.java.lang.Object$ByteBuddy$bnI49y2m cannot be cast to class io.appium.java_client.AppiumDriver (net.bytebuddy.renamed.java.lang.Object$ByteBuddy$bnI49y2m is in unnamed module of loader net.bytebuddy.dynamic.loading.ByteArrayClassLoader @4a2e1e52; io.appium.java_client.AppiumDriver is in unnamed module of loader 'app')

Balasureshkumar avatar Sep 29 '22 13:09 Balasureshkumar

I thought I linked the commit to this issue but turns out I linked it to another issue. It was a mistake. Apologies for the same.

https://github.com/SeleniumHQ/selenium/commit/fcc549ad290d9ae13beecfd875b57178d5fcb2c5

The commit is part of the latest 4.5 Selenium release. It allows passing in the target class you want to cast to and that should no longer cause the error.

pujagani avatar Sep 30 '22 04:09 pujagani

Please refer to https://github.com/SeleniumHQ/selenium/blob/316f9738a8e2079265a0691954ca8847e68c598d/java/test/org/openqa/selenium/support/events/EventFiringDecoratorTest.java#L422 as an example.

pujagani avatar Sep 30 '22 04:09 pujagani

Hi,

We ran into the same issue with the following environment:

  • org.seleniumhq.selenium:selenium-java:4.5.0
  • io.appium:java-client:8.2.0
  • java: 11

Related code: (Where EventListener implements WebDriverListener.class) IOSDriver driver = new IOSDriver(url, capabilities); IOSDriver decoratedDriver = (IOSDriver) new EventFiringDecorator(new EventListener()).decorate(driver);

Error: class net.bytebuddy.renamed.java.lang.Object$ByteBuddy$x5NKFlZa cannot be cast to class io.appium.java_client.ios.IOSDriver (net.bytebuddy.renamed.java.lang.Object$ByteBuddy$x5NKFlZa is in unnamed module of loader net.bytebuddy.dynamic.loading.ByteArrayClassLoader @732b562e; io.appium.java_client.ios.IOSDriver is in unnamed module of loader 'app')

Can you please give us some help please? Thank you

peterf2 avatar Oct 17 '22 12:10 peterf2