读书笔记 - RF 自动化测试框架核心指南
Chapter1. 初识 RF
1. 功能
支持 Web 界面测试、Web 接口测试、GUI 测试、多终端测试
2. 框架
3. RF 提供的相关插件
- Eclipse plugin: https://github.com/NitorCreations/RobotFramework-EclipseIDE/wiki
- Robot Plugin for InteliJ IDEA: http://plugins.jetbrains.com/plugin/7430-robot-plugin
- Jenins plugin: https://wiki.jenkins-ci.org/display/JENKINS/Robot+Framework+Plugin
- Maven plugin: http://robotframework.com/MavenPlugin/
- Ant task: http://code.google.com/p/robotframework-ant
- Pabot: 1)pip install -U robotframework-pabot;2)https://github.com/mkorpela/pabot
- Atom plugin: https://atom.io/packages/language-robot-framework
4. 常用的使用
用途 | 操作 | 说明 |
快速查询关键字的API说明 | 选中关键字 + (Ctrl +Alt) | |
快速补全关键字 | 关键字前缀 + (Ctrl + Alt + BackSpace) | |
创建列表 |
@{list1} Create List hello rebot fremawork Log ${list1} |
1)"@"定义,$打印;2)中间用 tab |
创建字典 |
&{Dict1} Create Dictionary a=hello b=robotframework Log Many &{Dict1} |
& 定义字典 |
拼接字符 | ${var2} Catenate Hello Robot | |
Exit For Loop 退出循环 |
1) Run KeyWord If ${index}==3 Exit for loop 2) Exit For Loop If ${index}==3 |
|
Return From KeyWord vs Return From KeyWord If 返回及条件返回 |
常用在 用户自定义关键字中。参照下面的例子 | |
Should Match vs Should Be Equal |
Test_ShouldEqual Test_ShouldMatch |
Should Match:常用于字符串比较 Should Be Equal 可用于多种形式的比较 |
Built In 库其他关键字 |
1)字符串中字符或字符串出现的次数:Get Count 2)用例中导入 Import Library / Import Resource / Import Variables 3)Run Keywords:执行多个关键字 4)Run Keyword And Return:执行关键字并返回 5)Fail:直接判断失败 6)Wait Until Keyword Succeeds:等待的时间内循环,超过时间后失败还是失败 7)Pass Execution:跳过当前测试,后面的都不执行 |
Test Wait \Variable Should Exist ${value} Test PassExecution Test Fail |
----------- 用户自定义关键字 ----------- *** Keywords *** Example Return From Keyword [Arguments] ${valueReturn} @{list2} Create List 1 2 3 4 FOR ${value} IN @{list2} Return From Keyword If '${value}'=='${valueReturn}' ${value} Log ${value} END Return From KeyWord ${value} ----------- Test Case -------------- *** Settings *** Resource Custom_Keyword.txt *** Test Cases *** Test1.2 ${result} Example Return From Keyword 2 Log ${result} ========= Output ========= Starting test: RIDE Lesson.Chapter1.Test1.2 20211108 13:56:40.911 : INFO : @{list2} = [ 1 | 2 | 3 | 4 ] 20211108 13:56:40.914 : INFO : 1 20211108 13:56:40.915 : INFO : Returning from the enclosing user keyword. 20211108 13:56:40.916 : INFO : ${result} = 2 20211108 13:56:40.917 : INFO : 2 Ending test: RIDE Lesson.Chapter1.Test1.2
Chapter2. RF对数据库的操作
1. DatabaseLibrary库
1)库导入
- pip install -U robotframework-databaselibrary
- pip install PyMySQL
- RIDE 导入 DatabaseLibrary
2)相关操作
关键字 | 说明 |
Connect To Database | 数据库连接 |
Connect To Database Using Custom Params | 自定义参数连接 |
Query | 数据库查询 |
Execute Sql String | 执行 sql 语句 |
Execute Sql Script | 执行 sql 脚本 |
Row Count Row Count Is Equal to / Greater Than / Less Than X |
获取 sql 查询返回的记录数 |
Disconnect From Database | 断开数据库连接 |
2. MongoDBLibrary 库
1)库导入
- 连接:https://github.com/iPlantCollaborativeOpenSource/Robotframework-MongoDB-Library,查看说明
- RIDE 导入库 MongoDBLibrary
2)相关操作
关键字 | 说明 |
Connect To Mongodb | 连接到MongoDB数据库 |
Get Mongodb Databases | 获取当前MongoDB下的所有在用的数据库 |
Get Mongodb Collections | 获取到指定MongoDB数据库下的所有Collection |
Save MongoDB Records | 向指定的Collection中保存插入的记录 |
Retrieve All Mongodb Records | 获取指定Collection下的所有数据记录 |
Update Many Mongodb Records | 更新Collection中的数据记录 |
Remove Mongodb Records | 删除指定Collection中的数据记录 |
Disconnect From Mongodb | 断开已经建立的MongoDB数据库连接 |
Chapter 3. HTTP 接口自动化测试
库 | 关键字 | 用途 | 说明 |
HttpLibrary.HTTP |
1) pip install robotframework-httplibrary 2)RIDE 导入 HttpLibrary.HTTP |
||
1) Create Http Context |
创建一个 HTTP 调用的环境 Create Http Context host=www.baidu.com:80 scheme=http |
版本兼容问题 | |
Get / Post | get/post 请求 | ||
Get / Set Response Body | 获取 / 请求传参 body | ||
Get Response Status | 获取 status | ||
Get /Set Response Header | 获取 / 请求传参 header | ||
Follow Response | 重定向请求 | ||
RequestLibrary |
1)pip install requests 2)pip install robotframework-request 3)RIDE导入 RequestLibrary |
||
Create Session | 创建session 连接 | ||
Get Request -> Get On Session | get 请求 | ||
Post Request -> Post On Session | post 请求 | ||
RESTinstance |
1)pip install RESTinstance 2)RIDE导入 RESTinstance |
||
Post | RESTful 形式的 post 请求 | ||
Get | RESTful 形式的 get 请求 | ||
Put | RESTful 形式的 put 请求 |
Chapter4. 手机自动化测试
1. Appium第三方自动化框架
- IOS:Apple 的 UIAutomation
- Android:Google 的 UiAutomator
2. Appium 库的使用
关键字 | 解释 |
appium 库安装 |
1)pip install robotframework-appiumLibrary 2)导入 AppiumLibrary |
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=7.1.2 deviceName=3141a881 \app=E:\\TestData\\QiYunZhiCe\\qyzc_3.5.23.apk \skipServerInstallation=${True} newCommandTimeout=30000 |
打开APP |
Input Text | 输入文本 |
Clear Text | 清空文本 |
Click Button | 点击按钮 |
Click Element | 点击元素 |
Click A Point | 点击某个点 |
Click Element At Coordinates | 通过具体的坐标点击 element |
Get Element Location | 获取 element 的 x,y 坐标 |
Get Current Context | 获取当前的上下文 |
Get Contexts | 获取所有的上下文 |
Switch To Context | 页面切换,通过 Get Contexts,然后切换到指定的 Context |
Get Elements | 通过 locator 获取匹配到的所有元素 |
Get Element Attribute | 通过元素获取 属性值 |
Get /Set Network Connection Status | 获取/设置 手机的网络连接状态 |
Element Attribute Should Match Element Name Should Be Element Value Should Be |
元素属性判断 |
Chapter 5. Web 自动化测试
关键词 | 说明 |
导入 |
1)pip install robotframework-seleniumlibrary 2)下载浏览器对应的 driver 3)RIDE 导入 Selenium2Library |
Open/Close Browser | 打开/关闭 浏览器 |
Input Text | 输入文本 |
Click Button | 点击按钮 |
Click Element | 点击元素 |
Click Link | 点击链接 |
Get/Add/Delete Cookie | 向本地浏览器,获取/添加/删除 Cookie缓存 |
Get All Links | 获取所有页面上所有的 href 链接的元素对应的 id,无id时,输出空字符串 |
Choose File | 选择文件(多用于上传文件) |
Get Text | 获取文本内容 |
Get Title | 获取浏览器的 title |
Get Window Titles | 获取当前已经打开的浏览器窗口所有的 Title |
Get Value | 获取元素的 value 属性 |
Get WebElements / Get WebElement | 获取 WebElement 对象 所有的列表/匹配到的第一个对象 |
Go Back / Go to | 返回 / 跳转 |
Get List Items | 获取下拉列表的所有下拉选项 |
Get Selected List Value | 获取页面选中的一个下拉列表的 Value 值 |
Select From List | 从指定的下拉列表中选择指定的下拉列表选项 |
Selenium2Library 其他关键字 | P120-128 |
Chapter 6. 编写自定义的 Robot Framework Lib
6.1.1 使用 Python 编写的本地库
6.1.2 远程库 Remote
6.2 使用 Java 编写的
1)DatabaseLibrary
RoboFramework 官网,https://robotframework.org/#resources,1)下载 java版本的 Database Library (Java),2)解压,3)执行“mvn clean install -Dmaven.test.skip=true”,4)在 target 目录下,生成 robotframework-dblibrary-3.2.1-SNAPSHOT.jar
mysql 官网下载 jar 包,https://www.cnblogs.com/it-mh/p/11205866.html
Chapter 8. 自动化框架的设计
8.1 Jenkins
1. 在主节点上创建自动化部署
- 源码 SVN,设置RF源码的 SVN地址
- 构建,选择 batch file,
* pybot.bat 后面的命令行部分,可以参考自 RIDE 执行后生成的 命令输出
call pybot.bat -L DEBUG --argumentfile C:\Users\ADMINI~1\AppData\Local\Temp\2\RIDE2qk6q9zc.d\argfile.txt --listener "c:\program files (x86)\python37\lib\site-packages\robotide\contrib\testrunner\TestRunnerAgent.py:49731:False" C:\Users\jenkins\AppData\Local\Jenkins\.jenkins\workspace\RobotFramework自动化测试\RIDE_Lesson exit 0