![](https://i-blog.csdnimg.cn/blog_migrate/13c05846d679c6e385dc62b069e32010.png)
*** Keywords ***
comment Function_Keyword
return_keywork
[Arguments] ${element1} @{items1}
${index} = Set Variable ${0}
FOR ${item} IN @{items1}
Run Keyword If '${item}'=='${element1}' Return From Keyword ${index}
${index}= Set Variable ${index+1}
END
Return From Keyword ${-1}
log_message
[Arguments] ${your_log}
Run Keyword And Return log ${your_log}
log_message_boolean
[Arguments] ${your_log}
Run Keyword And Return Status Should Be Equal ${your_log} hello
*** Settings ***
Documentation 初始文档
Suite Setup Set Tags First
Suite Teardown Run Keyword If All Tests Passed log success # success
Test Teardown Run Keyword If Test Failed log test fail
Force Tags ALL
Default Tags sanity
Resource ../My_Keyword/Stability_Keyword.robot
Resource ../My_Keyword/Function_Keyword.robot
Resource ../My_variables/Function_var.robot
Library SeleniumLibrary
Library Selenium2Library
*** Variables ***
@{LIST1infirstSuit} foo baz
*** Test Cases ***
BuiltIn_keyword
[Tags] A-
[Setup]
comment 标量 列表 字典, 分别使用语法格式 ${SCALAR}, @{LIST} 和 &{DICT} 来定义
comment 1.call method 调用一个方法
Comment Call Method ${hashtable} put myname myvalue
Comment ${isempty} Call Method ${hashtable} isEmpty
# ${my logout} Evaluate modules=print_string
# Call Method ${my logout} echo '' qwert
comment 2.catenate 合并字符串
${my_str} Catenate hello world !
log ${my_str} # hello world !
${my_str2} Catenate SEPARATOR=_ hello world !
log ${my_str2} # hello_world_!
${my_str3} Catenate SEPARATOR= hello world !
log ${my_str3} # helloworld!
comment 3.Convert to binary 将给定对象转换成二进制字符串
${to_binary} Convert To Binary 123
log ${to_binary} # 1111011
comment 4.Convert to boolean 将给定的对象转成布尔值
${to_bool} Convert To Boolean abcd
log ${to_bool} # True
comment 5.convert to Bytes 转成字节
comment 6.convert to Hex 转16进制值
comment 7. convert to integer 转整数
${my_int} Convert To Integer 123
log ${my_int} # 123
${my_B} Convert To Bytes abc
log ${my_B} # abc
comment 8.convert to number 转浮点数,可以指定几位小数
${my_float} Convert To Number 90.123 2
log ${my_float} # 90.12 保存两位小数
comment 9.convert to string 转成一个unicode string
${my_str} Convert To String 中国
log ${my_str} # 中国
comment 10.create dictionary 创建并返回一个字典
&{dict} Create Dictionary first_key=first_value second_key=second_value # key= value syntax
log many &{dict}
&{dict2} Create Dictionary key3 vaule3 key4 value4 # separate key and value
log many &{dict2}
&{dict3} Create Dictionary ${1}=${2} &{dict} first_key=new_value # using variables
log many &{dict3}
comment 11.create list 返回一个列表
@{list} Create List a b c
log many @{list} # @{list} = [ a | b | c ] a b c
${scalar} Create List d e f
log ${scalar} # ['d', 'e', 'f']
${ints} Create List ${1} ${2} ${3}
log ${ints} # [1, 2, 3]
comment 12.evaluate 计算一个表达式然后返回结果
${random} Evaluate random.randint(0,51)
log ${random}
${ns} Create Dictionary x=${4} y=${2}
${result} Evaluate x*10+y namespace=${ns}
comment 13.exit for loop 退出循环
@{for_list} Create List a b EXIT d
FOR ${for_var} IN @{for_list}
Run Keyword If '${for_var}' == 'EXIT' Exit For Loop
... ELSE log ${for_var}
END
comment 14.exit for loop if 条件为true退出循环
FOR ${for_var} IN @{for_list}
Exit For Loop If '${for_var}'=='EXIT'
log ${for_var}
END
comment 15.fail 发生错误,显示错误信息,停止执行本case之后的逻辑,不影响其他case tag将报错归类加一个标签
Comment FOR ${for_var} IN @{for_list}
Comment \ Run Keyword If '${for_var}'=='EXIT' fail 列表中出现exit,发生错误,停止执行 tag=FAIL_EXIT
... ELSE log ${for_var}
Comment END
comment 16.Fatal Error 整个测试全部停止
comment 17.Get Count 统计目标字符出现在容器里的次数
${count_list} Create List 1 2 2 1 3
${count} Get Count ${count_list} 1
log ${count}
comment 18.Get Longth 返回所给容器的长度,整型
${length} Get Length hello,world!
Should Be Equal As Integers ${length} 12
log ${length}
${len_list}= Create List hello, world!
${len}= Get length ${len_list}
Should Be Equal As Integers ${len} 2
comment 19.Get library instance 实例化一个包
&{all libs}= Get Library Instance all=True
log many &{all libs}
comment 20.get time 根据指定格式获取时间
${time} Get Time # 2021-12-14 14:04:22
log ${time}
${sec} Get Time epoch
log ${sec}
${year} Get Time return year
log ${year}
${yyyy} ${mm} ${dd} Get Time year,month,day
log many ${yyyy} ${mm} ${dd}
@{time} Get Time year month day hour min sec
${y} ${s} Get Time seconds and year
comment 21.Get Variable Value 返回变量值,变量不存在返回变量后面的值(default/none/任意设定的值)
${my_varia} Create List q w e
${get_my_varia} Get Variable Value ${my_varia} default # ${get_my_varia} = ['q', 'w', 'e']
${get_no_my_varial} Get Variable Value ${no_my_varial} no exist # ${get_no_my_varial} = no exist
${z} Get Variable Value ${z} # ${z} = None
comment 22.get variables 返回当前范围所有变量的字典
${log_variables} Get Variables
log ${log_variables}
comment 23.import library 动态导入一个库,只在本case内生效,该库由函数组成,不要写类(不知道类怎么用)
Import Library ${CURDIR}/print_string.py
${import_str} echo qqqq
log ${import_str} # qqqq
comment 24.import resource 绝对路径导入一个资源文件,只在这个case生效
comment 25.import variables 绝对路径导入一个变量文件
comment 26.keyword should exist 判断关键字是否存在,不存在报错,无返回值
Keyword Should Exist echo
comment 27.length should be 长度判断,无返回值,长度错误,报错
Length Should Be 123456 6
comment 28.log \ log many log后面打印一条消息,log many后面打印多条消息
log abc console=yes
log many a b c
comment 29.log to console 打印到console控制台
comment 30.log variables 获取当前case范围内所有变量
Comment Log Variables
Comment 31.No Operation 什么都不做,相当于pass
comment 32.Pass Execution message tag 以pass状态跳过当前test,setup or teardown,后面的语句不执行
Comment Pass Execution skip class A-
Comment log 1
comment 33.pass execution if message tag 以pass的状态有条件的跳过当前test,setup,teardown,后面的语句不执行
Comment @{str_ing} Create List 2 3 4 5 6
Comment FOR ${i} IN @{str_ing}
Comment \ Pass Execution If '${i}'=='4' Correct value was found class A-
Comment \ log ${i}
Comment END
Comment LOG 111222 # 后面语句不执行
comment 34.regexp escape 回转义的每个参数字符串,以用作正则表达式。 ##############不懂
comment 35.reload library 重新加载指定库中的所有关键字
Reload Library SSHLibrary # 输出Reloaded library SSHLibrary with 34 keywords.
comment 36.remove tags 仅在log和report中移除tag,ride中仍然存在
Remove Tags mytag
comment 37.repeat keyword 重复执行关键字
Repeat Keyword 3 times log 123
comment 38.replace variables 在给定的text文件中替换变量的值 #############不会
Comment log ${CURDIR}
Comment ${template} ${/}
Comment ${NAME}= Set Variable Robot
Comment ${message} Replace Variables ${CURDIR}/template.txt
Comment Should Be Equal ${message} Hello Robot!
comment 39.return from keywork 在关键字中使用,从关键字中跳出,不执行该关键字后面的逻辑 本例在index=1时结束关键字后面的步骤
@{list} Create List foo baz
${index} return_keywork baz @{list}
Should Be Equal ${index} ${1} # Returning from the enclosing user keyword.${index} = 1
${index} return_keywork non existing @{list}
Should Be Equal ${index} ${-1} # Returning from the enclosing user keyword.${index} = -1
comment 40.return from keyword if 满足后面的条件,返回用户关键字
comment 41.run keyword 执行后面的关键字,关键字以参数的形式给定,是一个变量
comment 42.run keyword and condtinue on failure 执行后面的关键字,即使错误也继续执行
Comment Run Keyword And Continue On Failure Fail this is a example
comment 43.run keyword and exepect error 执行关键字,检查预期的错误是否发生
${log_message} Run Keyword And Expect Error this is a my expect error example Run Keyword And Continue On Failure Fail this is a my expect error example
log ${log_message}
comment 44.run keyword and ignore error 根据参数执行关键字,忽略错误,返回两个值(pass/fail,收到的错误消息)
@{returnvalue} Run Keyword And Ignore Error Run Keyword And Continue On Failure Fail this is a error example
log many @{returnvalue}
comment 45.run keyword and return 在关键字中使用,执行关键字,然后从关键字中返回
log_message hello
comment 46.run keyword and return status 执行关键字,然后从关键字中以布尔值的形式返回
${result} Run Keyword And Return Status Should Be Equal a a
log ${result}
comment 47.run keyword and warn on failure 执行关键字,如果关键字执行失败,会有一个warn
${rk_warn} Run Keyword And Warn On Failure Should Be Equal A B
log ${rk_warn}
comment 48.run keyword if condition keywordname *args 如果条件为真 则执行后面的关键字
@{rkif} Create List 1 2 3 4
FOR ${i} IN @{rkif}
Run Keyword If '${i}'=='${3}' Continue For Loop
... ELSE log ${i}
END
comment 49.run keyword if all critical tests passed | keywordname |*args 如果所有Critical测试用例通过,则根据参数args执行关键字。只能用于suite teardown。管道传递参数。
comment 50.run keyword if all test passed | keywordname | *args 如果所有测试用例通过,则根据参数args执行关键字.只能用于suite teardown。管道传递参数。
comment 51.Run Keyword If Any Critical Tests Failed 如果有Critical测试用例Fail,则根据参数args执行关键字。只能用于suite teardown。
comment 52.Run Keyword If Any Tests Failed 如果有测试用例Fail,则根据参数args执行关键字。只能用于suite teardown。
comment 53.Run Keyword If Test Failed 如果测试用例Fail,则根据参数args执行关键字。只能用于test teardown。
Comment ${1} Set Variable 1
Comment ${2} Set Variable 2
Comment ${3} Should Be Equal ${1} ${2}
comment 54.run keyword if test passed 如果测试用例Pass,则根据参数args执行关键字。只能用于test teardown。
comment 55.run keyword if timeout occurred 如果测试用例超时,则根据参数args执行关键字。只能用于test teardown。
comment 56.run keyword unless condifion keywordname *args 如果condition为假,根据参数args执行关键字。
@{rku} Create List 1 2 3 4
FOR ${i} IN @{rku}
Run Keyword Unless '${i}'=='${3}' log ${i}
END
comment 57.run keywords 运行多个关键字,用AND连接
Run Keywords log 1
... AND log 2
comment 58.set global variable 设置一个全局变量,后面所有的测试用例和测试套都可以使用
Set Global Variable ${my_global_var} this is a global var
comment 59.set library search order 测试数据中的关键字名称与多个关键字匹配时,库搜索顺序用于解决冲突。
Set Library Search Order Selenium2Library.Open Broswer SeleniumLibrary.Open Broswer
comment 60.set local variable 设置局部变量,只在当前case中使用
Set Local Variable @{slva} 1 2 3
log many @{slva}
comment 61.set log level 将日志设置为指定级别并返回旧级别。感觉没啥用
Comment Set Log Level TRACE
comment 62.set suite documentation 设置当前测试套件的文档。现有的文档可能被覆盖。在log和report中可查看。
Set Suite Documentation 增加测试套文档描述 append=True
comment 63.set suite metadata 为测试套件设置元数据 ?????????????
comment 64.set suite variable 设置一个在当前测试套内都可获取的变量,在设置之后,后面的case可用
Set Suite Variable ${SCALAR} hello,world! children=true
comment 65.set tags 在测试用例或者suite setup中设置标签。
Set Tags YU
comment 66.set task variable 创建测试用例变量,只在当前case中可用
Set Task Variable ${task_var} this is a task var
log ${task_var}
comment 67.set test documentation 设置当前测试用例的文档。在report和log中可见
Set Test Documentation 增加测试用例文档
comment 68.set test message 可在test teardown中使用,设置测试用例的message,可在log、report中看到,若在teardown中使用可能覆盖错误内容
Set Test Message this is test message # 被teardown重写
comment 69.set test variable 设置当前测试用例变量
comment 70.set variable 设置变量,将值返回给变量,主要设置标量变量。也可将标量变量转换为列表变量,或创建多标量变量。
${sv1} Set Variable hello,world!
log ${sv1} # hello,world!
${sv2} set variable i said:${sv1}
log ${sv2} # i said:hello,world!
${sv3} Create List q w e r
log ${sv3} # ['q', 'w', 'e', 'r']
@{list_sv4} Set Variable ${sv3}
log many @{list_sv4} # @{list_sv4} = [ q w e r ] 每列打印1个元素
${two_items} Create List n m
log ${two_items} # ['n', 'm']
${sv5} ${sv6} Set Variable ${two_items}
log many ${sv5} ${sv6} # ${sv5} = n ${sv6} = m 每列打印一个元素
comment 71.set variable if condition value1 value2 根据条件,设置变量值,如果条件为true,则返回第一个值,若为false则返回第二个值
${my_svi} Set Variable 0
${my_svi1} Set Variable If ${my_svi}==0 zero nonzero # ${my_svi1} = zero 条件为真 返回第一个值
${my_svi2} Set Variable If ${my_svi}>0 bigger than zero smaller or equle zero # ${my_svi2} = smaller or equle zero 条件为假,返回第二个值
${my_svi3} Set Variable If ${my_svi}<0 smaller than zero bigger or equle zero # ${my_svi3} = bigger or equle zero 条件为假,返回第二个值
${my_svi4}= Set Variable If ${my_svi}==0 zero ${my_svi}==1 one ${my_svi}==2 two ${my_svi}>2 greater than two ${my_svi}>0 less than zero # ${my_svi4} = zero
comment 72.should be empty item msg 验证给定的item是否为空,无返回值,msg是可被重写,显示错误内容
${sbe} Create List # 1 # ${sbe} = []
Should Be Empty ${sbe} msg=not empty # Length is 0
comment 73.should be equal first second msg values=True 判断给定值是否相等,不相等则fail,msg错误提示消息.无返回值
${a} Set Variable 1
${b} Set Variable 1
Should Be Equal ${a} ${b} msg=not equal
comment 74.should be equal as integers first second msg \ 将对象转换为整数,判断转换后的值是否相等,不相等则fail,msg错误提示消息.无返回值
Should Be Equal As Integers 42 ${42} msg=not equal
Should Be Equal As Integers abcd ABCD msg=abcd not equal with ABCD base=16
Should Be Equal As Integers 0b1011 11
comment 75.should be equal as numbers fisrt second msg precision 判断对象转换为实数后是否相等,不相等则fail,可指定精确度precision,无返回值 ??????精确度怎么算
${x} Set Variable 1.1
Should Be Equal As Numbers ${x} 1.1 msg=they not equal
Should Be Equal As Numbers 1.123 1.1 precision=1
Should Be Equal As Numbers 1.123 1.4 precision=0
Should Be Equal As Numbers 112.3 75 precision=-2
comment 76.should be equal as string first second msg 将对象转换为字符串后,若不相等,则fail,无返回值
${first_sbqas} Set Variable 123
${second_abqas} Set Variable ${123}
Should Be Equal As Strings ${first_sbqas} ${second_abqas}
comment 77.should be true condition msg 如果条件为true,则pass,否则fail,自动导入python os模块和sys模块
${value} Set Variable 1
Should Be True ${value}>0
Should Be True ${value} # value不为0,pass
${list} Create List 1 2 3
Should Be True ${list} # 列表不为空,pass
comment 78.should contain container item msg container中包含item,则pass,否则fail,无返回值,相当于python中的in
Should Contain ${first_sbqas} ${value}
comment 79.should contain any container *items **configuration 如果container中没有包含items中的任意一个,则fail,无返回值
Should Contain Any ${list} 1 8 9
@{list1} Create List 1 8 9
Should Contain Any ${list} @{list1}
comment 80.should contain X times container item count msg container 包含item指定的count次,pass。否则fail
Should Contain X Times ${list} 1 1
comment 81.should end with str1 str2 msg 判断str1是否是以str2结尾,是的话pass。否则fail,无返回值
${string1} Set Variable abcd
${string2} Set Variable cd
Should End With ${string1} ${string2}
comment 82.should match string pattern msg string匹配模式pattern,模式匹配是和shell中的文件匹配类似,它区分大小写,'*'匹配所有字符,“?”单个字符。匹配成功,无返回值
should match ${string1} abc?
comment 83.should match regexp string能匹配正则表达式pattern ???????????
comment 84.should not be empty item msg 变量非空,pass。否则fail,无返回值。
Should Not Be Empty ${string1}
comment 85.should not be equal first second msg 判断两个对象是否相等,相等则fail。
Should Not Be Equal ${string1} ${string2} # 字符串${string1}和${string2}不相等则pass
comment 86.should not be equal as integers first second msg base 将对象转换为整数后不相等,则pass。无返回值。
Should Not Be Equal As Integers ${string1} ${string2} base=16
comment 87.should not be equal as numbers first second msg precision=6 将对象转换为实数后不相等则pass。
Should Not Be Equal As Numbers 123 124
comment 88.should not be equal as strings first second msg 两个对象转换为字符串后比较结果不相等,则pass
Should Not Be Equal As Strings ${string1} ${string2}
comment 89.should not be true conditon msg 给定条件为true则fail
Should Not Be True 5 < 2 # pass
comment 90.should not contain container item msg container中不包含item,则pass
Should Not Contain ${string1} e
comment 91.should not contain any container *item **configuration container不包含任何item中的任何一个,则pass。
Should Not Contain Any ${string1} e f g
comment 92.should not end with str1 str2 msg str1不以str2结尾。则pass。
Should Not End With abcd ef
comment 93.should not match string pattern msg 如果string与给定pattern规则不匹配,则pass
comment 94.should not start with str1 str2 msg 若str1以str2开头,则fail
Should Not Start With ${string1} bc
Should Start With ${string1} ab
Comment 95.skip msg 跳过当前测试的剩余部分
Comment skip
Comment log !!!!!!!!!!!!!
Comment 96.skip if condition msg 若条件为True,则跳过当前测试的剩余部分
Comment 97.Sleep time reason
Sleep 3
Comment 98.variable should exist name msg 变量在当前范围内存在,则pass
Variable Should Exist ${string1}
Variable Should Not Exist ${non_string1}
Comment 99.wait until keyword succeeds retry retry_interval name *args 运行指定的关键字,如果失败则重试。
[Teardown] Set Test Message teardown test message
lesson one
log many @{LIST1infirstSuit}
log many ${var1}
Comment log many @{slva}
log ${SCALAR}
log ${my_global_var}
log ${task_var}