欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

rf BuiltIn.run_keyword 传入关键字参数(字典列表)

程序员文章站 2022-04-23 22:38:43
...

传入关键字参数--dict or list(设置变量再引用)

上一篇,我们介绍了如何传入简单关键字,但是想传入字典和列表会出现问题

# 修改do_request 关键字
# 将allow_redirects设置为False, timeout=3, 设置一个headers
@keyword
def do_request():
    alias = 'session'
    url = 'https://google.com/'
    headers = {

        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
        "Connection": "keep-alive",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest"
    }
    bi = BuiltIn()
    bi.run_keyword('create_session', alias, url)

    # args = [alias, '/', headers, None, None, None, None, 'timeout=3']
    # args = [alias, '/', headers, 'allow_redirects=False', 'timeout=3']
    
    # args = [alias, '/', 'headers={}'.format(headers), 'allow_redirects=False', 'timeout=3'] #报错
 
    test.keywords.create(assign=['${headers}'], name='Set Variable', args=[headers])
    args = [alias, '/', "headers=${headers}", 'allow_redirects=False', 'timeout=3']
    response = bi.run_keyword('get_request', *args)
    logger.info(response.status_code)
    logger.info(response.text)

运行结果

rf BuiltIn.run_keyword 传入关键字参数(字典列表)