Sou*_*rno 2 python configparser robotframework
我有一个使用python和Robotframework脚本混合实现的项目.我的项目Config.ini文件中存储了一堆配置项,如下所示:
[Environment]
Username: username@mail.com
Password: testpassword
[WebUI]
login_url: http://testsite.net/
Run Code Online (Sandbox Code Playgroud)
Python能够使用ConfigManager对象解释上述变量,如下所示:
class MyConfigManager(ConfigManager):
"""
Class to hold all config values in form of variables.
"""
def __init__(self):
super().__init__("dispatch/config.ini")
self._android = Android(self._config)
@property
def username(self):
return self._config.get(_env_section, "Username")
@property
def password(self):
return self._config.get(_env_section, "Password")
config = MyConfigManager()
Run Code Online (Sandbox Code Playgroud)
是否可以将Robotframework中的config.ini导入为变量文件并使用这些值?我正在尝试不为我的Robot脚本提供另一个变量文件.
编辑:
我试图用我的机器人文件做这样的事情:
*** Settings ***
Documentation WebUI Login Tests
Library SeleniumLibrary
Resource common_keywords.robot
Variables config.ini
# ^ will this work?
Default Tags Smoke
Suite Setup Set Selenium Timeout 15seconds
Suite Teardown Close Browser
*** Variables ***
${login_button} class:auth0-lock-submit
*** Test Cases ***
TC001_Login_Test
[Documentation] Open login page, login with credentials in arguments.
Open Browser Confirm Login Page chrome ${login_url}
Provide Input Text name:email ${username}
Provide Input Text name:password ${password}
Find Element And Click ${login_button}
# the three vars ${login_url}, ${username}, ${password} would be from
# config.ini but this isnt working. What am I doing wrong? or is not
# possible to do this?
Run Code Online (Sandbox Code Playgroud)
机器人框架变量文件可以是python代码,因为它们是python,你可以以任何你想要的方式创建变量.
您只需要创建一个返回键/值对字典的python函数.每个键都将成为机器人变量.
例如,对于问题中的数据,如果要创建类似的变量${CONFIG.Environment.username},可以这样做:
import ConfigParser
def get_variables(varname, filename):
config = ConfigParser.ConfigParser()
config.read(filename)
variables = {}
for section in config.sections():
for key, value in config.items(section):
var = "%s.%s.%s" % (varname, section, key)
variables[var] = value
return variables
Run Code Online (Sandbox Code Playgroud)
将其保存到名为"ConfigVariables.py"的文件中,并将其放在测试可以找到的位置.然后你可以像这样使用它:
*** Settings ***
Variables ConfigVariables.py CONFIG /tmp/Config.ini
*** Test cases ***
Example
should be equal ${CONFIG.Environment.username} username@mail.com
should be equal ${CONFIG.Environment.password} testpassword
should be equal ${CONFIG.WebUI.login_url} http://testsite.net/
Run Code Online (Sandbox Code Playgroud)
使用函数定义变量在机器人框架用户指南的" 变量文件 "一节中进行了描述
| 归档时间: |
|
| 查看次数: |
2022 次 |
| 最近记录: |