expressions expressions - 1 month ago 14
Java Question

java.lang.ClassCastException: com.sun.proxy.$Proxy8 cannot be cast to org.openqa.selenium.internal.WrapsDriver

I have following pointcut and the given advice in AspectJ

@Pointcut("(call(* org.openqa.selenium.WebElement.sendKeys(..)))")
public void onWebElementAction() {
}

@After("onWebElementAction() && target(webelement)")
public void afterWebElementAction(JoinPoint joinPoint, WebElement webelement) {
System.out.println(webelement.getAttribute("name")); //1
WebDriver driver = ((WrapsDriver) webelement).getWrappedDriver(); //2
//DO SOMETHING HERE
}


While the line 1 is executed without any error. It is on line 2 I get error

java.lang.ClassCastException: com.sun.proxy.$Proxy8 cannot be cast to org.openqa.selenium.internal.WrapsDriver


The casting works in other places without issues.
Can someone please help?

Answer

This is a wild guess since I don't see a case where it actually worked. From the exception it seems that the WebElement that is being passed to afterWebElementAction is initialized via PageFactory. My guess is that if you pass WebElement derived from driver.findElement(), to afterWebElementAction, you wouldn't get casting exception. This is how it must be working for you in other cases most likely.

Comments