0%

Robot-Framework的几点总结

命令行执行RF用例

1
2
3
4
5
6
7
8
9
# 执行整个项目下的所有用例
pybot /opt/robotframework/
# 执行某个suite中的所有用例
pybot /opt/robotframework/rf.robot
# 执行具体的某个用例
pybot --test case_1 /opt/robotframework/rf.robot
# 执行项目中指定标签的用例,最后的参数用于指定包含tagName的用例所在的目录或者指定到suite
pybot --include tagName /opt/robotframework/
pybot --include=tagName /opt/robotframework/

设置环境变量,根据环境执行测试

变量脚本envVars.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def get_variables(env = 'test'):
if env=='preview':
#preview environment
variables = {
# 登录服务:大风车|广汇
"sso" : "http://sso.prepub.shadow.com",
"ghsso" : "http://hmc-sso.prepub.cgacar.com",
}
elif env=='online':
#online environment
variables = {
# 登录服务:大风车|广汇
"sso" : "http://sso.shadow.com",
"ghsso" : "http://hmc-sso.cgacar.com",
}
else:
#text environment
variables = {
# 登录服务:大风车|广汇
"sso" : "http://dfc.shadow.net",
"ghsso" : "http://sso.gh.shadow.net",
}

globalvars = {
'productid':'3456',
'userid':'test12'
} #共用变量
# variables['globalvars'] = globalvars

return variables

执行脚本的命令

1
2
3
4
5
6
7
# -d results                指定日志文件目录
# -V ./envVars.py:online V 要大写,变量文件路径:环境参数,可以不填参数,则是默认环境

# 如果有多个变量文件,可以使用多个“-V 文件路径:环境参数”

# --include=test_ty . 在当前目录下及子目录下,筛选[tag]为 test_ty 的所有 TC 并执行
pybot -d results -V ./envVars.py:online -V ./orderVars.py:preview --include=test_ty .

IDE设置命令行执行RF用例

1
2
# 执行单条用例
/usr/local/bin/pybot -d results -t testcase001 ./
1
2
# 执行单个suite
/usr/local/bin/pybot -d results testsuite001.robot

指定RF用例执行后日志的保存位置

其实上面的-d参数就是用来指定Log的保存位置的,默认-d results指定日志保存在运行命令的目录的results文件夹下。
ride中的run标签下,Arguments中填入-d results也能达到同样的效果。

重新运行上一轮FailCase

使用-R参数,同--rerunfailed output,后面跟前次执行生成的results/output.xml,这样就只会运行上次失败了的Case。

List中的字典循环

1
2
3
4
5
6
7
8
9
10
11
12
13
*** Test Cases ***
takeValueFromCircle
# 从返回结果中提取出List
@{items}= set variable ${json["data"]["items"]}
# 循环List中的item
: FOR ${params} IN @{items}
# 把item中的一个参数(每个参数都是一个字典)转化为Str格式,顺便去除 "u" 标识
\ ${params} Dumps ${params}
# 把字典转化为json
\ ${params} to json ${params}
#\ Log ${params["carId"]}
# 对每个item取出来的字典中的某个字段进行判断,如果是需要的值,就把另一个需要的值取出来,并打印出来
\ RUN KEYWORD IF "${params["carInfo"]["status"]}"=="评估中" Log ${params["carId"]}

wait until keyword succeeds关键字使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
*** Test Cases ***
"Wait until ..." with normal error
# Keyword is run multiple times, until timeout. Each run gives an exception
# traceback.
Wait Until Keyword Succeeds 1 sec 0.5 Keyword With Normal Error

"Wait until ..." with AttributeError
# Keyword is run only once, even if there is time left until the timeout.
# There is no exception traceback like above.
Wait Until Keyword Succeeds 1 sec 0.5 Keyword With AttributeError

*** Keywords ***
Keyword With Normal Error
${obj} = Evaluate "foo"
Should Be Equal As Strings ${obj} "bar"

Keyword With AttributeError
# In real life, this would get an object and use some of its (valid) attributes.
# In case of an error, and in Teardown context (continue-on-failure), a None object
# is returned instead causing the next keyword to create an AttributeError.
${obj} = Evaluate "foo"
Should Be Equal As Strings ${obj.bad_attr} "foo"
1
2
3
4
5
6
7
8
9
10
11
*** Test Cases ***
003.导出进度-/pc/export/taizhangaction/progress.json
wait until keyword succeeds 3 min 5 sec 导出进度-/pc/export/taizhangaction/progress.json

*** Keywords ***
导出进度-/pc/export/taizhangaction/progress.json
${params}= Create Dictionary jobId=${jobId}
&{json}= Rest.Post /pc/export/taizhangaction/progress.json ${params} form ${hosts["erp-online"]}
Should Be True ${json["success"]}
should be equal as strings ${json["data"]["progress"]} 100
${URL}= set variable ${json["data"]["url"]}

5秒执行一次关键字,如果${json["data"]["progress"]}!=100,执行一次关键字,直到相等时,执行一次关键字中的最后一行代码。

一个完整的独立case

1
2
3
4
5
6
7
8
9
10
*** Test Cases ***
登录
${dict}= Create Dictionary Content-Type=application/x-www-form-urlencoded
${host}= Create Session _session http://dfc.shadow.com ${dict}
${params}= Create Dictionary loginName=15558135526 password=shadow2015 jPushId=jpushid001
${response}= Post Request _session /rest/account/login params=${params} headers=${dict}
Should Be Equal As Strings ${response.status_code} 200
&{json}= Set Variable ${response.json()}
Should Be True &{json}[success]
Log &{json}[success]

对请求proxy、tag、headers、session、response的整体封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
*** Keywords ***
Rest.Post
[Arguments] ${uri} ${params} ${type}=form ${cur_host}=${EMPTY}
#设置代理服务器,这样方便调试代码
&{proxy}= Create Dictionary http=http://127.0.0.1:8888
#根据tag来区分请求应使用哪个host
${host}= Set Variable \ \ ${EMPTY}
: FOR ${tag} IN @{TEST TAGS}
\ ${host}= Evaluate $hosts.get($tag,"")
\ Run Keyword If "${host}"!="" Exit For Loop
#创建session,跨域模式,不需要维护Session
Run Keyword If "${cur_host}"!="" Create Session _session ${cur_host} proxies=${proxy}
... ELSE Create Session _session ${host} proxies=${proxy}
#已登录的用户在请求中带上token
Run Keyword If "${token}"!="" Set To Dictionary ${params} token=${token}
Log ${token}
#根据请求数据的类型设置header
&{headers}= Run Keyword If "${type}"=="form" Create Dictionary Content-Type=application/x-www-form-urlencoded TT=${token}
... ELSE IF "${type}"=="json" Create Dictionary Content-Type=application/json TT=${token}
${response}= Post Request _session ${uri} ${params} headers=&{headers}
Should Be Equal As Strings ${response.status_code} 200
&{json}= Set Variable ${response.json()}
[Return] &{json}

所有的安装内容

1
2
3
4
5
6
7
8
9
10
11
12
pip install robotframework
pip install robotframework-ride
pip install robotframework-appiumlibrary
pip install robotframework-selenium2library
pip install robotframework-databaselibrary
pip install robotframework-sshlibrary
pip install robotframework-requests
pip install robotframework-HttpLibrary
pip install robotframework-difflibrary
pip install requests
pip install PyMySQL
pip install MySQL-python

RobotFramework自带变量

VariableExplanationAvailable
${TEST NAME}The name of the current test case.Test case
@{TEST TAGS}Contains the tags of the current test case in alphabetical order. Can be modified dynamically using Set Tags and Remove Tags keywords.Test case
${TEST DOCUMENTATION}The documentation of the current test case. Can be set dynamically using using Set Test Documentation keyword. New in Robot Framework 2.7.Test case
${TEST STATUS}The status of the current test case, either PASS or FAIL.Test teardown
${TEST MESSAGE}The message of the current test case.Test teardown
${PREV TEST NAME}The name of the previous test case, or an empty string if no tests have been executed yet.Everywhere
${PREV TEST STATUS}The status of the previous test case: either PASS, FAIL, or an empty string when no tests have been executed.Everywhere
${PREV TEST MESSAGE}The possible error message of the previous test case.Everywhere
${SUITE NAME}The full name of the current test suite.Everywhere
${SUITE SOURCE}An absolute path to the suite file or directory.Everywhere
${SUITE DOCUMENTATION}The documentation of the current test suite. Can be set dynamically using using Set Suite Documentation keyword. New in Robot Framework 2.7.Everywhere
&{SUITE METADATA}The free metadata of the current test suite. Can be set using Set Suite Metadata keyword. New in Robot Framework 2.7.4.Everywhere
${SUITE STATUS}The status of the current test suite, either PASS or FAIL.Suite teardown
${SUITE MESSAGE}The full message of the current test suite, including statistics.Suite teardown
${KEYWORD STATUS}The status of the current keyword, either PASS or FAIL. New in Robot Framework 2.7User keyword teardown
${KEYWORD MESSAGE}The possible error message of the current keyword. New in Robot Framework 2.7.User keyword teardown
${LOG LEVEL}Current log level. New in Robot Framework 2.8.Everywhere
${OUTPUT FILE}An absolute path to the output file.Everywhere
${LOG FILE}An absolute path to the log file or string NONE when no log file is created.Everywhere
${REPORT FILE}An absolute path to the report file or string NONE when no report is created.Everywhere
${DEBUG FILE}An absolute path to the debug file or string NONE when no debug file is created.Everywhere
${OUTPUT DIR}An absolute path to the output directory.Everywhere