Evan Wiley
Evan Wiley

Reputation: 13

Robot Framework, integrating custom keywords with Selenium2Library

I am facing issues with adding custom keywords with robot selenium library. I have seen some solutions for adding custom keywords but this approach does not seem to be working for me. The custom keyword implemented in the test case is specified by CustomSeleniumLibrary.Get Ok Button.I have posted the code I have used below and my test case: Custom Keyword implementing 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()

Test that calls custom keyword:

*** 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

After running files, I am getting the following error: CaptureScreenshot failed. It does not give me a specific run error for the python custom keyword implemented. Feedback on if my implementation would be much appreciated!

Upvotes: 1

Views: 3154

Answers (1)

Pekka
Pekka

Reputation: 2280

Your code sample does not have line that imports Selenium2Library but you are constantly calling keywords from there. Maybe you forgot to include it in your sample.

Because CustomSeleniumLibrary inherits from Selenium2Library, you do not need to specify Selenium2Library. Try removing Selenium2Library import and all Selenium2Library and CustomSeleniumLibrary prefixes. I think they reference different browser instances.

I tested your custom keyword and it works fine: it fails where it should and makes a nice screenshot.

*** 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

Upvotes: 3

Related Questions