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

时间:2015-07-20 14:28:27

标签: python frameworks keyword robotframework

我在使用机器人selenium库添加自定义关键字时遇到了问题。我已经看到了一些添加自定义关键字的解决方案,但这种方法对我来说似乎并不适用。在测试用例中实现的自定义关键字由CustomSeleniumLibrary.Get Ok Button指定。我已经发布了我在下面使用的代码和我的测试用例: 实现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 custom关键字的特定运行错误。关于我的实施是否会受到高度赞赏的反馈意见!

1 个答案:

答案 0 :(得分:3)

您的代码示例没有导入Selenium2Library的行,但您不断地从那里调用关键字。也许你忘了将它包含在你的样本中。

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

我测试了你的自定义关键字,它运行正常:它失败的地方并且制作了一个很好的截图。

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

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