【问题标题】:Check whether image file exists, Robot-Framework, Selenium2Library检查图像文件是否存在,Robot-Framework,Selenium2Library
【发布时间】:2020-10-06 11:05:54
【问题描述】:

我想知道,如果可能以及如何检查,应该显示图片的元素是否确实显示了图片。

图片在<img src=..>,同域内。

【问题讨论】:

标签: selenium-webdriver robotframework


【解决方案1】:

您的目标是什么并不完全清楚。我认为可以安全地假设,如果您的代码一切正常(即:URL 是正确的,并且 css 规则不会导致元素被隐藏),那么浏览器将执行它应该执行的操作并显示图像。当然,如果您在实际测试浏览器本身的测试团队中,那就是另一个问题了。

所以,假设浏览器运行正常,这里有几个建议:

  1. 您可以使用Page should contain image 属性来验证元素是否在DOM 中。

  2. 可以使用Get Element Attribute关键字获取src属性的值,然后使用requests libraryGet关键字验证url返回200状态码。

  3. 您可以使用Element should be visible关键字来验证css规则是否允许显示元素。

这是一个完整的工作示例:

*** Settings ***
| Library | Selenium2Library
| Library | RequestsLibrary

*** Variables ***
| ${TEST_PAGE} | http://the-internet.herokuapp.com/hovers
| ${BROWSER}   | chrome

*** Keywords ***
| Assert an image is visible
| | [Arguments] | ${element id}
| | [Documentation]
| | ... | This keyword fails if the given image element is not visible.
| | ... | Three checks are performed: that the element is on the page,
| | ... | that selenium thinks the element is visible, and that the 
| | ... | src attribute of the image points to a resource that is 
| | ... | accessible. 
| | 
| | # Step 1: verify the page contains the given element
| | Page should contain image | ${element id}
| | 
| | # Step 2: verify that the element is visible
| | Element should be visible | ${element id}
| | 
| | # Step 3: verify the src attribute of the image is accessible
| | ${img src}= | Get element attribute | ${element id}@src
| | Create session | img-src | ${img src}
| | ${response} | Get | img-src | / | # URL is relative to the session URL
| | Should be equal as integers | ${response.status_code} | 200
| | ... | image url '${img src}' returned unexpected status code '${response.status_code}'
| | ... | show_values=False

*** Test Cases ***
| Example of test to verify an image is visible
| | [Documentation]
| | ... | Verify that the first image on the test page is visible
| | 
| | [Setup] | Open browser | ${TEST_PAGE} | ${BROWSER}
| | Assert an image is visible | //div[@class='figure']/img
| | [Teardown] | Close All Browsers

【讨论】:

  • 这太棒了!但是,${variable.something} 究竟是如何工作的?另外,为什么你在代码中使用|,而我只使用空格?这不是我第一次看到。
  • @Qwerty:我使用管道而不是空格,因为我认为它使测试更清晰。在某些情况下,很难看出一两个空格或制表符和空格之间的区别。 ${variable.something} 称为扩展变量语法。它将variable 视为一个对象,something 是该对象的一个​​属性。这在用户指南中有记录。
  • 不运行这一步有什么缺点吗Should be equal as integers
  • @Freddy:唯一的缺点是如果响应不是 200(例如:404),测试可能会认为图像是可见的(因为 DOM 元素在页面上)当实际图像不是时(因为 URL 不正确)
  • @Bryan:感谢您的解释。
【解决方案2】:

除了元素可见方面,您还可以通过 javascript 检查图像元素实际上是由浏览器呈现的,而不是显示一些丢失的图像图标。您可以使用 Selenium2Library 执行 javascript,或者将 Selenium2Library javascript 关键字调用包装到自定义关键字中以使其更具可读性。

http://watirmelon.com/2012/11/27/checking-an-image-is-actually-visible-using-webdriver/

【讨论】:

    【解决方案3】:

    我知道这个帖子有点老了,但我刚刚找到了另一种不使用请求库的方法:

    *** Variables ***
    ${avatar} | css: span.avatar_circle > img
    
    *** Test Cases ***
    Check Avatar From WordPress
    | [Documentation] | Check if the user image is ok
    | [Tags] | avatar | noncritical    
    | Click Element | ${avatar}
    | Wait Until Page Contains  | Latest Blog
    | Page Should Contain Image | ${avatar}
    | Element Should Be Visible | ${avatar}
    | ${src}= | Get Element Attribute | ${avatar} | src
    | Go To | ${src}
    | Wait Until Element Is Visible | css: img
    | Go Back
    

    【讨论】:

      【解决方案4】:

      据我所知,没有可以测试图像是否实际显示的 Robot Framework 库。有几个选项:

      1. 考虑将 Sikuli 与 Robot Framework 集成。有一些指南将指导您逐步完成集成。让 Sikuli 使用 Robot Framework 后,为您的图像截取屏幕截图并将其与基本屏幕截图进行比较。我认为这是您最好的选择。

      2. 还有其他工具可以进行视觉图像比较。例如,Needle 是用 Python 编写的,所以如果你有一些 Python 技能,你可以围绕 Needle 功能编写一个库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-15
        • 2017-02-21
        • 2013-01-13
        • 1970-01-01
        • 2013-05-12
        • 1970-01-01
        • 2016-09-08
        • 2012-08-13
        相关资源
        最近更新 更多