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

Please update your code to match the fix. Sharing an example from our test https://github.com/SeleniumHQ/selenium/blob/feed290ab31d246c2af631a0b30e4a6ff29a2cdf/java/test/org/openqa/selenium/support/events/EventFiringDecoratorTest.java#L413. Please pass in the target class you are looking to cast using the method showed above.

pujagani avatar Oct 17 '22 13:10 pujagani

@pujagani it seems the provided example won't work for IOSDriver, because it doesn't have no-arg constructor

valfirst avatar Oct 17 '22 18:10 valfirst

https://github.com/SeleniumHQ/selenium/blob/feed290ab31d246c2af631a0b30e4a6ff29a2cdf/java/test/org/openqa/selenium/support/events/EventFiringDecoratorTest.java#L414 EventFiringDecorator has the required constructor. I might be missing something here. Did you mean that IOSDriver has a constructor that Selenium does not handle? Is there a missing functionality from Selenium end? I am afraid I don't understand the issue here. IOSDriver is not a Selenium-provided driver so I am not aware of the concern. I am going to need more context around to understand this better. Please help regarding the same. Thank you!

pujagani avatar Oct 18 '22 04:10 pujagani

@pujagani please see the example below

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.events.EventFiringDecorator;
import org.openqa.selenium.support.events.WebDriverListener;

import io.appium.java_client.ios.IOSDriver;

public class Main
{
    public static void main(String[] args) throws MalformedURLException
    {
        IOSDriver driver = new IOSDriver(new URL("http://doesnot.matter/"), new DesiredCapabilities())
        {
            @Override
            protected void startSession(Capabilities capabilities)
            {
                // Override in a sake of simplicity to avoid the actual session start
            }
        };
        WebDriverListener webDriverListener = new WebDriverListener()
        {
        };
        IOSDriver decoratedDriver = (IOSDriver) new EventFiringDecorator(IOSDriver.class, webDriverListener).decorate(
                driver);
    }
}

The result is an exception:

Exception in thread "main" java.lang.IllegalStateException: Unable to create new proxy
	at org.openqa.selenium.support.decorators.WebDriverDecorator.createProxy(WebDriverDecorator.java:342)
	at org.openqa.selenium.support.decorators.WebDriverDecorator.decorate(WebDriverDecorator.java:201)
	at Main.main(Main.java:28)
Caused by: java.lang.InstantiationException: io.appium.java_client.ios.IOSDriver$ByteBuddy$5w74hreT
	at java.base/java.lang.Class.newInstance(Class.java:636)
	at org.openqa.selenium.support.decorators.WebDriverDecorator.createProxy(WebDriverDecorator.java:340)
	... 2 more
Caused by: java.lang.NoSuchMethodException: io.appium.java_client.ios.IOSDriver$ByteBuddy$5w74hreT.<init>()
	at java.base/java.lang.Class.getConstructor0(Class.java:3517)
	at java.base/java.lang.Class.newInstance(Class.java:623)
	... 3 more

The reason is IOSDriver does not have no-arg constructor, which is required by:

https://github.com/SeleniumHQ/selenium/blob/ce1b3b180ec21c6d8938ef1c4b98022d128c00f7/java/src/org/openqa/selenium/support/decorators/WebDriverDecorator.java#L340

valfirst avatar Oct 18 '22 08:10 valfirst

Thank you for the details. I understand now. Appreciate @mykola-mokhnach's contribution and @valfirst's help around this issue.

pujagani avatar Oct 19 '22 05:10 pujagani

Thank you for the quick responses & jumping on this issue! 🙏😊

peterf2 avatar Oct 19 '22 07:10 peterf2

I have prepared https://github.com/appium/java-client/pull/1790. I hope the PR could solve this issue without touching selenium deps

mykola-mokhnach avatar Nov 02 '22 19:11 mykola-mokhnach

Hi, I updated to Appium java-client 8.2.1 this Friday (which contains #1790 PR), but unfortunately I'm still getting error while creating a decorated IOSDriver.

Did anyone had success in resolving this issue? Any suggestion what am I doing wrong?

Error message:

Unable to create new proxy
java.lang.IllegalStateException: Unable to create new proxy

Related code:

IOSDriver driver = new IOSDriver(remoteUrl, getDesiredCapabilities());
WebDriverListener eventListener = new WebDriverListener();
IOSDriver decorated = (IOSDriver) new EventFiringDecorator(IOSDriver.class, eventListener).decorate(driver);

isitrunning avatar Dec 03 '22 13:12 isitrunning

@isitrunning this is expected. Follow https://github.com/appium/java-client/blob/master/docs/The-event_firing.md#createproxy-api-since-java-client-830 if you want to create a transparent driver proxy

mykola-mokhnach avatar Dec 03 '22 13:12 mykola-mokhnach

Thank you @mykola-mokhnach, I was able to decorate the IOSDriver with your help! ☺️

Currently the beforeCall, afterCall, call, onError (from MethodCallListener) events are received. 🎉 Do you know any way to receive the beforeClick(WebElement element) event as well? Thanks!

isitrunning avatar Dec 06 '22 10:12 isitrunning

WebElement is just a convenience wrapper. Eventually all commands are anyway get sent to the driver. For example, the click call gets translated into parent driver's execute call: https://github.com/SeleniumHQ/selenium/blob/b878a9911e6a38b31ebfa517d809ea3c896c4e86/java/src/org/openqa/selenium/remote/RemoteWebElement.java#L78

This means you could intercept calls to execute API and then check for the matching element id and name in the CommandPayload argument

mykola-mokhnach avatar Dec 06 '22 10:12 mykola-mokhnach

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'

sudo-naim avatar Jan 25 '24 16:01 sudo-naim

@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.

bapszy avatar Feb 28 '24 10:02 bapszy

@bapszy https://github.com/appium/java-client/blob/master/docs/The-event_firing.md

valfirst avatar Feb 28 '24 10:02 valfirst

@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

bapszy avatar Feb 28 '24 10:02 bapszy

@bapszy you can address this question to Selenium

valfirst avatar Feb 28 '24 13:02 valfirst

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

bapszy avatar Feb 28 '24 14:02 bapszy

@bapszy please provide full error stack trace

valfirst avatar Feb 29 '24 08:02 valfirst

@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`

bapszy avatar Feb 29 '24 09:02 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?

bapszy avatar Feb 29 '24 09:02 bapszy