RobotFramework项目yaml配置文件使用

RF支持从文件(.py, .java, .yaml)load变量。以当前工作项目proj为例,proj 自动化项目选用yaml文件作为标准配置文件,因为yaml具有结构分明,可读性强的优点。

配置文件入口

配置文件settings.yaml放置在 autotest4proj/resource 目录下。

入口文件是 autotest4proj/resource/common.robot,导入方法如下。

*** Settings ***

...

Variables         settings.yaml

配置文件结构和环境切换方法

settings.yaml内容如下。

---

automation:

  error_url: "http://192.168.231.10"

  filename: thelittleprince.txt

  host1: "192.168.231.1"

  host2: "192.168.231.2"

  host3: "192.168.231.3"

  login_url: "http://192.168.231.10"

  server: "192.168.231.10"

  ssh_password: 123456

  ssh_port: 22

  ssh_server: "192.168.231.200"

  welcome_url: "http://192.168.231.10/index.html"

legacy:

  error_url: "http://192.168.231.70"

   ...

  welcome_url: "http://192.168.231.70/index.html"

test_env: automation

这种文件结构下,RF加载到的参数和以下方法相同。

*** Variables ***

${test_env}     automation

&{automation}       error_url=xxx    filename=thelittleprince.txt    ...

&{legacy}           error_url=yyy    filename=thelittleprince.txt    ...

其中最后一行的test_env用来指定所选择的的环境。本例中选择automation环境。如果调试过程中需要使用legacy环境,可以有两个方法来切换。

1)直接修改settings.yaml中test_env对应的值。

2)在执行case的时候加上命令行参数 --variable test_env:legacy。RF的参数优先级是 环境变量> 命令行参数 > 其他。

RIDE(图丢了 :))

纯命令行

# robot -v test_env:legacy -v browser:headlesschrome testsuites\login

如何使用配置文件中的参数

如果要在用例或者keyword中使用settings.yaml中的变量,首先要import resource common.robot

*** Settings ***

...

Resource          ../../../resource/common.robot

要使用本例中的环境变量,可以用如下方法。

${${test_env}.login_url}   # 获取test_env指定的环境的login_url

${${test_env}.ssh_server}}    # 获取test_env指定的环境的ssh_server

注意配置文件应是合法的yaml文件。如果文件加载报错,肉眼检查不出问题,可以到 http://www.yamllint.com/ 修正格式。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值