检查图像文件是否存在,Robot-Framework,Selenium2Library

Qwe*_*rty 2 robotframework selenium-webdriver

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

图片位于<img src=..>同一域内.

Bry*_*ley 5

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

因此,假设浏览器行为正常,这里有几个建议:

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

  2. 您可以使用Get Element Attribute关键字获取属性的值src,然后使用请求库Get关键字来验证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
Run Code Online (Sandbox Code Playgroud)