2

I'm running WebDriver (Selenium) & I'm attempting to cast WebDriver driver to JavascriptExecutor as follows:

Object aa = ((JavascriptExecutor)driver).executeScript("var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;", this.element);

However I either get a class cast exception or a null pointer exception when I run this code.

My situation is as follows: I've created a WebDriverAdapter class which accepts a concrete instance of WebDriver as follows:

private final WebDriver driver;

public WebDriverAdapter(WebDriver driver)
{
    this.driver = driver;
}

I then created another class named WebDriverDecorator which accepts an instance of WebDriverAdapter. The class looks as follows:

WebDriverAdapter driver;


public WebDriverDecorator(WebDriverAdapter driver, ...)
{
    this.driver = driver;
    ....; // Random String parameter.
}

When I attempt to cast any of the Drivers to JS executor I get a class cast exception.

On the other hand if I have both WebDriverAdapter and WebDriverDecorator extend RemoteWebDriver I do not get a class cast exception but I do get a NullPointerException.

How do I fix this?

Thanks

Edit: In Main my program looks like this:

WebDriver FFDriver = new FirefoxDriver();   
WebDriverAdapter driverAdapter = new WebDriverAdapter(FFDriver);
WebDriverDecorator driverDecorator = new  WebDriverDecorator(driverAdapter, "....");

driverDecorator.navigate().to("http://google.com"); // Works correctly



String XPath = ("//*[@id='gbw']/div/div/div[1]/div[1]/a");

WebElement e1 = driver.findElementByXPath(.....);



System.out.println(e1.getText()); // Works correctly


Object aa = ((JavascriptExecutor)driverDecorator).executeScript("var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;", e1); // Causes NullPointerException or Class cast exception depending on if I extend remote web driver or not..
2
  • Give also the code of WebDriverDecorator class. Commented Mar 21, 2017 at 15:03
  • Added the code. Commented Mar 21, 2017 at 15:07

1 Answer 1

1

OK, I was working on this for some time and found the solution. Since no one answered the question I'll post the answer I came up with.

Basically, all I needed to do was add implements JavascriptExecutor to both WebDriverDecorator and WebDriverAdapter and add unimplemented methods (such as executeScript(String arg0, Object... arg1) ). I would then cast each respective driver to JS executor inside the unimplemented method and now I'm able to call executeScript() directly on driver of WebDriverDecorator or WebDriverAdapter without casting anything.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.