← 输入框|github.com|刷新
Skip to content

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

Closed

Description

@ChinmayPandaZee

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

Activity

  1. ChinmayPandaZee commented on May 27, 2022

    @ChinmayPandaZee
    Author

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

  2. mykola-mokhnach commented on May 27, 2022

    @mykola-mokhnach
    Contributor

    this is fixed in master although not yet published

  3. ChinmayPandaZee commented on May 27, 2022

    @ChinmayPandaZee
    Author

    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

  4. valfirst commented on May 31, 2022

    @valfirst
    Collaborator

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

  5. Rabindra184 commented on Jun 4, 2022

    @Rabindra184

    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);

  6. ChinmayPandaZee commented on Jun 6, 2022

    @ChinmayPandaZee
    Author

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

  7. mykola-mokhnach commented on Jun 6, 2022

    @mykola-mokhnach
    Contributor

    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?

  8. valfirst commented on Jun 6, 2022

    @valfirst
    Collaborator

    It seems this issue is similar to SeleniumHQ/selenium#9416

  9. WillingSasi commented on Jul 5, 2022

    @WillingSasi

    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')

  10. kirannailwal1 commented on Jul 19, 2022

    @kirannailwal1

    @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

  11. kirannailwal1 commented on Aug 11, 2022

    @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

  12. mykola-mokhnach commented on Aug 11, 2022

    @mykola-mokhnach
    Contributor

    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

  13. 20 remaining items

  14. added a commit that references this issue on Dec 22, 2022
  15. sudo-naim commented on Jan 25, 2024

    @sudo-naim

    Hi,
    What is the update on this ticket, cause I am facing the same issue::
    Exception message:

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

    Code:

    AndroidDriver driver = (AndroidDriver) new EventFiringDecorator(new Listener()).decorate(webDriverBuilder.getAndroidDriver());
    

    Dependencies:

    implementation 'org.seleniumhq.selenium:selenium-java:4.17.0'
    implementation 'io.appium:java-client:9.0.0'
    
  16. bapszy commented on Feb 28, 2024

    @bapszy

    @isitrunning can you give me a working example for eg how to implement the beforeClick(WebElement element) listener with this MethodCallListeren? I cannot figure it out. I tried to implement this proxy, the code compiles but I'm not sure it does anything, and I didn't find any other implementation example then the one in the doc here.

  17. bapszy commented on Feb 28, 2024

    @bapszy

    @valfirst Yes I read it and put his in my code. But how can I wire in an onClick event on a Webelement to for eg create a screenshot on every click? It was in our Eventlistener which implemented the WebDriverListener, but that does not work now

  18. valfirst commented on Feb 28, 2024

    @valfirst
    Collaborator

    @bapszy you can address this question to Selenium

  19. bapszy commented on Feb 28, 2024

    @bapszy

    @valfirst I don't know how should I implement it to get it work. The MethodCallListener don't do anythink, and i cannot decorate my AndroidDriver with my Eventlistener, I get "Unable to create new proxy" error

    
    `MethodCallListener listener = new MethodCallListener() {
                @Override
                public void beforeCall(Object target, Method method, Object[] args) {
                    if (!method.getName().equals("get")) {
                        throw new NotImplementedException();
                    }
                    LOGGER.info("beforeCall: {}", method.getName());
                }
    
                @Override
                public void afterCall(Object target, Method method, Object[] args, Object result) {
                    if (!method.getName().equals("get")) {
                        throw new NotImplementedException();
                    }
                    LOGGER.info("afterCall: {}", method.getName());
                }
            };
    
            AndroidDriver decoratedDriver = createProxy(
                    AndroidDriver.class,
                    new Object[] {new URL(remoteUrl.toString()), getDesiredCapabilities()},
                    new Class[] {URL.class, Capabilities.class},
                    listener
            );
    //        AppiumDriver originalAppiumDriver = new AndroidDriver(remoteUrl, getDesiredCapabilities());
    //        AndroidDriver driver = new AndroidDriver(remoteUrl, getDesiredCapabilities());
            WebDriverListener webDriverListener = new EventListener();
            AndroidDriver decoratedAppiumDriver = new EventFiringDecorator<>(AndroidDriver.class, webDriverListener).decorate(decoratedDriver);`
    
  20. valfirst commented on Feb 29, 2024

    @valfirst
    Collaborator

    @bapszy please provide full error stack trace

  21. bapszy commented on Feb 29, 2024

    @bapszy

    @valfirst

    `java.lang.IllegalStateException: Unable to create new proxy
                    at org.openqa.selenium.support.decorators.WebDriverDecorator.createProxy(WebDriverDecorator.java:365)
                    at org.openqa.selenium.support.decorators.WebDriverDecorator.decorate(WebDriverDecorator.java:203)
                    at com.****.****.android.tests.AndroidBaseTest.setupEnvironment(AndroidBaseTest.java:95)
                    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.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
                    at org.testng.internal.invokers.MethodInvocationHelper$2.runConfigurationMethod(MethodInvocationHelper.java:453)
                    at com.****.utils.BaseTest.run(BaseTest.java:48)
                    at org.testng.internal.invokers.MethodInvocationHelper.invokeConfigurable(MethodInvocationHelper.java:467)
                    at org.testng.internal.invokers.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:358)
                    at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:296)
                    at org.testng.internal.invokers.TestInvoker.runConfigMethods(TestInvoker.java:823)
                    at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:590)
                    at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:221)
                    at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
                    at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:969)
                    at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:194)
                    at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
                    at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
                    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
                    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
                    at java.base/java.lang.Thread.run(Thread.java:834)
    Caused by: java.lang.InstantiationException: io.appium.java_client.android.AndroidDriver$ByteBuddy$zuLRSRXN
                    at java.base/java.lang.Class.newInstance(Class.java:571)
                    at org.openqa.selenium.support.decorators.WebDriverDecorator.createProxy(WebDriverDecorator.java:363)
                    ... 23 more
    Caused by: java.lang.NoSuchMethodException: io.appium.java_client.android.AndroidDriver$ByteBuddy$zuLRSRXN.<init>()
                    at java.base/java.lang.Class.getConstructor0(Class.java:3349)
                    at java.base/java.lang.Class.newInstance(Class.java:556)
                    ... 24 more
    
    
    io.appium.java_client.android.AndroidDriver$ByteBuddy$zuLRSRXN
    java.lang.InstantiationException: io.appium.java_client.android.AndroidDriver$ByteBuddy$zuLRSRXN
                    at java.base/java.lang.Class.newInstance(Class.java:571)
                    at org.openqa.selenium.support.decorators.WebDriverDecorator.createProxy(WebDriverDecorator.java:363)
                    at org.openqa.selenium.support.decorators.WebDriverDecorator.decorate(WebDriverDecorator.java:203)
                    at com.****.****.android.tests.AndroidBaseTest.setupEnvironment(AndroidBaseTest.java:95)
                    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.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
                    at org.testng.internal.invokers.MethodInvocationHelper$2.runConfigurationMethod(MethodInvocationHelper.java:453)
                    at com.****.utils.BaseTest.run(BaseTest.java:48)
                    at org.testng.internal.invokers.MethodInvocationHelper.invokeConfigurable(MethodInvocationHelper.java:467)
                    at org.testng.internal.invokers.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:358)
                    at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:296)
                    at org.testng.internal.invokers.TestInvoker.runConfigMethods(TestInvoker.java:823)
                    at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:590)
                    at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:221)
                    at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
                    at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:969)
                    at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:194)
                    at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
                    at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
                    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
                    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
                    at java.base/java.lang.Thread.run(Thread.java:834)
    Caused by: java.lang.NoSuchMethodException: io.appium.java_client.android.AndroidDriver$ByteBuddy$zuLRSRXN.<init>()
                    at java.base/java.lang.Class.getConstructor0(Class.java:3349)
                    at java.base/java.lang.Class.newInstance(Class.java:556)
                    ... 24 more`
    
  22. bapszy commented on Feb 29, 2024

    @bapszy

    @valfirst I really want to understand how this whole thing works, maybe that would help me make it work. So exactly, what happens during test run. There is an element on the UI which I want to click, and I want to make a screenshot. When will be the MethodCallListener activated and what will it do? When is the beforeClick from Webdriver happen?

  23. mykola-mokhnach commented on Feb 29, 2024

    @mykola-mokhnach
    Contributor

    Read the source to understand how it works.

    For example:
    This is the click method of a WebElement.
    It calls the execute method of the parent webdriver with the DriverCommand.CLICK_ELEMENT(id) payload.

    This means your driver proxy must intercept invocations of the execute method matching the above payload for the given element identifier.

  24. bapszy commented on Feb 29, 2024

    @bapszy

    @mykola-mokhnach Can you give me an exapmle code how should I implement it? I tried to understand the unit tests for this MethodCallListener proxy but something is missing, I really don't see how should I implement what you say :( I even don't get the log I put in the MethodCallListener you see above anywhere.

  25. bapszy commented on Mar 4, 2024

    @bapszy

    @valfirst @mykola-mokhnach Please, can you give an implementation example? I still couldn't make it work, I don't understand what do you mean by "your driver proxy must intercept invocations of the execute method matching the above payload for the given element identifier" ? I need an example

  26. viduuuur commented on Mar 18, 2024

    @viduuuur

    @bapszy I am facing same issue , have you figured out the solution ?

  27. ZhongGuan commented on Oct 25, 2024

    @ZhongGuan

    Since the execute method in driver marks as protected, so I have to create a custom Driver that extends the origin one. and make the execute method public. Now I can use if (((CommandPayload)args[0]).getName().equals(DriverCommand.CLICK_ELEMENT)) in beforecall to do the same of beforeClick in WebDriverListener

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions