TestProject Forum
Powered by leading experts in the test automation community

Check if image is displayed with selenium?

I have a website to test with various image galleries

I’m trying to check if my page has images displayed - that they contain valid image data.

Can it be done with selenium ?

A solution I used a while back:

check if image is displayed with JS executor

Object result = ((JavascriptExecutor) driver).executeScript(
   "return arguments[0].complete && "+
   "typeof arguments[0].naturalWidth != \"undefined\" && "+
   "arguments[0].naturalWidth > 0", imageElement);

    boolean loaded = false;
    if (result instanceof Boolean) {
      loaded = (Boolean) result;