概述
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}
[Teardown] close all browsers 个人中心-消息中心 海淘登陆 ${user} 点击元素 //*[@id="user-center"] 点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li/a wait until page contains 您暂未收到任何消息~ [Teardown] close all browsers 个人中心-我的收藏 海淘登陆 ${user} 点击元素 //*[@id="user-center"] 点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li[2]/a wait until page contains 全部 [Teardown] close all browsers 个人中心-我的订单 海淘登陆 ${user} 点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li[3]/a wait until page contains 所有订单 [Teardown] close all browsers 个人中心-我的优惠券 海淘登陆 ${user} 点击元素 //*[@id="user-center"] 点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li[4]/a wait until page contains 未使用 [Teardown] close all browsers 个人中心-账户管理 海淘账户管理 [Teardown] 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"] [Teardown] close all browsers 个人中心-收货地址 海淘登陆 ${user} 点击元素 //*[@id="user-center"] 点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li[6]/a wait until page contains 添加收货地址 [Teardown] close all browsers 个人中心-积分管理 积分管理 [Teardown] close all browsers 个人中心-积分管理-收入 积分管理 点击元素 //*[@class="integral-tab-c1 fl"]/ul/li[2]/a #/html/body/div[2]/div[2]/div/div/div[2]/div/div[1]/div[2]/div[1]/ul/li[2]/a [Teardown] close all browsers 个人中心-积分管理-支出 积分管理 点击元素 //*[@class="integral-tab-c1 fl"]/ul/li[3]/a #/html/body/div[2]/div[2]/div/div/div[2]/div/div[1]/div[2]/div[1]/ul/li[2]/a [Teardown] close all browsers 个人中心-邀请有礼 海淘登陆 ${user} 点击元素 //*[@id="user-center"] 点击元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li[8]/a wait until page contains 邀请有礼 [Teardown] close all browsers *** Keywords *** 带状态登录 [Arguments] ${url} open browser ${url} ff ff_profile_dir=${config} Maximize Browser Window sleep 1 重试 [Arguments] ${key} ${args1}=none wait until keyword succeeds 2 min 7 s ${key} ${args1} 等待元素 [Arguments] ${locator} # 元素定位xpath、CSS、ID等 wait until page contains element ${locator} 点击元素 [Arguments] ${locator} 重试 等待元素 ${locator} 重试 click element ${locator} 填写 [Arguments] ${locator} ${content}=none Selenium2Library.input text ${locator} ${content} 无视错误执行 [Arguments] ${key} ${args1}=none Run Keyword And Ignore Error ${key} ${args1} 随机数 ${num} evaluate random.randint(10000000,99999999) random [Return] ${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[3]/a 提取数据 [Arguments] ${data} ${out} evaluate ${data}[0][0] [Return] ${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[5]/div[3]/input[4] 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[1]/a/span 20 click element xpath=//*[@id="sidebar"]/ul/li[1]/a/span sleep 1 新增部门 后台sso 展开后台 点击元素 //*[@id="sidebar"]/ul/li[1]/ul/li[8]/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(