Robot Framework,将自定义关键字与 Selenium2Library 集成

Robot Framework, integrating custom keywords with Selenium2Library

我在使用 robot selenium 库添加自定义关键字时遇到问题。我已经看到了一些添加自定义关键字的解决方案,但这种方法似乎对我不起作用。测试用例中实现的自定义关键字由 CustomSeleniumLibrary.Get 指定 Ok Button.I 已发布我在下面使用的代码和我的测试用例: 实现 Selenium2Library 的自定义关键字:

from Selenium2Library import Selenium2Library


# create new class that inherits from Selenium2Library
class CustomSeleniumLibrary(Selenium2Library):
    # create a new keyword called "get webdriver instance"
    def get_ok_button(self, locator):
               element = self._get_checkbox(locator)
               element.click()

调用自定义关键字的测试:

*** Settings ***

Documentation   A test suite containing tests related to invalid login. These
...             tests are data-driven by they nature. They use a single
...             keyword, specified with Test Template setting, that is called
...             with different arguments to cover different scenarios.

Resource        resource_one.txt
Library         CustomSeleniumLibrary.py
*** Variables ***
${button_xpath}   //button[@type='button']/span[text()=""'"Ok"'""]"))
${message}        Please select one or more entries to unlock!
${table_TO} //table[@id='userdetailTable']/tbody/tr/td
${unlock_checkbox}  //*[@id='userdetailTable']/tbody/tr/td[6]/input
*** Test Cases ***
Happy_Path_Unlock
    Given a Intellix user is on the login page
    When they enter their credentials
    Attempt to Unlock an Valid TO User Id
    Then they will see the TO User Id in the results list

*** Keywords ***
a Intellix user is on the login page
    Selenium2Library.Open Browser    ${url}   ${BROWSER_CHROME}

they enter their credentials
    Selenium2Library.Input Text    ${username_xpath}    ${username_txt}
    Selenium2Library.Input Password    ${password_xpath}    ${password_txt}
    Selenium2Library.Click Button    ${btn_submit}

Attempt to Unlock an Valid TO User Id
    Selenium2Library.Page Should Contain Link    ${view_tab}    ${view_tab_text}
    Selenium2Library.Page Should Contain Link    ${admin_tab}    ${admin_tab_text}
    Selenium2Library.Page Should Contain Link    ${unlock_tab}    ${unlock_tab_text}
    Selenium2Library.Click Link    ${unlock_tab}
    Selenium2Library.Wait Until Element Is Visible    ${TO_User_Id}
    Selenium2Library.Click Element    ${TO_User_Id}
    Selenium2Library.Input Text    ${TO_User_Id}    teksmith
    Selenium2Library.Click Button    ${go_btn}

they will see the TO User Id in the results list
    Selenium2Library.Wait Until Page Contains Element   ${unlock_checkbox}
    CustomSeleniumLibrary.Get Ok Button                    ${unlock_checkbox}
    Selenium2Library.Close Window

在 运行 宁文件之后,我收到以下错误:CaptureScreenshot 失败。它没有给我实施的 python 自定义关键字的特定 运行 错误。关于我的实施是否会非常感激的反馈!

您的代码示例没有导入 Selenium2Library 的行,但您经常从那里调用关键字。也许您忘记将其包含在示例中。

因为CustomSeleniumLibrary继承自Selenium2Library,所以不需要指定Selenium2Library。尝试删除 Selenium2Library 导入和所有 Selenium2Library 和 CustomSeleniumLibrary 前缀。我认为他们引用了不同的浏览器实例。

我测试了你的自定义关键字,它工作正常:它在它应该的地方失败了,并制作了一个漂亮的屏幕截图。

*** Settings ***
Library           CustomSeleniumLibrary.py
Suite Teardown    Close All Browsers

*** Test Cases ***
Whosebug
    Open Browser    http://www.google.com/   Chrome
    Page Should Contain Element    btnK
    Get Ok Button    foo