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