Robot framework使用文档.docx
《Robot framework使用文档.docx》由会员分享,可在线阅读,更多相关《Robot framework使用文档.docx(17页珍藏版)》请在冰点文库上搜索。
Robotframework使用文档
目录
1.编写目的2
2.执行Testcases2
2.1.运行testcase2
2.1.1.用python运行testcase2
2.1.2.用jython运行testcase2
2.1.3.写自定义脚本运行testcase2
2.1.4.用RIDE使用和运行testcase3
2.1.5.用Eclipse运行testcase3
2.2.参数使用4
2.2.1.escape4
2.2.2.argumentfile4
3.创建TestData5
3.1.TestDataFiles5
3.1.1.TestDataSyntax5
3.1.2.SettingTable6
3.1.3.VariablesTable7
3.1.4.TestCaseTable7
3.1.5.KeywordTable7
3.2.使用Library8
3.2.1.Librarysearchpath8
3.2.2.使用物理地址8
3.2.3.自定义库名9
3.3.使用Variables9
3.3.1.Testcase定义变量9
3.3.2.Variablefiles定义变量10
3.4.使用Keyword10
3.4.1.Testcase定义keyword10
3.4.2.PythonorJava定义keyword11
3.5.使用Resourcefiles11
4.扩展Robot12
4.1.创建TestLibrary12
4.1.1.statickeywords12
4.1.2.参数13
1.编写目的
本文档用于描述Robotframework的使用方法。
2.执行Testcases
2.1.运行testcase
2.1.1.用python运行testcase
1)在python环境下执行case可用pybot命令。
例:
pybottest.html
2)Testcase中引入的library只能是.py,不能引入java的文件
2.1.2.用jython运行testcase
1)在jython环境下执行case可用jybot命令。
例:
jybottest.html
2)Testcase中引入的library可以是.py也可以说.class,java的文件要先编译好才能用。
不能引入jar包。
3)可以将所需要的.class文件打成jar包,然后运行时加上--pythonpathxxx.jar,或者放在classpath变量里。
2.1.3.写自定义脚本运行testcase
设置运行所需要的jar包,然后再运行case
@echooff
setCP=.
for%%jarin(lib\*.jar)do(
call:
set_cp%%jar
)
setCLASSPATH=%CP%
jybot--ouputdird:
\mytest\outputtest.html
goto:
eof
:
:
Helperforsettingvariablesinsideaforloop
:
set_cp
setCP=%CP%;%1
goto:
eof
2.1.4.用RIDE使用和运行testcase
1)使用java或python编写keyword
2)用libdoc.py创建关键字文档,以让RIDE可以显示与搜索。
jythonlibdoc.py-fxmlcom/test/wsg/ta/library/FtpLibrary.java
上述是先设置创建文档所需要的jar包,然后生成xml格式的关键字文档。
将生产的xml放到pythonpath目录中,然后打开RIDE,引入com.test.wsg.ta.library.FtpLibrary后在tools->serchkeywords中即可找到关键字及其描述。
3)在RIDE上写好case后,可以使用pybot、jybot或customscript方式运行,同上述三种方式一样。
2.1.5.用Eclipse运行testcase
1)新建一个项目后引入robotframework独立运行的jar包。
2)新建一个class,代码如下:
importorg.robotframework.RobotFramework;
publicclassRobotTest{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
intrc=RobotFramework.run(newString[]{
"--outputdir",
"d:
\\mytest\\output",
"d:
\\mytest\\test.html"
});
if(rc==0)
System.out.println("Alltestspassed");
elseif(rc<=250)
System.out.println(rc+"testsfailed.");
else
System.out.println("Erroroccurred");
}
}
2.2.参数使用
2.2.1.escape
Escape作用是替换符号。
例:
--escapespace:
_--metadataX:
Value_with_spaces
经过替换后metadataX的值为Valuewithspaces
--pythonpathlib/STAR.jar--escapestar:
STAR
经过替换后则会引入lib下所有的jar包
可使用的替换符
Character
Nametouse
Character
Nametouse
&
amp
(
paren1
'
apos
)
paren2
@
at
%
percent
\
blash
|
pipe
:
colon
?
quest
comma
"
quot
{
curly1
;
semic
}
curly2
/
slash
$
dollar
space
!
exclam
[
square1
>
gt
]
square2
#
hash
*
star
<
lt
2.2.2.argumentfile
1)可以将所需的参数全部放在一个文件里,运行时引入该文件即可。
jybot--argumentfileall_arguments.txt
2)一个文件夹下的case是按字母顺序执行的,以下情况可以将其放到文件里,按自己想要的顺序执行。
tests/some_tests.html
tests/second.html
tests/more/tests.html
tests/more/another.html
tests/even_more_tests.html
3.创建TestData
3.1.TestDataFiles
3.1.1.TestDataSyntax
1)支持的文件格式:
HTML(.html、.htm、.xhtml),TSV(.tsv),plaintext(.txt),reStructuredText(.rst、.rest)。
2)一个文件是一个testsuite,包含多个case;一个文件夹包含多个文件,文件夹中还可包含多个文件夹(目录结构)。
3)使用HTML格式写case如下:
Setting
Value
Value
Value
Library
OperatingSystem
Variable
Value
Value
Value
${MESSAGE}
Hello,world!
TestCase
Action
Argument
Argument
MyTest
[Documentation]
Exampletest
Log
${MESSAGE}
MyKeyword
/tmp
AnotherTest
ShouldBeEqual
${MESSAGE}
Hello,world!
Keyword
Action
Argument
Argument
MyKeyword
[Arguments]
${path}
DirectoryShouldExist
${path}
4)整个testdata包括settingtable、variablestable、testcasetable、keywordtable。
用RIDE可以很方便的编辑testdata。
3.1.2.SettingTable
Setting
Value
Value
Value
Value
Library
Resource
Variables
Documentation
Metadata
SuiteSetup
SuiteTeardown
ForceTags
DefaultTags
TestSetup
TestTeardown
TestTemplate
TestTimeout
Library:
有standard(builtIn、operatingSystem、Telnet、collections、String、Dialogs、screenshot、Remote)、External(selenium、swing…)以及自定义的库。
Resource:
可在文件中定义keywords、variables,然后引入
Variables:
这里的变量是在python中写的,故该变量可动态地设置(通过函数返回,或调取数据库…)
Documentation:
对testsuite的描述
Metadata:
自定义一些信息,如版本信息
Suitesetup:
suite执行之前做的事
Suiteteardown:
suite结束之前做的事
ForceTag:
强制有的tag
DefaultTag:
默认的tag
Testsetup:
testcase执行前做的事(每个testcase,要是该testcase有自己的setup,会覆盖掉本setup)
Testteardown:
testcase结束前做的事
Testtemplate:
将keyword-driven转换成data-driven的格式输出(case引用模板后要按模板的格式来,使用其他keyword没用)
Testtimeout:
testsuite执行的时间,超过这个时间会认为是failure
3.1.3.VariablesTable
Variable
Value
Value
Value
Value
这里可设置变量
3.1.4.TestCaseTable
TestCase
Action
Argument
Argument
Argument
[Documentation]
[Tags]
[Setup]
[Teardown]
[Template]
[Timeout]
Documentation:
testcase描述
Tags:
testcase标签
Setup:
testcase执行前做的事
Teardown:
testcase结束前做的事
Template:
testcase模板
Timeout:
testcase执行时间
3.1.5.KeywordTable
Keyword
Action
Argument
Argument
Argument
[Documentation]
[Arguments]
[Return]
[Timeout]
[Teardown]
Documentation:
keyword描述
Arguments:
参数
Return:
返回值
Timeout:
超时时间
Teardown:
keyword结束前做的事
3.2.使用Library
3.2.1.Librarysearchpath
pythonpath、classpath、Using--pythonpathoption
1)将自定义的library(.py或.class)放入pythonpath或classpath
2)将jar包的位置添到classpath环境变量中,也可在运行的时候先setCLASSPATH=xxx.jar
3)运行的时候加入--pythonpath参数,例:
Jybot--pythonpathlib/(.pyor.class文件)
Jybot--pythonpathlib/STAR.jar--escapestar:
STARtest.html(这里表示引入lib下所有的jar包)
4)Test.html中library设置
Setting
Value
Library
com.test.wsg.ta.library.FtpLibrary
(FtpLibrary在package包里)
Library
FtpLibrary.class
(FtpLibrary不在任何包里)
Library
FtpLibrary.py
Library
OperatingSystem
内置库
3.2.2.使用物理地址
.py或.class文件所在的绝对地址或相对地址
Test.html中library设置
Setting
Value
Library
Lib/FtpLibrary.class
Library
D:
/mylib/FtpLibrary.class
3.2.3.自定义库名
以不同名字引入同一个库
Setting
Value
Value
Value
Value
Value
Library
SomeLibrary
localhost
1234
WITHNAME
LocalLib
Library
SomeLibrary
server.domain
8080
WITHNAME
RemoteLib
TestCase
Action
Argument
Argument
MyTest
LocalLib.SomeKeyword
somearg
secondarg
RemoteLib.SomeKeyword
anotherarg
whatever
LocalLib.AnotherKeyword
3.3.使用Variables
3.3.1.Testcase定义变量
1)变量分为scalar和list。
2)Scalar为单值变量,例:
${GREET}
3)List里可包含多个值,list不能嵌套list,例@{USER}
4)变量使用优先级,依次降低:
commandline->casefile定义的->从resource或变量文件引入的->执行测试时设置的->内置变量
5)数字${3.14}、${3}
Variable
Value
${GREET}
helloworld!
@{USER}
robot
secret
TestCase
Action
Argument
Argument
Strings
Login
robot
secret
TitleShouldBe
Welcomerobot!
ListVariable
Login
@{USER}
TitleShouldBe
Welcome@{USER}[0]!
3.3.2.Variablefiles定义变量
1)Variablefiles使用python编写
2)引入变量文件如前使用library一样
3)简单只用如下:
VARIABLE="Anexamplestring"
ANOTHER_VARIABLE="Thisisprettyeasy!
"
INTEGER=42
STRINGS=["one","two","kolme","four"]
NUMBERS=[1,INTEGER,3.14]
4)动态变量:
importmath
defget_area(diameter):
radius=diameter/2
area=math.pi*radius*radius
returnarea
AREA1=get_area
(1)
AREA2=get_area
(2)
3.4.使用Keyword
3.4.1.Testcase定义keyword
[Documentation]设置keyword描述
[Arguments]设置keyword调用时需输入的参数
[ReturnValue]设置keyword的返回值
[Teardown]设置keyword结束前做的事
[Timeout]超时时间
Keyword
Action
Argument
Argument
OpenLoginPage
OpenBrowser
http:
//host/login.html
[Timeout]
20seconds
GetTitle
[Arguments]
${expected}
[Return]
${expected}title
3.4.2.PythonorJava定义keyword
使用pythonorjava写keyword,然后通过library引入。
见创建TestLibrary
3.5.使用Resourcefiles
1)可在该文件中引入library、resource、variablefiles(python文件);定义keywords、variables;不能定义testcase
2)引入该文件如前使用library一样
4.扩展Robot
4.1.创建TestLibrary
4.1.1.statickeywords
1)Testlibraries可以用python或java实现
2)Java文件需编译好后才能引入,引入方法见使用library
3)简单使用如下:
(通过构造函数传参数)
Python:
fromexampleimportConnection
classMyLibrary:
def__init__(self,host,port=80):
self._conn=Connection(host,int(port))
defsend_message(self,message):
self._conn.send(message)
Java:
publicclassAnotherLib{
privateStringsetting=null;
publicAnotherLib(Stringsetting){
setting=setting;
}
publicvoiddoSomething(){
ifsetting.equals("42"){
//dosomething...
}
}
}
Setting
Value
Value
Value
Library
MyLibrary
10.0.0.1
8080
Library
AnotherLib
${VAR}
4)Testlibrary范围
TESTCASE:
每个case可以共享该实例
TESTSUITE:
每个suite可以共享该实例
GLOBAL:
整个执行期间就创建一个实例
Python:
ROBOT_LIBRARY_SCOPE='TESTSUITE'
Java:
publicstaticfinalStringROBOT_LIBRARY_SCOPE="GLOBAL";
5)Testversion
Python:
__version__='0.1'
Java:
publicstaticfinalStringROBOT_LIBRARY_VERSION="1.0.2";
4.1.2.参数
1)使用不同参数
defno_arguments():
print"Keywordgotnoarguments"
defone_argument(arg):
print"Keywordgotoneargument'%s'"%arg
defmultiple_arguments(a1,a2,a3):
print"Keywordgotthreearguments'%s','%s'and'%s'"%(a1,a2,a3)
2)使用默认参数
Python:
defone_default(arg='default'):
print"Argumenthasvalue'%s'"%arg
defmultiple_defaults(arg1,arg2='default1',arg3='default2'):
print"Gotarguments%s,%sand%s"%(arg1,arg2,arg3)
Java:
(通过重载实现)
publicvoidoneDefault(Stringarg){
System.out.println("Argumenthasvalue'"+arg"'");
}
publicvoidoneDefault(){
oneDefault("default");
}