Selenium - 检查页面上是否显示图像

Mat*_*ský 13 selenium image selenium-webdriver

我正在为网络相册应用程序创建一套Selenium测试.我想测试图像是否实际显示(它包含有效的图像数据).这样的事可能吗?

Har*_*ddy 20

我遇到了类似的情况,之前图像的src是预期的,但图像没有显示在页面上.

您可以使用JavaScriptExcecutor检查图像是否显示.

使用以下代码 - 传递WebElement(图像) -

 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);
    }
Run Code Online (Sandbox Code Playgroud)

您可以通过执行此操作验证图像是否已实际加载到网页上.