我想知道是否可能以及如何检查应该显示图片的元素是否确实显示了图片。
图片位于<img src=..>
并且在同一域内。
目前尚不完全清楚您的目标是什么。我认为可以安全地假设,如果您的代码执行正确的所有操作(即:URL 正确,并且 css 规则不会导致元素被隐藏),则浏览器将执行其应该执行的操作并显示图像。当然,如果您所在的测试团队实际上正在测试浏览器本身,那就是另一个问题了。
因此,假设浏览器运行正常,这里有一些建议:
您可以使用页面应包含图像 http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html#Page%20Should%20Contain%20Image属性来验证元素是否在 DOM 中。
您可以使用获取元素属性 http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html#Get%20Element%20Attribute关键字来获取值src
属性,然后使用Get http://bulkan.github.io/robotframework-requests/#Get的关键字请求库 http://bulkan.github.io/robotframework-requests/验证 url 是否返回 200 状态代码。
您可以使用元素应该可见 http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html#Element%20Should%20Be%20Visible关键字来验证 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)