Selenium - check if an image is displayed on page

0
0

I’m creating a suite of Selenium tests for an web album application. I would like to test whether an image is actually displayed (it contains valid image data). Is such thing possible?

  • You must to post comments
0
0

I faced this similar situation before where the src of the image is as expected but the image is not displayed on the page.

You can check if the image is getting displayed or not by using the JavaScriptExcecutor.

Use the following code –
Pass the WebElement (image) –

 Object result = ((JavascriptExecutor) driver).executeScript(
"return arguments[0].complete && "+
"typeof arguments[0].naturalWidth != \"undefined\" && "+
"arguments[0].naturalWidth > 0", image);
boolean loaded = false;
if (result instanceof Boolean) {
loaded = (Boolean) result;
System.out.println(loaded);
}

You can verify if the image has actually loaded on the webpage by doing this.

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website