Selenium - check if an image is displayed on page

后端 未结 5 1461
天涯浪人
天涯浪人 2020-12-12 23:25

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 thi

5条回答
  •  渐次进展
    2020-12-12 23:50

    Use this one liner function:

    Boolean isImageLoaded = (Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", imageWebElement);
    

    where:

    imageWebElement = WebElement to locate image on page

    isImageLoaded = Result in boolean (return true if image is present, false if its not)

提交回复
热议问题