Categories
java javascript selenium selenium-webdriver

NullPointerException on image verification in Selenium

I am using Selenium Server with Java and trying to verify that an image is actually displayed.
I am using the following code, but getting NullPointerException:

String imageURL = driver.findElement(By.xpath(imageXpath)).getAttribute("src");
assertTrue((Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", imageURL));

When I manually verify the URL stored in the variable imageURL I am getting a valid image, therefore I don’t understand why am I getting a NullPointerException?