Search code examples
pythonselenium-webdriverrobotframeworkbasic-authentication

Creating and opening a URL to log in to Website via Basic Auth with Robot Framework/Selenium (Python)


I'm trying to login to a Basic Auth, using Robot Framework and Selenium (Python). The idea is to get the URL, where the Basic Auth popup appears, edit the URL (include the login data) in a custom function and then open the modified URL.

The Robot Framework script will run without FAIL, but it won't open the Basic Auth URL.

This is the Robot Framework script. The URL points to a playground website for test automation. ${link_auth} contains the link to the Basic Auth login.

*** Settings ***
Library    Selenium2Library
Library    robot_test_func.py


*** Variables ***

${url}    https://the-internet.herokuapp.com/
${link_auth}    xpath://a[contains(text(), 'Basic Auth')]

*** Test Cases ***

login to page with basic auth
    Open Browser    ${url}    ${browser}
    basic auth login    admin    admin


*** Keywords ***

basic auth login
    [Arguments]    ${user}    ${pw}
    ${link_auth}    GENERATE BASIC AUTH URL    ${link_auth}    ${user}    ${pw}
    Go To    ${link_auth}

This is the imported robot_test_func.py

try:
    from robot.libraries.BuiltIn import BuiltIn
    from robot.libraries.BuiltIn import _Misc
    import robot.api.logger as logger
    from robot.api.deco import keyword
    ROBOT = False
except Exception:
    ROBOT = False


@keyword("GENERATE BASIC AUTH URL")
def basicAuthURL(link, user, password):
    parts_lst = link.partition("://")
    link = "{}{}{}:{}@{}".format(parts_lst[0], parts_lst[1], user, password, parts_lst[2])
    print(link)
    return link

Solution

  • You are giving locator to the element that contains link instead of giving link itself as string. You should get href attribute of a element and pass it to GENERATE BASIC AUTH URL keywords. So your basic auth login keyword should look like:

    basic auth login
        [Arguments]    ${user}    ${pw}
        ${href}    Get Element Attribute    ${link_auth}    href
        ${link_auth}    GENERATE BASIC AUTH URL    ${href}    ${user}    ${pw}
        Go To    ${link_auth}
    

    Side-note: If there is no specific reason to use Selenium2Library (it's no longer updated) you should update (uninstall robotframework-selenium2library, install robotframework-seleniumlibrary, change import) to SeleniumLibrary.