logo资料库

robot_framework自动化测试-by 虫师.pdf

第1页 / 共105页
第2页 / 共105页
第3页 / 共105页
第4页 / 共105页
第5页 / 共105页
第6页 / 共105页
第7页 / 共105页
第8页 / 共105页
资料共105页,剩余部分请下载后查看
《Robot Framework 自动化测试》 虫师分享 Robot Framework 自动化测试 作者:虫师 Robot Framework 特点:  使用简单  非常丰富的库  可以像编程一样写测试用例  支持开发系统关键字 http://fnng.cnblogs.com ---博客园 1
《Robot Framework 自动化测试》 虫师分享 目录 第 1 章 Robot Framework 介绍......................................................................................................................... 4 1.1 介绍.....................................................................................................................................................4 1.2 特点.....................................................................................................................................................5 1.3 例子.....................................................................................................................................................6 1.4 所支持的测试库................................................................................................................................ 7 第 2 章 Robot Framework 环境搭建............................................................................................................... 10 2.1 安装 Python............................................................................................................................. 10 2.2 安装 steuptools 与 pip......................................................................................................... 11 2.3 安装 Robot Framework........................................................................................................... 12 2.4 安装 wxPython......................................................................................................................... 13 2.4 安装 RIDE................................................................................................................................. 13 第 3 章 Robot Framework 入门....................................................................................................................... 14 3.1 创建项目.......................................................................................................................................... 15 3.1.1 创建测试项目...................................................................................................................... 15 3.1.2 从 F5 开始学习.................................................................................................................... 16 3.2 测试项目与测试套件的概念.......................................................................................................... 17 3.3 常用关键字介绍.............................................................................................................................. 19 log 就是“print”............................................................................................................. 19 3.3.1 3.3.2 定义变量.............................................................................................................................. 20 3.3.3 连接对象.............................................................................................................................. 21 3.3.4 定义列表.............................................................................................................................. 21 3.3.5 时间的操作.......................................................................................................................... 22 if 语句................................................................................................................................. 23 3.3.6 for 循环............................................................................................................................... 23 3.3.7 3.3.8 强大的 Evaluate................................................................................................................. 25 3.3.9 导入库.................................................................................................................................. 26 3.3.10 注释.................................................................................................................................... 28 Screenshot 库................................................................................................................................. 28 3.4.1 屏幕截图.............................................................................................................................. 29 Collections 库............................................................................................................................... 30 3.5.1 创建字典.............................................................................................................................. 30 3.5.2 操作字典.............................................................................................................................. 30 第 4 章 认识 RIDE............................................................................................................................................. 31 Edit 标签......................................................................................................................................... 32 4.1.1 导入库.................................................................................................................................. 33 4.1.2 导入资源.............................................................................................................................. 34 4.1.3 定义变量.............................................................................................................................. 35 4.1.4 定义列表变量...................................................................................................................... 36 Text Edit 标签............................................................................................................................... 37 Run.....................................................................................................................................................38 Run 标签............................................................................................................................... 39 4.3.1 4.3.2 运行与停止.......................................................................................................................... 41 4.2 4.3 4.1 3.4 3.5 http://fnng.cnblogs.com ---博客园 2
4.4 5.1 5.4 5.3 《Robot Framework 自动化测试》 虫师分享 4.3.3 报告与日志.......................................................................................................................... 44 4.3.4 筛选执行用例...................................................................................................................... 46 Settings.......................................................................................................................................... 49 4.4.1 测试用例的 Settings......................................................................................................... 49 4.4.2 测试套件的 Settings......................................................................................................... 50 4.5 用户关键字...................................................................................................................................... 51 4.5.1 创建用户关键字.................................................................................................................. 52 4.5.2 创建资源.............................................................................................................................. 54 第 5 章 Selenium2Library 库......................................................................................................................... 58 Selenium.......................................................................................................................................... 58 Selenium 介绍..................................................................................................................... 58 5.1.1 5.1.2 安装 Selenium2Library..................................................................................................... 58 5.1.3 第一个例子.......................................................................................................................... 59 5.2 元素定位.......................................................................................................................................... 61 5.2.1 前端工具.............................................................................................................................. 62 id 和 name 定位................................................................................................................... 64 5.2.2 xpath 定位........................................................................................................................... 64 5.2.3 css 定位............................................................................................................................... 66 5.2.4 Selenium2Library 关键字............................................................................................................. 68 5.3.1 浏览器驱动.......................................................................................................................... 69 5.3.2 关闭浏览器.......................................................................................................................... 69 5.3.3 浏览器最大化...................................................................................................................... 70 5.3.4 设置浏览器窗口宽、高...................................................................................................... 70 5.3.5 文本输入.............................................................................................................................. 70 5.3.6 点击元素.............................................................................................................................. 71 5.3.7 点击按钮.............................................................................................................................. 71 5.3.8 等待元素出现...................................................................................................................... 71 5.3.9 获取 title........................................................................................................................... 71 5.3.10 获取 text........................................................................................................................... 72 5.3.11 获取元素属性值................................................................................................................ 72 cookei 处理....................................................................................................................... 72 5.3.12 5.3.13 验证.................................................................................................................................... 73 5.3.14 表单嵌套............................................................................................................................ 73 5.3.15 下拉框选择........................................................................................................................ 73 5.3.16 执行 JavaScript............................................................................................................... 74 Robot Framework 分层设计........................................................................................................... 74 第 6 章 DatabaseLibrary 库........................................................................................................................... 79 6.1 安装 DatabaseLibrary 库.............................................................................................................. 79 6.2 操作 Oracle 数据库........................................................................................................................ 80 6.2.1 连接数据库.......................................................................................................................... 80 6.2.2 执行 SQL 语句...................................................................................................................... 81 6.2.3 执行 SQL 文件...................................................................................................................... 81 6.2.4 添加系统关键字.................................................................................................................. 81 第 7 章 AutoItLibrary 库............................................................................................................................... 84 7.1 安装 AutoItLibrary 库.................................................................................................................. 84 http://fnng.cnblogs.com ---博客园 3
7.2 7.3 《Robot Framework 自动化测试》 虫师分享 AutoIt v3 入门............................................................................................................................... 88 7.2.1 下载与安装.......................................................................................................................... 88 7.2.1 实现 web 上传...................................................................................................................... 89 AutoItLibrary 库........................................................................................................................... 93 7.3.1 操作计算器的例子.............................................................................................................. 94 7.3.2 运行程序.............................................................................................................................. 95 7.3.3 关闭程序.............................................................................................................................. 95 7.3.4 控制点击.............................................................................................................................. 96 7.3.5 发送...................................................................................................................................... 96 7.3.6 等待活动窗口...................................................................................................................... 97 7.3.7 鼠标点击.............................................................................................................................. 97 7.3.8 关闭进程.............................................................................................................................. 97 7.3.9 获得窗口的宽高.................................................................................................................. 98 7.3.10 窗口标题............................................................................................................................ 98 7.4 帮助.................................................................................................................................................. 99 第 8 章 系统关键字开发................................................................................................................................ 101 http://fnng.cnblogs.com ---博客园 4
《Robot Framework 自动化测试》 虫师分享 第 1 章 Robot Framework 介绍 本章对 Robot FrameWork 进行介绍。 1.1 介绍 Robot Framework 的架构是一个通用的验收测试和验收测试驱动开发的自动化测试框架(ATDD)。它 具有易于使用的表格来组织测试过程和测试数据。 New Test Case open browser input text click button close browser http://www.baidu.com id=kw id=su robot framework 它使用关键字驱动的测试方法。 对于上面的例子来说,open browser 、input text、click button 和 close browser,都是“关键 字”,这些关键字由 robotframework-selenium2library 类库所提供。当然,我们也可以自定义关键字。 其检测能力可以通过测试库实现可以使用 Python 或 Java 的扩展,用户可以使用相同的语法,用于创 建测试用例创建新的更高层次的现有的关键词。 Robot Framework 的操作系统和应用独立框架。核心框架是使用 Python 和运行在 Jython(JVM)和 IronPython(.NET)。 Clear 1.2 特点 Robot Framework has a modular architecture that can be extended with bundled and self-made test libraries. Test data is defined in files using the syntax shown in the examples below. A file containing test cases creates a test suite and placing these files into directories creates a nested structure of test suites. Easy http://fnng.cnblogs.com ---博客园 5
《Robot Framework 自动化测试》 虫师分享 When test execution is started, the framework first parses the test data. It then utilizes keywords provided by the test libraries to interact with the system under test. Libraries can communicate with the system either directly or using other test tools as drivers. Test execution is started from the command line. As a result you get report and log in HTML format as well as an XML output. These provide extensive look into what your system does. Modular. 测试数据语法............................................................................. 测试库 API................................................................................. 系统接口..................................................................................... 1.3 例子 下面通过官方网站的一个例子,从感官上认识一下通过 Robot Framework 是如何编写测试脚本的。 http://fnng.cnblogs.com ---博客园 6
《Robot Framework 自动化测试》 虫师分享 *** Settings *** Test Template Calculate Library CalculatorLibrary *** Test Cases *** Expression Expected Additions 12 + 2 + 2 2 + -3 16 -1 Substractions Multiplication Division Calculation error 12 - 2 - 2 2 - -3 12 * 2 * 2 2 * -3 12 / 2 / 2 2 / -3 [Template] kekkonen ${EMPTY} 1 / 0 8 5 48 -6 3 -1 Calculation should fail Invalid button 'k'. Invalid expression. Division by zero. *** Keywords *** Calculate [Arguments] Push buttons Result should be ${expression} C${expression}= ${expected} ${expected} Calculation should fail [Arguments] ${error} = Should be equal ${expression} Should fail ${expected} C${expression}= ${error} ${expected} 简单来分析一下 Robot Framework 编写测试脚本的套路: ***Settings*** 用于导入相关的库(Library),就像在通过 Python 编写脚本时,先 import 相关的模块一样。如 CalculatorLibrary 就是导入的库。 http://fnng.cnblogs.com ---博客园 7
《Robot Framework 自动化测试》 虫师分享 ***Test Cases*** 用于编写测试用例,在编写用例的过程中需要使用 Library 中所提供的关键字(或使用自定义的关键 字)。在编程语言中可以看作调用模块所提供的类或方法。如,Additions、Substractions 等就是 Library 所 提供的关键字。 *** Keywords *** 用于自定义关键字。在编程语言中就是可以理解成自己编写的函数,类、方法等。如,Calculate、 Calculation should fail 就是自定义关键字。 1.4 所支持的测试库 不同的测试库完成不同的测试功能,Robot Framework 通过导入不同的库,就可以使用库中所提供的 关键字,从而时行相关的测试。 有几个标准库是和 Robot Framework 捆绑在一起,除此之外 Robot Framework 还有大量的被分别开发 的外部库,你可以根据需要安装。当然,你也可以创建自己的测试库。 下面是 Robot Framework 官方网站所提供的库。 标准库: 库 Builtin Dialogs Collections OperatingSystem Remote 介绍 Provides a set of often needed generic keywords. Always automatically available without imports. Provides means for pausing the test execution and getting input from users. Provides a set of keywords for handling Python lists and dictionaries. Enables various operating system related tasks to be performed in the system where Robot Framework is running. Special library acting as a proxy between Robot Framework and test libraries elsewhere. Actual test libraries can be running on different machines and be implemented using any programming language supporting XML-RPC protocol. Screenshot Provides keywords to capture screenshots of the desktop. http://fnng.cnblogs.com ---博客园 8
分享到:
收藏