Domanda

I have my own CustomDriver which extends ChromeDriver.

public CustomDriver extends ChromeDriver

For some need I am wrapping the CustomDriver inside the EventFiringWebDriver .Everything is working fine . but when I try to unwrap the underneath Driver inside the EventFiringWebDriver I get the below exception.

java.lang.ClassCastException: com.sun.proxy.$Proxy14 cannot be cast to com.test.CustomDriver.

Below is the Code which I am trying to Unwrap my Driver.

    private CustomDriver extract(EventFiringWebDriver wd) {
        return (CustomDriver)  wd.getWrappedDriver();
    }

Can anybody please help, Is this a bug with Selenium 2.0 or its the desired feature, If yes then how can I achive it.

È stato utile?

Soluzione 3

I could achive it in this way. It is not important for me that what eventually EventFiringWebDriver does with the proxy Object.

public class EventFiringWebDriverWrapper extends EventFiringWebDriver {
    private WebDriver driver;

public EventFiringWebDriverWrapper(WebDriver driver) {
    super(driver);
    this.driver= driver;
}

@Override
public WebDriver getWrappedDriver() {
    return driver;

}
}

And I can extract it this way, which is good to go

private CustomDriver extract(EventFiringWebDriverWrapper wd) {
    return  (CustomDriver) wd.getWrappedDriver();
 }

Altri suggerimenti

issue fixed by selenium . Above mentioned answers is just work around. please refer the fix from selenium.

It looks like Selenium uses JDK dynamic proxies in this case. This proxy is wrapped aroung your implementation, effectively hiding it. You can try Proxy.isProxyClass(wd) to check, if this is true. In general there is no way to exctract a delegate from a proxy, but you can try to look in InvocatioinHandler obtained by Proxy.getInvocationHandler(wd), chances are that your CustomDriver can be extracted from there:

private CustomDriver extract(EventFiringWebDriver wd) {
    if (Proxy.isCroxyClass(wd)){
       InvocationHandler ih = Proxy.getInvocationHandler(wd);
       // examine ih with debugger or reflection, your CustomDriver is likely to be somewhere inside
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top