python - 如何验证特定元素或文本在 Robot Framework 或 Python Selenium 的监视器上是否可见

标签 python selenium robotframework

我需要测试单击链接后,网页的特定部分在显示器上可见。考虑一下维基百科的以下代码:

Open Browser    https://en.wikipedia.org/wiki/Selenium_(software)    Chrome
Page should contain    Selenium Remote Control
Click Element    //*[@href="#Selenium_Remote_Control"]
Sleep    3s
Close All Browsers

正如预期的那样,当单击该元素时,我可以直观地看到维基百科页面中的“Selenium Remote Control”部分出现在我的显示器屏幕上。我可以验证该部分或文本在页面上是否可用,并且预计 html 代码应该能够处理“跳转到 href”部分。但为了确保它在我们的应用程序和不同的浏览器实例上运行,我们希望有一种自动化方法,可以实际验证单击链接后“Selenium Remote Control”部分在监视器屏幕上实际上可见。

最佳答案

您给出的示例确实是一个长页面,其中相关元素已加载到 DOM 中,但在浏览器 View 端口中不可见。但是,当使用 ... should bevisible 关键字时,只会查看 DOM,并且从这个角度来看它是可见的。只是看不到。

有一些小的 JavaScript 函数可以评估可见元素是否确实可见。关于这个Go Make Things article找到 isInViewPort(elem) 函数。

IsInViewPort.js

/*
 * Obtained from: https://gomakethings.com/how-to-test-if-an-element-is-in-the-viewport-with-vanilla-javascript/
 */
var isInViewport = function (elem) {
    var bounding = elem.getBoundingClientRect();
    return (
        bounding.top >= 0 &&
        bounding.left >= 0 &&
        bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
        bounding.right <= (window.innerWidth || document.documentElement.clientWidth)
    );
};

在机器人脚本中,我们可以继续获取元素,然后使用自定义关键字将其传递给此函数:Element Should Be In View,当一个元素是真的,哪个不是。

View.robot 中是否有元素

*** Settings ***
Library    Selenium2Library
Library    OperatingSystem    


*** Test Cases ***
Element Should Be in View Test
    [Documentation]
    ...    Test to validate that an element isn't view-able and then is view-able. 
    Open Browser    https://en.wikipedia.org/wiki/Selenium_(software)    Chrome

    ${elem}    Get Webelement    xpath=//*[@id="Selenium_Remote_Control"]
    ${bool}    Element Should Be In View    ${elem}
    Should Not Be True    ${bool}

    Click Element    //*[@href="#Selenium_Remote_Control"]

    ${elem}    Get Webelement    xpath=//*[@id="Selenium_Remote_Control"]
    ${bool}    Element Should Be In View    ${elem}
    Should Be True    ${bool}

    [Teardown]    Close All Browsers

*** Keywords ***
Element Should Be In View
    [Documentation]
    ...    Partially Inspired by: https://stackoverflow.com/a/28709012/6152737
    [Arguments]    ${element}
    ${s2l}=     Get Library Instance    Selenium2Library
    ${js}=      Get File    IsInViewport.js    
    ${visible}=    Call Method    
    ...                ${s2l._current_browser()}    
    ...                execute_script    ${js} return isInViewport(arguments[0]);    ${element}
    [Return]    ${visible}

当 View 发生变化时,即由于单击的链接,则需要再次检索该元素。否则不会返回旧状态。

关于python - 如何验证特定元素或文本在 Robot Framework 或 Python Selenium 的监视器上是否可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48251621/

相关文章:

python - python 函数 Unresolved reference

javascript - 如何使用 Python 从包含 JavaScript 的 <a> 标签获取 href?

selenium-webdriver - RobotFramework:拖放 Selenium2 关键字似乎不起作用

robotframework - 有没有办法在机器人框架中生成测试报告,其中显示总测试用例= 0,通过= 0,失败= 0?

python - 如何从类变量引用静态方法

python - 使用 Python 运行的查询不在 MySQL 中执行 unixtime 函数

Python - 如何实现与 NumPy 函数兼容的自定义类?

xpath - 在selenium.get.Text和selenium.click中使用xpath

java - 无法单击 docusign 文档