rebotframework与BuiltIn(二)

BuiltIn常用关键字(二)

1.Evaluate

作用:执行python表达式,并将执行结果返回。很强大,通过它可以使用 Python 语言中所提供的方法

语法:

Name:Evaluate
Source:BuiltIn
Arguments:[ expression | modules=modules | namespace=namespace ]
参数说明:

l expression:要执行的python表达式

l modules:指定要导入的python模块,多个的话用逗号分隔

l namespace:命名空间

示例:

***Variables ***
${result} 3.14

*** Test Cases ***
Example
# 用字符串’3.14’比较,返回False
${status} = Evaluate 0 < ${result} < 10
# 转换为数字类型的3.14
e x p = E v a l u a t e f l o a t ( {exp}= Evaluate float( exp=Evaluatefloat({result} )
Should Be Equal ${exp} ${3.14}
${random} = Evaluate random.randint(0, sys.maxsize) modules=random, sys
# 生成一个字典 {‘x’: 4, ‘y’: 2}
n s = C r e a t e D i c t i o n a r y x = {ns} = Create Dictionary x= ns=CreateDictionaryx={4} y=${2}
# 将字典作为命名空间,用字典中的数据进行运算
r e s u l t = E v a l u a t e x ∗ 10 + y n a m e s p a c e = {result} = Evaluate x*10 + y namespace= result=Evaluatex10+ynamespace={ns}
执行结果:

2.Run Keyword If

作用:如果给出的判断条件满足,就执行给出的关键字,相当于python中的if

语法:

Run Keyword If 判断条件 其他关键字

… ELSE IF 判断条件 其他关键字

… ELSE 判断条件 其他关键字

注意:ELSE IF或者ELSE前面的3个点不能省略

示例:

*** Test Cases ***
If Test Int
# 判断数字类型
${var} Set Variable ${3}
Run Keyword If ${var}<0 Log -1
… ELSE IF v a r = = 0 L o g 0... E L S E I F 1 < = {var}==0 Log 0 ... ELSE IF 1<= var==0Log0...ELSEIF1<={var}<=10 Log 1
… ELSE Log 2

If Test Str
# 判断字符串类型
v a r S e t V a r i a b l e a a a R u n K e y w o r d I f ′ {var} Set Variable aaa Run Keyword If ' varSetVariableaaaRunKeywordIf{var}’==‘aaa’ Log equal
… ELSE Log different

If Test 2 Condtions
# 2个判断条件
# 默认都是字符串类型
@{list} Create List 1 2 3
Log ${2} in @{list}
Run Keyword If ${2}in@{list} and ${3}in@{list} Log 1111
… ELSE Log 2222
执行结果:

说明:

  1. 变量 v a r 值 为 字 符 或 者 布 尔 类 型 时 , 在 判 断 条 件 中 其 需 要 加 单 引 号 或 双 引 号 , 为 ’ {var}值为字符或者布尔类型时,在判断条件中其需要加单引号或双引号,为’ var{var}’或者”${var}”

  2. 在判断条件中,与变量${var}对比的字符常量也需要加单引号或双引号;

  3. if的条件可以有多个,and表示且也就是同时满足所有条件,or表示或某个满足即可

  4. RF中默认都在字符串类型,所以最后一个测试用例返回的是2222

  5. For Loop

作用:for循环,通过“:for”定义 for 循环;in range 用于指定循环的范围。

语法:

旧语法:

:FOR变量 IN 序列(or 列表)
\ 关键字 参数值

:FOR变量 IN RANGE 循环限量
\ 关键字 参数值
新语法:

FOR变量 IN 序列(or 列表)
关键字 参数值
END

FOR变量 IN RANGE 循环限量
关键字 参数值
END
区别:

  1. 新语法FOR前面补需要写冒号:

  2. FOR循环中的关键字前不需要加反斜杠;

  3. FOR循环结束用END关键字标识。

注意:

  1. Robot Framework 3.1 支持以上2种形式的for循环,有的工具甚至即使用新语法也会自动转换为旧的语法,所以2种都要了解一下。但是尽量用新语法,因为3.2版本将只支持新语法;

  2. FOR循环关键字必须是大写,否则会提示语法错误

示例1:新旧语法对比

*** Test Cases ***
Example For New
FOR ${animal} IN cat dog
Log ${animal} console=true
Log 2nd keyword console=true
END
Log Outside loop console=true
Example For Old
:FOR ${animal} IN cat dog
\ Log ${animal} console=true
\ Log 2nd keyword console=true
Log Outside loop console=true

执行结果:

示例2:for循环遍历多个变量

*** Test Cases ***
Example For Three Loop Variables
FOR ${index} ${english} ${finnish} IN
… 1 cat kissa
… 2 dog koira
… 3 horse hevonen
Log Many ${english} ${finnish} ${index} console=true
END

示例3:For-in-range,类似于python中的range

注意:FOR…IN RANGE必须要大写

*** Test Cases ***
Only upper limit
[Documentation] Loops over values from 0 to 9
FOR ${index} IN RANGE 10
Log ${index}
END

Start and end
[Documentation] Loops over values from 1 to 10
FOR ${index} IN RANGE 1 11
Log ${index}
END

Also step given
[Documentation] Loops over values 5, 15, and 25
FOR ${index} IN RANGE 5 26 10
Log ${index}
END

Negative step
[Documentation] Loops over values 13, 3, and -7
FOR ${index} IN RANGE 13 -13 -10
Log ${index}
END

Arithmetic
[Documentation] Arithmetic with variable
${var} Set Variable 3
FOR ${index} IN RANGE ${var} + 1
Log ${index}
END

Float parameters
[Documentation] Loops over values 3.14, 4.34, and 5.54
FOR ${index} IN RANGE 3.14 6.09 1.2
Log ${index}
END
执行结果:

Starting test: For In Range Test.Only upper limit
20190712 10:32:39.295 : INFO : 0
20190712 10:32:39.295 : INFO : 1
20190712 10:32:39.295 : INFO : 2
20190712 10:32:39.295 : INFO : 3
20190712 10:32:39.311 : INFO : 4
20190712 10:32:39.311 : INFO : 5
20190712 10:32:39.311 : INFO : 6
20190712 10:32:39.311 : INFO : 7
20190712 10:32:39.311 : INFO : 8
20190712 10:32:39.311 : INFO : 9
Ending test: For In Range Test.Only upper limit

Starting test: For In Range Test.Start and end
20190712 10:32:39.327 : INFO : 1
20190712 10:32:39.327 : INFO : 2
20190712 10:32:39.327 : INFO : 3
20190712 10:32:39.327 : INFO : 4
20190712 10:32:39.342 : INFO : 5
20190712 10:32:39.342 : INFO : 6
20190712 10:32:39.342 : INFO : 7
20190712 10:32:39.342 : INFO : 8
20190712 10:32:39.342 : INFO : 9
20190712 10:32:39.342 : INFO : 10
Ending test: For In Range Test.Start and end

Starting test: For In Range Test.Also step given
20190712 10:32:39.358 : INFO : 5
20190712 10:32:39.358 : INFO : 15
20190712 10:32:39.358 : INFO : 25
Ending test: For In Range Test.Also step given

Starting test: For In Range Test.Negative step
20190712 10:32:39.374 : INFO : 13
20190712 10:32:39.374 : INFO : 3
20190712 10:32:39.374 : INFO : -7
Ending test: For In Range Test.Negative step

Starting test: For In Range Test.Arithmetic
20190712 10:32:39.374 : INFO : ${var} = 3
20190712 10:32:39.374 : INFO : 0
20190712 10:32:39.374 : INFO : 1
20190712 10:32:39.389 : INFO : 2
20190712 10:32:39.389 : INFO : 3
Ending test: For In Range Test.Arithmetic

Starting test: For In Range Test.Float parameters
20190712 10:32:39.389 : INFO : 3.14
20190712 10:32:39.389 : INFO : 4.34
20190712 10:32:39.405 : INFO : 5.54
Ending test: For In Range Test.Float parameters

示例4:For-in-enumerate

和python中的枚举类似,可同时遍历下标和值

注意:FOR…IN ENUMERATE 必须要大写

*** Variables ***
@{LIST} a b c

*** Test Cases ***
Manage index manually
${index} = Set Variable -1
FOR ${item} IN @{LIST}
${index} = Evaluate ${index} + 1
Log Many ${index} ${item}
END

For-in-enumerate
FOR ${index} ${item} IN ENUMERATE @{LIST}
Log Many ${index} ${item}
END
执行结果:

Starting test: For In Enumerate Test.Manage index manually
20190712 10:42:12.201 : INFO : ${index} = -1
20190712 10:42:12.201 : INFO : ${index} = 0
20190712 10:42:12.201 : INFO : 0
20190712 10:42:12.201 : INFO : a
20190712 10:42:12.201 : INFO : ${index} = 1
20190712 10:42:12.201 : INFO : 1
20190712 10:42:12.201 : INFO : b
20190712 10:42:12.217 : INFO : ${index} = 2
20190712 10:42:12.217 : INFO : 2
20190712 10:42:12.217 : INFO : c
Ending test: For In Enumerate Test.Manage index manually

Starting test: For In Enumerate Test.For-in-enumerate
20190712 10:42:12.217 : INFO : 0
20190712 10:42:12.217 : INFO : a
20190712 10:42:12.217 : INFO : 1
20190712 10:42:12.217 : INFO : b
20190712 10:42:12.217 : INFO : 2
20190712 10:42:12.217 : INFO : c
Ending test: For In Enumerate Test.For-in-enumerate

  1. Continue For Loop

作用:用在for循环中,表示跳过当前循环,开始下一次循环,相当于python中的continue。

语法:

Name:Continue For Loop
Source:BuiltIn
Arguments:[ ]

示例:如果值为2则跳出本次循环,所以打印的结果是1和3

*** Test Cases ***
Example1
@{list} Create List 1 2 3
:FOR i I N @ l i s t   C o n t i n u e F o r L o o p I f ′ {i} IN @{list} \ Continue For Loop If ' iIN@list ContinueForLoopIf{i}’==‘2’
\ Log ${i}
执行结果:

5.Exiting For Loop

作用:终止for循环。

语法:

Name:Exit For Loop
Source:BuiltIn
Arguments:[ ]
示例:

*** Test Cases ***
Exit Example
${text} = Set Variable ${EMPTY}
FOR v a r I N o n e t w o R u n K e y w o r d I f ′ {var} IN one two Run Keyword If ' varINonetwoRunKeywordIf{var}’ == ‘two’ Exit For Loop
${text} = Set Variable t e x t {text} text{var}
END
Should Be Equal ${text} one
6. 断言

*** Test Cases ***
断言一
#01、should contain 、 should not contain 与should contain x times
@{list1}= create list 3 a ${28} 22 25
@{list2}= set variable 3.0 a ${28} 22 22
@{list3}= create list
${string1}= set variable robot is a good
${name}= set variable robot
should contain ${list2} 3.0
should not contain ${list2} 3
should contain x times ${list2} 22 2

断言二
#should be empty 与 should not be empty
@{list1}= create list 3 a ${28} 22 25
@{list2}= create list
${list3}= create list cq
should be empty ${list2}
should not be empty ${list1}

断言三
#should be equal 与 should not be equal
@{list1}= create list 3 a ${28} 22 22 3.0
should be equal ${list1[3]} ${list1[4]}
should not be equal ${list1[0]} ${list1[5]}

断言四
#Should Be Equal As Numbers 与 Should not Be Equal As Numbers
@{list1}= create list 3 a ${28} 22 3.98 3.0
should be equal as numbers ${list1[0]} ${list1[5]}
should not be equal as numbers ${list1[0]} KaTeX parse error: Expected 'EOF', got '#' at position 17: …list1[4]} #̲说明:{list1[0]}=3,忽略精度,与3.0相等;忽略精度,1与3.98还是不相等的

断言五
#Should Be Equal As Integers与Should not Be Equal As Integers
@{list1}= create list 3 a ${28} 22 3.98 3.0 3 ${3}
should be equal as Integers ${list1[0]} ${list1[6]}
should not be equal as Integers ${list1[0]} ${list1[3]}

断言六
#Should Be True与Should not Be True
@{list1}= create list 3 a ${28} 22 3.98 3.0 3 ${3}
should be true l i s t 1 [ 0 ] < {list1[0]}< list1[0]<{list1[3]}
should not be true l i s t 1 [ 4 ] < {list1[4]}< list1[4]<{list1[0]}

断言七
#Should start With与Should not start With
${string1}= set variable cq is a girl
should start with ${string1} cq
should not start with ${string1} cd

断言八
#Should End With与Should not End With
${string1}= set variable cq is a girl
should end with ${string1} girl
should not end with ${string1} girls

断言九
#should match与 should not match
@{list1}= create list cq a ${28} 22 3.98 3.0 3 ${3}
should match ${list1[0]} c?
should not match ${list1[0]} a?
模式匹配和shell中的通配符类似,区分大小写,’*'匹配0~无穷多个字符,“?”匹配单个字符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值