用户关键字
通常的从库(也就是python文件,安装好robot framework就有的)里面提供的关键字,我们叫库关键字或系统关键字;
用户关键字更多的是来源于资源文件,定义好用户关键字后,该测试套件里面就可以使用该用户关键字了,就像使用库关键字一样
定义关键字
可支持参数
*** Keywords ***
LoginWeb #LoginWeb为用户关键字
[Arguments] ${username} ${password} #自定义参数
open browser http://... chrome
set browser implicit wait 5
input text id=username ${username}
input password id=password ${password}
click element css:button[class='…']
set selenium implicit wait 5
可支持返回值
*** Keywords
GetCourses
Click Element css=...
Sleep
${lists}= get webelements css=...
${courses} create list
:FOR ${ele} IN @{lists}
\ log to console \n${ele.text}
\ append to list ${courses} ${ele.text}
[Return] ${courses}
使用关键字
使用关键字,需要在Settings里引入关键字表
*** Settings ***
Library SeleniumLibrary
Resource config/CaseKeys.robot #导入资源文件,里面包含用户关键字
*** Test Cases ***
AddFirstCourse
[Setup] removetheAllCourse #初始化用户关键字
AddCourses 高数 高等数学 1 # 实例化用户关键字
${lessons}= GetCourses #使用关键字返回值
should be true $lessons==['高数'] #判断关键字返回值是否包含值
[Teardown] removetheAllCourse #清除用户关键字
资源文件
资源文件里可以包含用来共享的变量和关键字
只能在本测试套件中有效,无法共享给其他测试套件使用
资源文件的引用使用Resource
Resource 后可以跟绝对路径也可以用相对路径(相对于当前文件所在的目录),但文件名必须要带后缀。
如果同级目录下没有,就会去path路径下寻找
*** Settings ***
Library SeleniumLibrary
Resource config/CaseKeys.robot #导入资源文件,里面包含用户关键字