前言
前言
如果这是一部电影的话, 名字或许该叫重制 或者 xxx - 2。这是因为我们已经写过一篇, 我个人觉得,比较完整的“基于我过去三年经验的robot 框架教程”的文章。那么为啥还要在写一篇呢, 有两个原因:
- 尽管没有robot 框架没有革命性变化,但是随着时间的推移,它也在不停的进化和改变。在这篇新的文章里将有所体现。
- 当然并且我们已经学习了新的东西和新的框架和整体测试方法的经验, 这也是将在这篇新的教程中体现的。
Robot 框架快览
在这里来一个对于robot framework的快览。了解一下安装步骤等等事项。好了,来吧干了这杯。。。
- Robot 是基于python实现的。
- 也有java实现版本
- 测试功能是基于关键字(keywords)实现的,这是robot测试功能的中心思想。
- 对于不同方面测试的关键字,例如数据库测试,是和不同的测试包结合在一起的。Robot framework本身自带了标准测试包。(新手套装 :))
- 新的关键字和新的测试功能可以用python或者java来写。
- 也可以用已有的关键字创建新的关键字。就像自己搞一个自己的编程语言一样,略碉。
- robot框架有着很好的日志功能,可以和一些持续集成环境。
安装robot
我用Python, 我就只翻译python的方法了,java的亲请自行google 或者
参照原文
pip install robotframework
robot --version Robot Framework 3.0 (Python 2.7.11 on darwin)这样就好了, 就是这么简单。
关键字 keywords
robot 官网已经有了一个
非常完整的用户手册。有可能你会觉得这篇博客会有些多余,但是我个人觉得这篇文章还是在某些方面的叙述比官方手册要具体些。比如: 关键字。
第一个例子:
现在我们来写一个很简单的robot 框架测试。
*** Settings *** *** Test Cases *** Test Robot Framework Logging Log "Test Logging"
在 test cases里面我们定义了一个测试。这个测试执行了一个关键字 Log, 而且我们给这个关键字传递了一个参数,一个字符串: “Test Logging”。在关键字和参数之间必须有
两个空格区分,而且每个test cases 必须有两个字符的缩进。这两个都能从例子中体现,但是有时候总会被忽略。测试可以用pybot 或者 java执行。这里我用的是python。
pybot --outputdir ./report example.robot
--outputdir 是用来定义日志和报告文件存储地址的。这里我们存在report 子目录下。
Robot 框架支持不同的测试的格式,这里我用的是robot后缀,这个和原文略有不同,是因为这个是pycharm robot framework插件默认的新测试的格式后缀。这个插件可以自动生成一些测试模板。但是基本和原文里用txt的方法一样,只要将txt里的内容拷贝到新robot文件里就好了。其他你还可以用 HTML 或者 行为测试描述。这两种方法略难,就不赘述了。这里的平文的方法是最简单和直接的, 使用者只需要习惯那些时常出现的多出来的空格。
关键字--一个编程语言
使用关键字,就和学习一门新的编程语言一样,所以你需要记忆一些语法,幸运的是robot的语法不是很复杂。当然你还必须知道这门新的语言给你提供了些什么,对于robot来说你需要知道例如怎么实现一些结构,像循环和条件语句等一些关键字,这就又意味着需要知道新手装中哪些测试包和关键字是可用的。
在来一个例子:
*** Settings *** Library String *** Test Cases *** Test Robot Framework Logging Log Test Logging Log Many First Entry Second Entry Log To Console Display to console while Robot is running Test For Loop : FOR ${INDEX} IN RANGE 1 3 \ Log ${INDEX} \ ${RANDOM_STRING}= Generate Random String ${INDEX} \ Log ${RANDOM_STRING}我个人比较喜欢这个例子,虽然短但是基本给了一些怎么使用关键字的方法。首先我们添加了两个新的不同关键字的测试,在原来的关键字底下添加新的关键字, 这基本就是实现多个测试的方法,尽管这里我们只是实现了日志测试。例子中还描述了怎么定义一个新的测试集。第二个测试集给出了怎么实现循环和怎么用关键字赋值。这里的 关键字 “Generate Random String” 是来自 String 包里面的, 我们在Setting里面导入了这个包,这个包是属于新手装里面的。
定义自己的关键字
知道现在我们都是用的已有的关键字,现在我们来写一个新的关键字。
*** Settings *** *** Test Cases *** Test Robot Framework Logging Log Test Logging Test My Robot Framework Logging My Logging My Message WARN *** Keywords *** My Logging [Arguments] ${msg} ${level} Log ${msg} ${level}新的关键字写在Keywords区域下面, 语法和写一个测试集相似。主要的区别是你可以给你自己设定的关键字传递任意的参数。例如Login关键字通常会得到一对username 和 password的参数,然后将所有的实现方法隐藏起来。如果你的keywords特别多,或者你想让你的自定义keywords更加容易的重复使用,你可以将keywords定义块放在一个resource文件中,然后在setting中导入这个文件,如下。
*** Settings *** Resource resource-2.txt *** Test Cases *** Test Robot Framework Logging Log "Test Logging" Test My Logging My Logging "Test My Logging 1" "Test My Logging 2"除了 基本的新建keywords的用法,你还可以在keywords块定义Setup和Teardown。
*** Settings *** Suite Setup Setup Actions Suite Teardown Teardown Actions *** Test Cases *** Test Robot Framework Logging Log Test Logging *** Keywords *** Setup Actions Log Setup Actions done here Teardown Actions Log Teardown Actions done here
Tagging 标签
这个和keywords关系不大,就是在读测试报告的时候比较方便看出你在测什么。
*** Settings *** *** Test Cases *** Test Robot Framework Logging [Tags] sample logging Log "Test Logging"
测试报告 和 日志 文件
Robot 框架的报告和日志功能都很碉。请看。


Java 部分的keywords实现翻译,就交给大家了, 我只用python。
Seleniun2Library: 一个替代SeleniumLibrary的库
Selenium 简介
Selenium 知道现在还是首选的网络应用的测试工具。起源于2004年,经历了不少改进。最大的改动是从Selenium 1 (RC)到 Selenium 2 (Web-Driver)的转变。 现在 Selenium 2 的出现已经不是什么新闻了。但是还有很多使用robot 框架的项目还在使用1代的产品。这篇博客希望能够在转到Selenium 2上面提供帮助, 并且告诉大家这个转变并不需要花费很大经历,况且你在测试稳定性上能得到极大的提高。
一代和二代的对比
原文提供了一张很长对比表, 我就直接拷贝了。表中罗列的只是两代中同样的keywords的对比。请注意这里面对比的是Robot关键字,非Selenium原生方法。我在看原文的时候也差点被带沟里。我就说我怎么没在Selenium Web Driver里面听说过 keywords这个术语呢。
SeleniumRC | Selenium2 | SeleniumRC | Selenium2 | SeleniumRC | Selenium2 |
– | Add Cookie | Add Location Strategy | Add Location Strategy | Alert Should Be Present | Alert Should Be Present |
Assign Id To Element | Assign Id To Element | Call Selenium Api | – | – | Assign Id To Element |
Capture Page Screenshot | Capture Page Screenshot | Capture Screenshot | – | Checkbox Should Be Selected | Checkbox Should Be Selected |
Checkbox Should Not Be Selected | Checkbox Should Not Be Selected | Choose Cancel On Next Confirmation | Choose Cancel On Next Confirmation | Choose File | Choose File |
– | Choose Ok On Next Confirmation | – | Clear Element Text | Click Button | Click Button |
Click Element | Click Element | Click Flex Element | – | – | Click Element At Coordinates |
Click Image | Click Image | Click Link | Click Link | Close All Browsers | Close All Browsers |
Close Browser | Close Browser | Close Window | Close Window | Confirm Action | Confirm Action |
– | Create Webdriver | Current Frame Contains | Current Frame Contains | Current Frame Should Contain | – |
– | Current Frame Should Not Contain | Delete All Cookies | Delete All Cookies | Delete Cookie | Delete Cookie |
– | Dismiss Alert | Double Click Element | Double Click Element | Double Click Flex Element | – |
Drag And Drop | Drag And Drop | – | Drag And Drop By Offset | Element Should Be Disabled | Element Should Be Disabled |
Element Should Be Enabled | Element Should Be Enabled | Element Should Be Visible | Element Should Be Visible | Element Should Contain | Element Should Contain |
Element Should Not Be Visible | Element Should Not Be Visible | – | Element Should Not Contain | Element Text Should Be | Element Text Should Be |
– | Execute Async Javascript | Execute Javascript | Execute Javascript | Flex Element Property Should Be | – |
Flex Element Should Exist | – | Flex Element Should Not Exist | – | Flex Element Text Should Be | – |
Focus | Focus | Frame Should Contain | Frame Should Contain | Frame Should Contain Text | – |
Get Alert Message | Get Alert Message | Get All Links | Get All Links | Get Cookie Value | Get Cookie Value |
Get Cookies | Get Cookies | Get Element Attribute | Get Element Attribute | Get Horizontal Position | Get Horizontal Position |
Get Inner Html | – | Get List Items | Get List Items | Get Location | Get Location |
Get Matching Xpath Count | Get Matching Xpath Count | Get Selected List Label | Get Selected List Label | Get Selected List Labels | Get Selected List Labels |
Get Selected List Value | Get Selected List Value | Get Selected List Values | Get Selected List Values | – | Get Selenium Implicit Wait |
– | Get Selenium Speed | – | Get Selenium Speed | – | Get Selenium Timeout |
Get Source | Get Source | Get Table Cell | Get Table Cell | Get Text | Get Text |
Get Title | Get Title | Get Value | Get Value | Get Vertical Position | Get Vertical Position |
– | Get Webelement | – | Get Webelements | Get Window Identifiers | Get Window Identifiers |
Get Window Names | Get Window Names | – | Get Window Position | – | Get Window Size |
Get Window Titles | Get Window Titles | Go Back | Go Back | Go To | Go To |
Highlight Element | – | Input Password | Input Password | Input Text | Input Text |
Input Text Into Flex Element | – | – | Input Text Into Prompt | List Selection Should Be | List Selection Should Be |
List Should Have No Selections | List Should Have No Selections | – | List Windows | Location Should Be | Location Should Be |
Location Should Contain | Location Should Contain | – | Locator Should Match X Times | – | Log Location |
Log Source | Log Source | – | Log Title | Maximize Browser Window | Maximize Browser Window |
Mouse Down | Mouse Down | Mouse Down On Image | Mouse Down On Image | Mouse Down On Link | Mouse Down On Link |
Mouse Out | Mouse Out | Mouse Over | Mouse Over | Mouse Up | Mouse Up |
Open Browser | Open Browser | Open Context Menu | Open Context Menu | Page Should Contain | Page Should Contain |
Page Should Contain Button | Page Should Contain Button | Page Should Contain Checkbox | Page Should Contain Checkbox | Page Should Contain Element | Page Should Contain Element |
Page Should Contain Image | Page Should Contain Image | Page Should Contain Link | Page Should Contain Link | Page Should Contain List | Page Should Contain List |
Page Should Contain Radio Button | Page Should Contain Radio Button | Page Should Contain Textfield | Page Should Contain Textfield | Page Should Not Contain | Page Should Not Contain |
Page Should Not Contain Button | Page Should Not Contain Button | Page Should Not Contain Checkbox | Page Should Not Contain Checkbox | Page Should Not Contain Element | Page Should Not Contain Element |
Page Should Not Contain Image | Page Should Not Contain Image | Page Should Not Contain Link | Page Should Not Contain Link | Page Should Not Contain List | Page Should Not Contain List |
Page Should Not Contain Radio Button | Page Should Not Contain Radio Button | Page Should Not Contain Textfield | Page Should Not Contain Textfield | Press Key | Press Key |
Press Key Native | – | Radio Button Should Be Set To | Radio Button Should Be Set To | Radio Button Should Not Be Selected | Radio Button Should Not Be Selected |
Register Keyword To Run On Failure | Register Keyword To Run On Failure | Reload Page | Reload Page | – | Remove Location Strategy |
Select All From List | Select All From List | Select Checkbox | Select Checkbox | Select Flex Application | – |
Select Frame | Select Frame | Select From Flex Element | – | Select From List | Select From List |
– | Select From List By Index | – | Select From List By Label | – | Select From List By Value |
Select Radio Button | Select Radio Button | Select Window | Select Window | – | Set Browser Implicit Wait |
– | Set Screenshot Directory | – | Set Selenium Implicit Wait | Set Selenium Speed | Set Selenium Speed |
Set Selenium Timeout | Set Selenium Timeout | – | Set Window Position | – | Set Window Size |
Simulate | Simulate | Start Selenium Server | – | Stop Selenium Server | – |
Submit Form | Submit Form | Switch Browser | Switch Browser | Table Cell Should Contain | Table Cell Should Contain |
Table Column Should Contain | Table Column Should Contain | Table Footer Should Contain | Table Footer Should Contain | Table Header Should Contain | Table Header Should Contain |
Table Row Should Contain | Table Row Should Contain | Table Should Contain | Table Should Contain | – | Textarea Should Contain |
– | Textarea Value Should Be | Textfield Should Contain | Textfield Should Contain | Textfield Value Should Be | Textfield Value Should Be |
Title Should Be | Title Should Be | Unselect Checkbox | Unselect Checkbox | Unselect Frame | Unselect Frame |
Unselect From List | Unselect From List | – | Unselect From List By Index | – | Unselect From List By Label |
– | Unselect From List By Value | Wait For Condition | Wait For Condition | – | Wait For Flex Element |
– | Wait Until Element Contains | – | Wait Until Element Does Not Contain | – | Wait Until Element Is Enabled |
– | Wait Until Element Is Not Visible | – | Wait Until Element Is Visible | Wait Until Page Contains | Wait Until Page Contains |
Wait Until Page Contains Element | Wait Until Page Contains Element | Wait Until Page Loaded | – | – | Wait Until Page Does Not Contain |
– | Wait Until Page Does Not Contain Element | Xpath Should Match X Times | Xpath Should Match X Times |
- Call Selenium Api
- Capture Screenshot
- Click Flex Element
- Current Frame Should Contain
- Double Click Flex Element
- Flex Element Property Should Be
- Flex Element Should Exist
- Flex Element Should Not Exist
- Flex Element Text Should Be
- Frame Should Contain Text
- Get Inner Html
- Highlight Element
- Input Text Into Flex Element
- Press Key Native
- Select Flex Application
- Select From Flex Element
- Start Selenium Server
- Stop Selenium Server
- Wait Until Page Loaded
不同点的分析
回到主题, 貌似这个selenium 2 没有的keywords略多,达不到和一代“无缝替代”的名号,OK, 让我静静。。。
没有出现的 flex-keywords 关系不大,忽略。start 和 stop selenium server,很明显二代已经不需要了,因为根本没有server给你来启动和停止。Highlight Element 是用做调试用的。Get Inner HTML 和 Capture Screenshot 被 Capture Page Screenshot取代了。Current Frame Should Contain 和 Frame Should Contain Text 分别被 Select Frame 和 Current Frame Contains 取代了。Wait Until Page Load 已经被关键字内置的wait机制所取代, 你可以给关键字一个非必须的参数 “dont_wait”来控制这个wait机制的开关。这样算算,其实就只剩下 Call Selenium API 和 Press Key Native了。Call Selenium API 是用来执行一些没有被弄成keywords的Selenium原生API。因为Selenium 2库加了新的keywords,那些没有的可能已经被包含在新的keywords里面了。The Press Key Native 也不常用 忽略。
基本大概就是这样,用Selenium 2 有几个好处:
- Webdriver API 是支持真的浏览器的,所以就不要运行一个selenium 服务器了。
- 你可能可以用无头模式测试: 我估计是用phantomjs之类的无头浏览器。
- 更好的支持使用AJAX 测试应用。
- 如果项目较小,就直接把所有的测试拷贝一下,而后新旧两个测试同时运行一段时间,直到新的测试可以达到交付标准。
- 大的话,同样的方法也可以,但是最后是对于软件内部构件一个一个的来做。
- 在产品开发中, 有一些库说不定已经包含了Selenium,那么最好也做个备份,然后平行的运行两个新旧的测试,知道变更的库是可以运行的。
和TeamCity CI 一起用
Well 我发现TeamCity 还算比较好用。就试着用了一下。整体上 和Jenkin的方法类似,Jenkin的我就不翻译了,大家触类旁通就好了。懂得。。。
自动化测试如果没有和CI 服务器联合的话,是完全没有意义的。所以这章和下一章都是关于怎么把Robot框架和不同的CI平台结合。并且我们经常用Jenkins来作CI服务器,是时候讲讲什么不一样的了。Well,所以我们现在讲讲Robot框架和Teamcity CI 服务器的结合。
安装
Teamcity 可以直接去人家的官网下载。
tar xvf TeamCity-9.1.6.tar
cd TeamCity/bin
./runAll.sh start
打开一个浏览器, 输入
http://localhost:8111/mnt。
然后各种proceed,各种安装。
如果一切顺利,应该是这个画面

点击project 然后创建一个新的项目。各种填。
填好了之后,你应该能看到这个画面。

点击 create build configuration 来添加新的设置。然后又是各种填。
填完之后,应该是这个画面。

点击 Add build step

Well 这个画面是魔法的开始。在custom script里面我们定义了 一些我们想要执行的命令。基本这三个命令的含义是 变换到robot 测试文件的文件夹, 然后运行测试文件,然后退出文件夹。