robot framework api使用技巧

创建变量

# test.py
from robot.api import TestSuite
suite = TestSuite('Activate Skynet')
suite.resource.variables.create("${create_var}", "test")

导入库

# test.py
from robot.api import TestSuite
suite = TestSuite('Activate Skynet')
# 已安装
suite.resource.imports.library('OperatingSystem')
suite.resource.imports.library('RequestsLibrary')
# 自己创建的库, 支持相对路径
suite.resource.imports.library('/xxx/xxx/xxx.py')
suite.resource.imports.library('./xxx/xxx.py')

传入关键字参数

# 假设有一个关键字如下
# keywords.py
@keyword("keyword_arguments_test")
def test_keyword_arguments(first, **kwargs):
    '''
    必须传入second这个关键字参数
    '''
    args = kwargs.copy()
    second = args.pop('second', None)
    if not second:
        raise Exception("Need second argument")
    logger.info("First: {}\nSecond: {}".format(first, second))


# test.py
suite.resource.imports.library('./keywords.py')
test = suite.tests.create('Should Activate Skynet', tags=['smoke'])
# pass
test.keywords.create('keyword_arguments_test', args=[
    'first_param', "second={}".format('second_param')])
# fail
test.keywords.create('keyword_arguments_test', args=['first_param'])

处理字符串中的等号

需要反斜杠处理特殊字符: =

# 假设有一个关键字如下
# keywords.py
@keyword("escape_equal")
def test_escape_equal(alias, uri, **kwargs):
    '''
    If uri had symbol: =, robot will think it's keyword
    Example:
        uri = '/api/test/?link=/view'
        robot will think: /api/test/?link was a keyword, will failed
    '''
    pass

# test.py
# fail
test.keywords.create('escape_equal', args=[
    'alias', '/api/test/?link=/view'])
# pass
test.keywords.create('escape_equal', args=[
    'alias', '/api/test/?link\=/view'])

setup/teardown中设置多个已有关键字

# keywords.py
@keyword(name='this_is_a_simple_setup')
def simple_setup(suite):
    bi = BuiltIn()
    bi.run_keyword('Set Environment Variable',
                   'SUITESKYNET', 'activated')
    bi.run_keyword(
        'Environment Variable Should Be Set', 'SUITESKYNET')

# test.py
# 导入关键字文件
suite.resource.imports.library('./keywords.py')
# 直接使用, 声明type=setup, teardown
suite.keywords.create('this_is_a_simple_setup', args=[suite], type='setup')

持续更新ing

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值