Selenium - 检查页面上是否显示图像 [英] Selenium - check if an image is displayed on page

查看:158
本文介绍了Selenium - 检查页面上是否显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

在图像的src符合预期但图像不符合之前,我遇到过类似的情况显示在页面上。

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

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

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

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

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.

这篇关于Selenium - 检查页面上是否显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆