【原创】Robotframework 简介
概述Robot Framework是一个通用的关键字驱动自动化测试框架。
测试用例以HTML,纯文本或TSV(制表符分隔的一系列值)文件存储。通过测试库中实现的关键字驱动被测软件。Robot Framework灵活且易于扩展。它非常适合测试有不同接口的复杂软件:用户接口、命令行,Web服务,专有的编程接口等。 Robot Framework是开源软件,安装包和源代码等文档可通过http://robotframework.org获取。
安装
http://www.cnblogs.com/maple42/p/5076328.html
最好通过pip安装,可以自动下载依赖与匹配版本
注意事项
1.32位与64位要分清
2.selenium2library 库的安装 有依赖库 selenium 与 decorator,最好使用pip来安装,因为pip 会自动安装依赖库。
3.如果是公司内网使用代理上网,RF无法识别IE的配置需要使用proxy.pac脚本实现代理上网
配置方法 IE》工具》Internet选项》连接》局域网设置
勾选使用自动配置脚本 file://c:/proxy.pac ------脚本存放路径 注意斜杠
目前robotFramework 支持的测试库
https://github.com/robotframework
https://github.com/bulkan/robotframework‐difflibrary
http://robotframework.org/#test‐libraries https://code.google.com/p/robotframework/wiki/TestLibraries
标准测试库:
· BuiltIn
· OperatingSystem
· Screenshot
· Telnet
· Collections
· String
· Dialogs
· Remote
· XML (new in RF 2.7.4)
外部测试库:
This is a list of publicly available test libraries that can be used with Robot Framework but need to be installed separately. Please refer to the individual project pages for information about installing and using them.
· SeleniumLibrary - A web testing library that uses popular Selenium tool internally.
· Selenium2Library - A drop-in-replacement for SeleniumLibrary using newer Selenium 2 WebDriver API.
· Selenium2Library Java port - Java implementation of Se2Lib. Compatible with Jython 2.5.
· watir-robot - A web testing library that uses popular Watir tool via the remote library interface.
· WatinLibrary - A web testing library that uses Watin tool (a .NET port of Watir) via the remote library interface.
· SwingLibrary - A Swing GUI testing library.
· EclipseLibrary - A library for testing Eclipse RCP applications using SWT widgets.
· AutoItLibrary - Windows GUI testing library that uses AutoIt freeware tool as a driver.
· DatabaseLibrary (Java) - A test library that provides common functionality for testing database contents. Implemented using Java so works only with Jython.
· DatabaseLibrary (Python) - Another library for database testing. Implemented with Python and works also on Jython.
· SSHLibrary - A test library that enables SSH and SFTP.
· HTTP test library using livetest
· HTTP test library using Requests
· SudsLibrary - library for testing SOAP-based web services
· AndroidLibrary - Library for Android testing that uses Calabash Android internally.
· IOSLibrary - Library for iOS testing that uses Calabash iOS Server internally.
· Rammbock - Generic network protocol test library.
· How-To: Sikuli and Robot Framework Integration - This is not really a library but these instructions explain how to integrate Sikuli tool with Robot Framework Found these also, might be useful to add them as well (to the external libraries):
· PhantomJS and Zombie.js libraries (similar to SeleniumLibrary? but headless)
· Generic Network Protocol test library
· REST specific HTTP library
· SoapUI library (similar approach to Sikuli lib, but with SoapUI)
· IOS (Iphone/Ipad) test automation library (uses Calabash IOS Server to drive IOS device)
· Android test automation library (uses Calabash Android Server to drive android device)
· ADB (android) library
· VMWare and HyperV virtual machine libraries
· Library supporting debugging in robot framework
· Diff style file comparison library
· Closure webdriver library
· Sikuli integration libraries here and here (The how-to is helpful, but not exactly a library. Checkout here for code based on the how-to though)
· email library
· Image Comparison library
· HTML Check library (doesn't look maintained though, for shame Janne )
关键字
测试用例由关键字创建。关键字有三个来源:总是可用的内置关键字,来自导入测试库的库关键字和用户在创建测试用例时使用表格语法创建的用户关键字。都可以在ride下通过F5来查看
内置关键字
一些通用的关键字,如获取时间(Get Time)和应该等于(Should Be Equal)。这些关键字来自内建测试库BuiltIn。通过它的文档,可以看到一个完整可用关键字列表。
库关键字
所有最低级的关键字在测试库中定义,这些测试库使用标准编程语言实现。 Robot Framework带有一些库,包括OperatingSystem:支持常见的操作系统功能,Screenshot截图库。
除了这些标准库,还有其他开源项目,如SeleniumLibrary。如果没有合适的可用的库也很容易实现自己的库。
使用测试库的关键字之前需要导入,它必须考虑到使用。本文件中的测试需要导入标准OperatingSystem的库中的关键字(如Remove File)以及从自定义库LoginLibrary(如Attempt to login with credentials)。
用户关键字
Robot Framework最强大的功能之一是能够方便地从其他关键字创建新的更高级别的关键字。创建用户定义的关键字(简称为用户关键字)的语法类似于创建测试用例。
用户关键字可以包括其他用户定关键字,内置关键字,或库关键字。正如下面例子可以看到,用户关键字可以带参数。他们也可以返回值,甚至包含FOR循环。用户关键字可以生成可重用的通用动作序列。用户关键字提高测试的可读性,并在不同情况下使用适当的抽象层次
找一个项目举例
*** Settings ***
Library Selenium2Library
Library DatabaseLibrary
*** Variables ***
${user} 13917745687 # 默认登陆用户
*** Test Cases ***
登陆
海淘登陆 ${user}
close all browsers
个人中心-消息中心
海淘登陆 ${user}
点击元素 //*[@id="user-center"]
点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li/a
wait until page contains 您暂未收到任何消息~
close all browsers
个人中心-我的收藏
海淘登陆 ${user}
点击元素 //*[@id="user-center"]
点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li/a
wait until page contains 全部
close all browsers
个人中心-我的订单
海淘登陆 ${user}
点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li/a
wait until page contains 所有订单
close all browsers
个人中心-我的优惠券
海淘登陆 ${user}
点击元素 //*[@id="user-center"]
点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li/a
wait until page contains 未使用
close all browsers
个人中心-账户管理
海淘账户管理
close all browsers
个人中心-账户管理-修改昵称性别
海淘账户管理
${rand} 随机数
点击元素 //*[@id="user-info-modify-btn"]
wait until keyword succeeds 2 min 5 sec input text //*[@id="nickname"] ${rand}
Comment wait until keyword succeeds 2 min 5 sec input text //*[@id="user-name"] ${user}
select from list //*[@id="select-sex"] FEMALE
点击元素 //*[@id="user-info-confirm-btn"]
close all browsers
个人中心-收货地址
海淘登陆 ${user}
点击元素 //*[@id="user-center"]
点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li/a
wait until page contains 添加收货地址
close all browsers
个人中心-积分管理
积分管理
close all browsers
个人中心-积分管理-收入
积分管理
点击元素 //*[@class="integral-tab-c1 fl"]/ul/li/a #/html/body/div/div/div/div/div/div/div/div/div/ul/li/a
close all browsers
个人中心-积分管理-支出
积分管理
点击元素 //*[@class="integral-tab-c1 fl"]/ul/li/a #/html/body/div/div/div/div/div/div/div/div/div/ul/li/a
close all browsers
个人中心-邀请有礼
海淘登陆 ${user}
点击元素 //*[@id="user-center"]
点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li/a
wait until page contains 邀请有礼
close all browsers
*** Keywords ***
带状态登录
${url}
open browser ${url} ff ff_profile_dir=${config}
Maximize Browser Window
sleep 1
重试
${key} ${args1}=none
wait until keyword succeeds 2 min 7 s ${key} ${args1}
等待元素
${locator} # 元素定位xpath、CSS、ID等
wait until page contains element ${locator}
点击元素
${locator}
重试 等待元素 ${locator}
重试 click element ${locator}
填写
${locator} ${content}=none
Selenium2Library.input text ${locator} ${content}
无视错误执行
${key} ${args1}=none
Run Keyword And Ignore Error ${key} ${args1}
随机数
${num} evaluate random.randint(10000000,99999999) random
${num}
连接海豚库
Connect To Database Using Custom Params pymysql database='CSCOnline', user='csc_a', password='dp!@DkbH3ABUv', host='10.1.77.69', port=3306
海豚登录
清理海豚登录状态
open browser http://csc-online-kefu-web01.beta/login.jsp
Maximize Browser Window
input text xpath=//*[@id="j-login-staffno"] ${global-staff-no}
input password xpath=//*[@id="j-login-password"] 123456
wait until element is visible xpath=//*[@id="j-login-button"]
点击元素 xpath=//*[@id="j-login-button"]
run keyword and ignore error wait until page contains element //*[@id="j-button-relogin"] 3
run keyword and ignore error click element //*[@id="j-button-relogin"]
wait until page contains 大众点评 30
sleep 1
更改接线人数
log 修改最大接线人数
click element xpath=//*[@id="j-button-reception"]
sleep 2
click element xpath=//*[@id="j-ul-reception"]/li/a
提取数据
${data}
${out} evaluate ${data}
${out} # 提取出的内容
清理海豚登录状态
连接海豚库
execute sql string Delete FROM icc_staff_online;
Comment Delete All Rows From Table icc_staff_online
sleep 2
后台sso
open browser https://sso.51ping.com/login?TARGET=http%3A%2F%2Fbackend.csc.dp%3A8080%2F ff
Maximize Browser Window
run keyword and ignore error input text //*[@id="username"] 0018169
run keyword and ignore error input password //*[@id="password"] Ceshi123
run keyword and ignore error click element //*[@id="fm1"]/div/div/input
sleep 1
run keyword and ignore error reload page
run keyword and ignore error wait until page contains 后台管理 20
展开后台
wait until element is visible xpath=//*[@id="sidebar"]/ul/li/a/span 20
click element xpath=//*[@id="sidebar"]/ul/li/a/span
sleep 1
新增部门
后台sso
展开后台
点击元素 //*[@id="sidebar"]/ul/li/ul/li/a
wait until element is visible xpath=//*[@id="add_grid-table"]/div/span 30
点击元素 xpath=//*[@id="add_grid-table"]/div/span
点击元素 xpath=//*[@id="editmodgrid-table"]
select from list by value //*[@id="deptSelect2"] 10
${dept} evaluate random.randint(1111111,9999999) random
input text xpath=//*[@id="name"] ${dept}
sleep 0.5
log 提交新部门信息
点击元素 xpath=//*[@id="sData"]
sleep 1
连接海豚库
${data-check} query select id from CSC_Department where name='${dept}'
should not be empty ${data-check}
log 数据入库成功
${dept} # 部门名称
新增技能组
后台sso
展开后台
点击元素 //*[@id="sidebar"]/ul/li/ul/li/a
Comment wait until element is visible //*[@id="select-btn"]/button
Comment click button //*[@id="select-btn"]/button
log 新增技能组
wait until element is visible //*[@id="add_grid-table"]/div/span
点击元素 //*[@id="add_grid-table"]/div/span
${skill-code} evaluate random.randint(10000,99999) random
sleep 0.6
log 新技能组名称
wait until element is visible //*[@id="name"]
input text //*[@id="name"] ${skill-code}
wait until element is visible //*[@id="code"] ${skill-code}
log 选择部门为商服
select from list by value //*[@id="deptSelect"] 20 #10user \ \ 20shop \ \ 30sales
sleep 0.6
log 提交
wait until element is visible //*[@id="sData"]/i
点击元素 //*[@id="sData"]/i
log 提交成功
sleep 1
${skill-code} # 技能组
新增客服
后台sso
展开后台
log 打开员工页
点击元素 //*[@id="sidebar"]/ul/li/ul/li/a
点击元素 xpath=//*[@id="add_grid-table"]/div/span
sleep 0.5
${staff-no} evaluate random.randint(1111111,9999999) random
select checkbox //*[@id="staffNOEditable"]
log 输入客服名称
wait until element is visible xpath=//*[@id="staffName"]
input text xpath=//*[@id="staffName"] ${staff-no}
log 输入工号
wait until element is visible xpath=//*[@id="staffNO"]
input text xpath=//*[@id="staffNO"] ${staff-no}
log 输入密码
wait until element is visible xpath=//*[@id="password"]
input password xpath=//*[@id="password"] 123456
log 选择技能组
@{skill-code} set variable 2 3 8
sleep 1
Comment click element xpath=//*[@id="tr_skillIdListStr"]/td/div/button
select from list xpath=//*[@id="deptSelect"] 10
click element //*[@id="tr_skillIdListStr"]/td/div/button
log 最小接线人数
wait until element is visible xpath=//*[@id="minReceptionNum"]
input text xpath=//*[@id="minReceptionNum"] 5
log 最大接线人数
wait until element is visible xpath=//*[@id="maxReceptionNum"]
input text xpath=//*[@id="maxReceptionNum"] 10
log 输入昵称
wait until element is visible xpath=//*[@id="nickName"]
input text xpath=//*[@id="nickName"] ${staff-no}
click element xpath=//*[@id="sData"]
${staff-no} # 员工工号
海淘登陆
${user}
${bro} open browser http://www.fengqu.com/index.html ff
Maximize Browser Window
点击元素 //*[@id="user-login"]
select frame //*[@class="register"]/div/iframe
wait until keyword succeeds 2 min 5 sec input text //*[@id="user-name"] ${user}
wait until keyword succeeds 2 min 5 sec input password //*[@id="user-pwd"] yiyi521
Comment input text //*[@id="user-name"] ${user}
Comment input password //*[@id="user-pwd"] yiyi521
点击元素 //*[@id="gotologin"]
unselect frame
wait until page contains 欢迎回来
${bro} # 登陆页面driver
海淘账户管理
海淘登陆 ${user}
点击元素 //*[@id="user-center"]
点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li/a
wait until page contains 基本资料
积分管理
海淘登陆 ${user}
点击元素 //*[@id="user-center"]
点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li/a
wait until page contains 我的积分
setting中的内容为三方库,可以使用开源的也可以自己编写,如何自己编写自己的库见下文
*** Settings ***
Library Selenium2Library
Library DatabaseLibrary
Variables中放的是变量,维度可以是project、suite、case,类型可以是单独变量或者list
*** Variables ***
${user} 13917745687 # 默认登陆用户
Test Cases中就是执行单元了,可以直接使用内置库、三方库、用户关键字
*** Test Cases ***
登陆
海淘登陆 ${user}
close all browsers
个人中心-消息中心
海淘登陆 ${user} #用户关键字
点击元素 //*[@id="user-center"]
点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li/a
wait until page contains 您暂未收到任何消息~#三方库方法
close all browsers
Keywords用户关键字,对于复用率高、可抽离公共熟悉的操作或者方法可以通过Keywords来将这些逻辑抽象出来,Keywords可以定义入参与返回值,这样可使得case中操作的连贯,也更易于case的维护
*** Keywords ***
带状态登录
${url}
open browser ${url} ff ff_profile_dir=${config}
Maximize Browser Window
sleep 1
重试
${key} ${args1}=none
wait until keyword succeeds 2 min 7 s ${key} ${args1}
等待元素
${locator} # 元素定位xpath、CSS、ID等
wait until page contains element ${locator}
点击元素
${locator}
重试 等待元素 ${locator}
重试 click element ${locator}
填写
${locator} ${content}=none
Selenium2Library.input text ${locator} ${content}
无视错误执行
${key} ${args1}=none
Run Keyword And Ignore Error ${key} ${args1}
组织测试用例
Test suites
Robot Framework中测试用例的集合被称为test suites。每个包含测试用例的文件形成了一个test suites。
Setup 和 Teardown
如果想在每个测试执行之前和之后执行某些操作,可以使用Test Setup和Test Teardown:
SettingValue
Test SetupClear Login Database #清理连接的数据库
Test Teardown
同样,也可以使用Suite Setup 和Suite Teardown来指定整形整个test suite之前和之后要执行的操作。
使用标签
Robot Framework允许给测试用例的设置标签。通过Default Tags或Force Tags可以所有的测试用例打上标签。也可以给单个测试用例打上标签。
在测试报告中可以看到测试及其标签以及基于标签的统计。标签有很多其他用途,最重要的是选择执行测试:
pybot --include smoke quickstart.html
pybot --exclude database quickstart.html
编写测试库
CombineLibrary.py
import clr
clr.AddReferenceToFileAndPath('Combine.dll') #include full path to Dll if required
from Test import Combine
def add_two_numbers(num1, num2):
try:
intNum1 = int(num1)
intNum2 = int(num2)
except:
raise Exception("Values must be integer numbers!")
cmb = Combine()
total = cmb.AddNumbers(intNum1, intNum2)
return str(total)
def add_two_words(word1, word2):
cmb = Combine()
return cmb.AddStrings(word1, word2)
测试报告
case运行结束后会生成三个文件:Output.xml、Log.html、Report.html
我们重点查看Log.html和Report.html ,Log.html更关注脚本的执行过程的记录,Report.html更关注脚本的执行结果的展示。
页:
[1]