英文:
Creating and opening a URL to log in to Website via Basic Auth with Robot Framework/Selenium (Python)
问题 {#heading}
我正在尝试使用Robot Framework和Selenium(Python)登录到Basic Auth。思路是获取出现Basic Auth弹窗的URL,通过自定义函数编辑URL(包括登录数据),然后打开修改后的URL。
Robot Framework脚本将运行而不会失败,但不会打开Basic Auth URL。
以下是Robot Framework脚本。URL指向一个用于测试自动化的游乐场网站。${link_auth}
包含Basic Auth登录链接。
*** 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}
`
这是导入的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
英文:
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
`
答案1 {#1}
得分: 0
你正在给包含链接的元素提供定位器,而不是直接提供链接本身作为字符串。你应该获取a
元素的href
属性,并将其传递给GENERATE BASIC AUTH URL
关键字。所以你的basic auth login
关键字应该如下所示:
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}
附注:如果没有特殊原因使用Selenium2Library
(它不再更新),你应该更新(卸载robotframework-selenium2library
,安装robotframework-seleniumlibrary
,并更改导入)为SeleniumLibrary
。
英文:
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
.