一篇搞定RobotFramework中字典和列表的取值和判断和evaluate用法

RF中一些关键字的用法可以参考:robotframework官方文档

http://robotframework.org/robotframework/latest/libraries/BuiltIn.html

test.robot

复制代码

*** Keywords ***
#自定义变量: scalar  list  dict
getval
   ${val1}  set variable  ${22}
   [Return]  ${val1}
getdict
   &{dict2}  create dictionary   id=1    name=Lara
   [Return]  ${dict2}
getlist
   @{list2}   create list  hello  world
   [Return]  ${list2}


*** Variables ***
#共享变量(变量表)
@{list1}    hello  world
&{dict1}     name=auto   pwd=sdfsdfsdf

*** Test Cases ***
#测试用例
测试1--By Lara should be true 参数是list
    #得到关键字getlist返回的列表list
    ${retlist}   getlist

    log to console  ${list1}
    log to console  ${retlist}
    log   判断列表相等
    should be true  $list1==$retlist

测试2--should be true 参数是dict
    #得到关键字getdict返回的字典dict
    ${retdict}   getdict

    log to console  ${dict1}
    log to console  ${retdict}
    log   判断字典相等
    should be true  $dict1==$retdict

测试3--取字典&{dict1}中name属性的值
    #打印auto   常用
    log to console  &{dict1}[name]
    #打印auto
    log to console  ${dict1}[name]
    #打印auto
    log to console  ${dict1['name']}

    #打印22   常用
    run keyword if  $dict1['name']=='auto'   log to console   22

测试4--取字典中所有的key[注意evaluate用法!!!]
    ${retkeys}  evaluate  [one for one in $dict1]
    #这种方式也可以
    #${retkeys}  evaluate  [one for one in ${dict1}]
    log to console  -----------
    log to console  ${retkeys}

测试5--拓展evaluate用法
    ${retval}  getval
    ${sum}  evaluate   ${retval}+1
    log to console  ${sum}
#总结:
#1.run keyword if <condition>
#2.should be true <condition>
#后面紧跟python表达式,取字典中的值要去掉大括号,key要带引号.

#3.evaluate 会把你想要计算的表达式直接传递给Python,并把Python的计算结果返回给你。这是最经常要用到的。
#跟python中的eval函数用法一致

复制代码

运行结果:

“学习必须如蜜蜂一样,采过许多花,这才能酿出蜜来。”

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
RobotFramework基础教程 基础教程 安装 安装 Pycharm 安装插件 Pycharm File->Settings->Plugins 搜索并安装插件 IntelliBot Run Robot Framework Testcase RunRobot Framework 基本格式 基本格式 Settings Library Resource Force Tags Defalut Tags(未定义任何标签时) Test Setup Test TearDown Test Template Test Timeout Variables Keywords Test Cases [Tags] [Template] [Documention] [Timeout] [Return] [Arguments] [Setup] [TearDown] Suite(⽂件夹) __init__⽂件 library为第三⽅库或⾃定义库,resource为⾃定义关键字集合,variables为⾃定义变量集合 简单⽰例 简单⽰例 Selenium Demo pip install robotframework robotframework-selenium2library *** Setttings *** ** Test Setup ** ... ** Test Teardown ** ... *** Keywords *** loginwebsite [Arguments] ${username} ${password} Open Browser http://... chrome [Return] ${lessons} *** Settings *** Library SeleniumLibrary *** Test Cases *** test rf log hello robot framework Baidu search case Open Browser http://www.baidu.com chrome Input text id=kw robot framework Click button id=su Close Browser baidu_search.robot Headless Demo MySQL操作 操作Demo *** Settings *** Documentation Simple examle using SeleniumLibrary Library SeleniumLibrary *** Variables *** ${URL} http://www.baidu.com ${BROWSER} Chrome *** Keywords *** Baidu Search [Arguments] ${search_key} Input text id:kw ${search_key} click button id:su Evaluate time.sleep(2) time ${title} Get title [Return] ${title} *** Test Case *** case1 Open Browser ${URL} ${BROWSER} ${title} Baidu Search robot framework Should contain ${title} robot framework_百度搜索 close Browser case2 Open Browser ${URL} ${BROWSER} ${title} Baidu Search selenium Should contain ${title} selenium_百度搜索 close Browser *** Settings *** Documentation This example demonstrates how to use current library Library SeleniumLibrary *** Test cases *** Open Browser with Chrome options in headless mode ${options} Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver Call Method ${options} add_argument --start-maximized Call Method ${options} add_argument --headless Call Method ${options} add_argument --disable-gp

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值