gpt4 book ai didi

python - 配置解析 Robotframework

转载 作者:行者123 更新时间:2023-11-28 20:35:02 30 4
gpt4 key购买 nike

我有一个混合使用 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()

是否可以将 Robotframework 中的 config.ini 作为变量文件导入并使用这些值?我尽量不为我的机器人脚本准备另一个变量文件。

编辑:

我正在尝试用我的机器人文件做这样的事情:

*** 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 函数。每个键都将成为一个机器人变量。

例如,对于你问题中的数据,如果你想创建像${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 的部分描述了使用函数定义变量

关于python - 配置解析 Robotframework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47684027/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com