robot framework .robot脚本介绍

1.*** Settings ***介绍

settings设置一些测试用例依赖的库和资源等。

*** Settings ***
Library           Selenium2Library                    /库文件 库名
Documentation     接口:/duty/sendToInnerDuty.action
Suite Setup       Run Keywords    获取cookie    chunyu    AND    DutyLib.Set Cookie    ${cookie}
Test Template     duty_sendToInnerDuty
Library               ../../../Libs/DutyLib.py
Resource           准备数据.txt

字段说明:
Library :robot framework自带的系统库
Documentation: 描述测试接口的路径
Suite Setup : 配置用例运行的环境,比如说提前获取到cookie Run Keywords字段就是,通过调用<获取cookie>这个keyword来设置DutyLib这个python文件中需要的cookie值
Test Template : 设置运行该测试用例需要调用的python函数:duty_sendToInnerDuty
Library : 设置运行该测试用例需要调用的外部库文件,也就是自己写的python文件,该文件中包含了函数duty_sendToInnerDuty,只有设置了这个库文件,上述函数才可以被调用
Resource: 设置运行该测试用例需要的数据资源,其中<准备数据.txt>文件和该用例处于同一个文件夹下。

2.*** Variables ***介绍

Robotframework主要的变量有两类,每个变量都可以用变量标识符{变量名}来进行命名,Robotframework在日常使用过程中主要分为“Scalar”和“List”两种变量。其中“Scalar”的变量标识符为“$”,而“List”的标识符为“@”。

${boyname}  Set variable  TOM  //Scalar变量赋值
log    ${boyname}

3.*** Test Cases ***介绍

*** Test Cases ***
vaild user      //用例名称或说明
	[Setup]    duty_setup    startTimeStr=${next_Mon} 09:00:00    type=normal_duty    customerId=177016
    id=${duty_id}
    [Template]    Login with valid user should succeed    //数据驱动
    johns    long  //值

说明:
关键字:[Setup] 用于设置该用例的前置步骤
前置步骤名称: duty_setup //该前置步骤是一个keyword,后边跟的是这个keyword的入参
入参: startTimeStr=${next_Mon} 09:00:00 type=normal_duty customerId=177016

4.*** Keywords ***介绍

“关键字”:即提供特定功能的函数,包含 “返回值” “函数体” “参数”
关键字的作用:
1、Robot Framework主要就是关键字驱动的自动化测试,关键字是它的核心。从关键字的类型来说,可以分为系统关键字和用户关键字两种
2、系统关键字通常都是来源于测试库,导入库后可直接在testcase里使用
3、用户关键字更多的是来源于资源文件
4、系统关键字底层就是Python函数,而用户关键字其实和函数也没什么两样,完全可以像设计函数一样设计自己的用户关键字
5、可以在资源文件里添加用户关键字,也可以在测试套件中添加关键字,后者不推荐

用户关键字使用步骤:
1.编写PY脚本,即导入系统所需的资源文件
2.关联PY脚本,即把py脚本导入到系统,放在***settings***的library
3.设置关键字,即在***keywords***填写相关关键字的定义,关键字的格式都是:返回值 PY函数 函数参数
4.导入资源,创建的关键字所在资源文件属于外部资源文件,因此在使用时需要导入对应的资源文件,才能使用该资源文件下的关键字,settings"Resource"导入的。当然若测试用例直接用的是PY函数(没有封装成关键字的PY函数,就需要通过Library导入)
5.调用关键字,***test cases***里调用,1.返回值在左边、函数(关键字)在中间、参数在右边),2.关键字"log"打印上一步关键字的返回值"${返回值}"

注:
PY脚本、关键字、资源文件、测试用例之间的关系为:
PY脚本组成关键字,关键字组成资源文件,资源文件中的关键字组成测试用例
(1)关键字通过Library关联PY脚本,测试用例(所在Suite)通过Resource关联资源文件
资源文件(关键字)包括***settings*** Library .py和**keywords***
测试用例文件包括***settings*** Resource .resource和**testcases***
python文件:

			```
			def Add(num1,num2):
			    sum = 0
			    sum = int(num1) + int(num2) 
			    return sum
			```

资源文件(用户关键字)

			```
			// keyword.resource
			*** Settings ***
			Library           ../keyword.py
			
			*** Keywords ***
			key1
			    [Arguments]    ${arg1}    ${arg2}
			    [Documentation]    name:Add
			    ${return}    Add    ${arg1}    ${arg2}
			    [Return]    ${return}
			```

用例文件

			```
			*** Settings ***
			Resource          keyword.resource
			
			*** Test Cases ***
			testcase1
			    ${ret}    Add    12    12
			    Log    ${ret}
			```

(2)测试用例也可直接通过Library关联PY脚本
测试用例文件包括***settings*** Library .py和**testcases***

python文件:

			```
			def Add(num1,num2):
			    sum = 0
			    sum = int(num1) + int(num2) 
			    return sum
			```	

用例文件:

			```
			*** Settings ***
			Library           keyword.py
			
			*** Test Cases ***
			testcase1
			    ${ret}    Add    12    12
			    Log    ${ret}    
			```

用户关键字:

*** Keywords ***
Login with valid user should succeed //keyword的名称
[Arguments] ${username} ${password} //[Arguments]设置入参
Set Global Variable u s e r n a m e / / 设 置 {username} //设置 username//{name}为全局变量,使得所有用例和关键字中可以使用
[Return] ${cookie} //设置出参

*** Keywords ***
duty_setup     //keyword名称
    [Arguments]    ${startTimeStr}=2018-12-31 00:00:00    ${type}=normal_duty    ${customerId}=177016
    ${duty_id}    duty_save    id=${EMPTY}    dutySn=${EMPTY}    problemId=${EMPTY}    type=${type}    customerId=${customerId}
    ...    startTimeStr=${startTimeStr}    endTimeStr=2018-12-31 23:59:59    customerContact=客户联系人_test    contactInfo=联系方式_test    infoSource=internal    sourceDetail=来源详情_test
    ...    associationEntityListJson=[]    attention=注意事项_test    priority=HIGH    title=任务主题_test    description=<p>${time}_任务描述_test</p>
    Set Suite Variable    ${duty_id}

说明:
该keyword设置入参: [Arguments] ${startTimeStr}=2018-12-31 00:00:00 ${type}=normal_duty ${customerId}=177016
调用python函数: duty_save 该函数是在库文件DutyLib.py中定义的
该python函数的出参: ${duty_id}
该python函数的入参:跟在函数名后边
设置出参变量为全局变量: Set Suite Variable ${duty_id} ,方便其他用例在以该keyword为前置步骤时,可以使用这个全局变量

keyword使用:

谷歌浏览器的兼容性检测

open browser    http://www.baidu.com    gc

火狐浏览器的兼容性检测

open browser    http://www.baidu.com    ff

ie浏览器的兼容性检测

open browser    http://www.baidu.com    ie

print

log    hello world

1_定义变量

${a}    Set variable    hello world
log    ${a}

2_连接对象

${hi}    Catenate    hello    world
log    ${hi}

3_连接对象

${hi}    Catenate    SEPARATOR=­­--    hello    world
log    ${hi}

4_定义列表

${abc}    Create List    a    b    c
log    ${abc}

4.1_定义列表

@{abc}    Create List    a    b    c
log many    @{abc}

5.时间的操作

${t}    get time
log    ${t}

5.1时间的操作_sleep

${t}    get time
sleep    5
${t}    get time

6.if语句

${a}    Set variable    59
run keyword if    ${a}>=90    log    优秀
...    ELSE IF    ${a}<=70    log    良好
...    ELSE IF    ${a}<=60    log    及格
...    ELSE    log    不及格

7.for循环

FOR    ${var}    IN    a    b    c
log    ${var}
log    结束

7.1inrangefor循环

FOR    ${var}    IN RANGE    10
log    ${var}
log    结束

8.createlist:for循环

@{abc}    create list    a    b    c
FOR    ${i}    IN    @{abc}
log    ${i}

9.exit for Loop if for循环

FOR    ${var}    IN    a    b    c
Runkeyword if    '${var}'=='b'    Exit for loop
log    ${var}
log    循环结束

10.Evaluate随机函数

${d}    Evaluate    random.randint(1000,9999)    random
log    ${d}

11.导入库

Import Library    unittest

12.导入testpy文件

Import Library    test.py
${add}    add    4
log    ${add}

界面最大化的验证

open browser    https://www.baidu.com/index.php?tn=monline_3_dg    ie
maximize browser window

停留时间

open browser    https://www.baidu.com/
sleep    2
close browser

点击元素-click element

open browser    https://www.baidu.com
click element    xpath=/html/body/div[1]/div[1]/div[3]/a[2]
log    运行结束

点击按钮button

点击link

open browser    https://www.baidu.com
click link    xpath=/html/body/div[1]/div[1]/div[3]/a[2]

文本框架的验证

open browser    http://10.1.2.229/zentao/user-login-L3plbnRhby8=.html
sleep    3
input text    xpath=//*[@id="account"]    admin
input text    xpath=/html/body/div/div[1]/div[2]/form/table/tbody/tr[2]/td/input    123456
click button    xpath=//*[@id="submit"]

多页面跳转验证-select window new

open browser    https://www.baidu.com    gc
click link    xpath=/html/body/div[1]/div[1]/div[3]/a[1]
select window    new    #跳转别的网站使用的关键字
click link    xpath=//*[@id="pane-news"]/div/ul/li[1]/strong/a

变量-set varriavle

${a}    set variable    10
log    ${a}

条件判断-run keyworld if

${a}    set variable    10
${b}    set variable    20
log    ${a}
log    ${b}
run keyword if    ${a}>${b}    log    上自习
...    ELSE    log    今晚上自习

自动获取页面数据-get window size

open browser    https://www.baidu.com    gc
sleep    2
${width}    ${height}    get window size
log    ${width}
log    ${height}

自动化获取web页面分辨率数据-百度-gc-ff-ie

open browser    https://www.baidu.com    gc
sleep    2
${width}    ${height}    get window size
maximize browser window
${width}    ${height}    get window size
open browser    https://www.baidu.com    ff
sleep    2
${width}    ${height}    get window size
maximize browser window
${width}    ${height}    get window size
open browser    https://www.baidu.com    ie
sleep    2
${width}    ${height}    get window size
maximize browser window
${width}    ${height}    get window size

自动化获取web页面分辨率数据-平安银行-gc-ff-ie

open browser    http://bank.pingan.com    gc
sleep    2
${width}    ${height}    get window size
maximize browser window
${width}    ${height}    get window size
open browser    http://bank.pingan.com    ff
sleep    2
${width}    ${height}    get window size
maximize browser window
${width}    ${height}    get window size
open browser    http://bank.pingan.com    ie
sleep    2
${width}    ${height}    get window size
maximize browser window
${width}    ${height}    get window size
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值