【问题标题】:Robot Framework: conditional import of resourceRobot Framework:有条件地导入资源
【发布时间】:2019-04-21 21:02:19
【问题描述】:
是否可以在机器人框架中进行资源文件的条件导入?根据测试环境,我想导入具有不同变量的资源文件。该变量可以从机器人 CLI 中读取(例如机器人 --variable VAR:production myTestSuite)
举例说明:
*** Settings***
Resource variables_url_environment_a.robot
Resource variables_url_environment_b.robot
Run keyword if '${VAR}'=='production' Import resource variables_url_environment_b.robot
【问题讨论】:
标签:
import
frameworks
robotframework
【解决方案1】:
您可以使用具有不同环境变量的参数文件,您可以使用类似
QA.args
--variable Enviroment:http://sample.url/QA:1111
--variable USER:John
--variable PASSWORD:John
然后在你的 Robot.test 中
*** Test Cases ***
Run Argument File
Go To ${Enviroment}
Login With User ${USER} ${PASSWORD}
注意:这只是参数文件的一个示例,使用 Login with User 不是实际的关键字
然后执行命令
robot --argumentfile "QA.args" tests
您也可以在命令行上覆盖变量。
robot --argumentfile "QA.args" --variable Enviroment:http://sample.url/Staging:1111 tests
【解决方案2】:
您可以在导入文件的名称中使用变量。
如果您使用的是 maven,请从 pom.xml 文件中设置变量的值。
如下所示,其中 ${PLATFORM} 是一个变量:
*Settings*
Resource ../platforms/settings_${PLATFORM}.tsv
Resource ../platforms/settings_default.tsv
*Variables*
${PLATFORM} ${ENV_PLATFORM}
下面是来自POM.xml的sn-p
....
<env.platform>Platform1.</env.platform>
....
<configuration>
<variables>
<param>ENV_PLATFORM:${env.platform}</param>
</variables>
</configuration>
....
此外,您可以通过这种方式从 jenkins 传递平台的值(如果使用)
使用 -Denv.platform=Platform_5
【解决方案3】:
我认为在 Robot Framework 中不可能以您喜欢的方式进行条件 imort。
但是,您可以做的不是将环境文件作为资源导入,而是将它们作为 --variablefile
传递给您的测试
我会怎么做?
variables_url_environment_a.py
msg='env a'
variables_url_environment_b.py
msg='env b'
Test.robot
*** Settings ***
*** Variables ***
*** Test Cases ***
print message to console
print msg
*** Keywords ***
print msg
log to console ${msg}
现在只需创建一个简单的 python 脚本,按照您需要的环境运行您的测试套件。
Python_run_script
import subprocess
var='Production'
command_a='pybot -V variables_url_environment_a.py Test.robot'
command_b='pybot -V variables_url_environment_a.py Test.robot'
if var='Production':
procId = subprocess.Popen(command_a,stdout = subprocess.PIPE)
else:
procId = subprocess.Popen(command_b,stdout = subprocess.PIPE)
有关如何使用 --variablefile 的更多信息,您也可以参考下面的 url
https://automationlab0000.wordpress.com/2018/11/20/how-to-pass-python-variable-file-in-robotframework/
【解决方案4】:
Run Keyword If '${VAR}' == 'iOS' Import Library a.py