JavaScript executor is one of themost useful features of Selenium WebDriver. It allows Selenium user to perform many complex scenarioseasily. This post covers one such important scenario.Selenium verify image present example using WebDriver. We have used JavaScript executor in WebDriver Java code hereto verify if image is present in a web page.
@Test
public void CheckImage() throws Exception {
driver.get(baseUrl);
WebElement ImageFile = driver.findElement(By.xpath("//img[contains(@id,'Test Image')]"));
Boolean ImagePresent = (Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", ImageFile);
if (!ImagePresent)
{
System.out.println("Image not displayed.");
}
else
{
System.out.println("Image displayed.");
}
}
Run your test. It should identify if image is displayed in web page and will print appropriate result in console