配置解析Robotframework
Config parsing Robotframework
我有一个混合使用 python 和 Robotframework 脚本实现的项目。我的项目 Config.ini 文件中存储了一堆配置项,如下所示:
[Environment]
Username: username@mail.com
Password: testpassword
[WebUI]
login_url: http://testsite.net/
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()
是否可以将 config.ini 作为变量文件导入 Robotframework 并使用这些值?我正在尝试不为我的机器人脚本添加另一个变量文件。
编辑:
我正在尝试用我的机器人文件做这样的事情:
*** 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?
机器人框架变量文件可以是 python 代码,并且因为它们是 python,所以您可以根据需要以任何方式创建变量。
您只需要创建一个 python 函数,returns 一个包含 key/value 对的字典。每个键都将成为一个机器人变量。
例如,对于你问题中的数据,如果你想创建像${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
将其保存到名为 "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/
机器人框架用户指南中标题为 Variable Files
的部分描述了使用函数定义变量
您仍然可以使用您的 config.ini 文件,如下所示:
[Environment]
Username = username@mail.com
Password = testpassword
[WebUI]
login_url = http://testsite.net/
简单地创建一个 python 文件 'MyVariables.py 如下获取变量:
import sys
import os
import ConfigParser
try:
dir_path = os.path.dirname(os.path.realpath(__file__))
config_file = os.path.join(dir_path,'config.ini')
config = ConfigParser.ConfigParser()
config.read(config_file)
login_url = config.get('WebUI','login_url')
Username = config.get('Environment','Username')
Password= config.get('Environment','Password')
except (ConfigParser.Error, ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
sys.stderr.write("Error: {0}.\n".format(e))
sys.exit()
请注意,您在 py 文件中定义的变量与它们在指向相应部分的配置文件中的命名相同,例如在 python 文件中我们定义变量 'login_url' 使用配置文件部分 'WebUI' 和变量 'login_url'
在你的机器人文件中,你只需要在设置中设置变量文件,你就可以在你的测试用例中使用定义的变量:
*** Settings ***
Library Selenium2Library
Variables MyVariables.py
*** Test Cases ***
Testing Variables
log variables
您的报告将类似于:
15:02:41.615 INFO ${login_url} = http://testsite.net/
15:02:41.616 INFO ${Password} = testpassword
15:02:41.616 INFO ${Username} = username@mail.com
我有一个混合使用 python 和 Robotframework 脚本实现的项目。我的项目 Config.ini 文件中存储了一堆配置项,如下所示:
[Environment]
Username: username@mail.com
Password: testpassword
[WebUI]
login_url: http://testsite.net/
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()
是否可以将 config.ini 作为变量文件导入 Robotframework 并使用这些值?我正在尝试不为我的机器人脚本添加另一个变量文件。
编辑:
我正在尝试用我的机器人文件做这样的事情:
*** 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?
机器人框架变量文件可以是 python 代码,并且因为它们是 python,所以您可以根据需要以任何方式创建变量。
您只需要创建一个 python 函数,returns 一个包含 key/value 对的字典。每个键都将成为一个机器人变量。
例如,对于你问题中的数据,如果你想创建像${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
将其保存到名为 "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/
机器人框架用户指南中标题为 Variable Files
的部分描述了使用函数定义变量您仍然可以使用您的 config.ini 文件,如下所示:
[Environment]
Username = username@mail.com
Password = testpassword
[WebUI]
login_url = http://testsite.net/
简单地创建一个 python 文件 'MyVariables.py 如下获取变量:
import sys
import os
import ConfigParser
try:
dir_path = os.path.dirname(os.path.realpath(__file__))
config_file = os.path.join(dir_path,'config.ini')
config = ConfigParser.ConfigParser()
config.read(config_file)
login_url = config.get('WebUI','login_url')
Username = config.get('Environment','Username')
Password= config.get('Environment','Password')
except (ConfigParser.Error, ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
sys.stderr.write("Error: {0}.\n".format(e))
sys.exit()
请注意,您在 py 文件中定义的变量与它们在指向相应部分的配置文件中的命名相同,例如在 python 文件中我们定义变量 'login_url' 使用配置文件部分 'WebUI' 和变量 'login_url'
在你的机器人文件中,你只需要在设置中设置变量文件,你就可以在你的测试用例中使用定义的变量:
*** Settings ***
Library Selenium2Library
Variables MyVariables.py
*** Test Cases ***
Testing Variables
log variables
您的报告将类似于:
15:02:41.615 INFO ${login_url} = http://testsite.net/
15:02:41.616 INFO ${Password} = testpassword
15:02:41.616 INFO ${Username} = username@mail.com