Python自动化测试系列[v1.0.0][Robot Framework]

Robot Framework

Robot Framework 是通用型的验收测试和验收测试驱动开发的自动化测试框架(ATDD),基于Python语言开发,它本身只提供基本功能例如测试用例的组织、执行、测试报告的生成,以及一些标准库BuildIn,String,Screenshot,DataTime和Process等。
如果要是用这个框架做不同领域的自动化测试还单独安装扩展库,Robot FrameWork提供了丰富的Library。

测试领域 扩展库
Web自动化测试 Selenium、watir-robot等
Windows GUI测试 AutoItLibrary
移动测试 Android Library、IOS Library、Appium Library等
数据库测试 DataBase Library(Java)、DataBase Library(Python)、MongoDB Library等
文件对比测试 Diff Library
HTTP测试 HTTP Library(livetest)、HTTP Requests(Requests)等

环境搭建

安装3个Python模块,robotframework, robotframework-requests, request

C:\Users\Administrator>pip install robotframework
Collecting robotframework
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)")': /simple/robotframework/
  Downloading https://files.pythonhosted.org/packages/22/0f/1b9ffa0c4e59789b50e6034866e823b7d4a5c7eaedad7bfd0bba42f2aa9d/robotframework-3.1.2-py2.py3-none-any.whl (602kB)
    100% |████████████████████████████████| 604kB 6.4kB/s
Installing collected packages: robotframework
Successfully installed robotframework-3.1.2

C:\Users\Administrator>pip install robotframework-requests
Collecting robotframework-requests
  Downloading https://files.pythonhosted.org/packages/42/83/d9cc0a0b170c10eed016fdcc1a6fb6c5d9df050297830b759482ee438012/robotframework-requests-0.6.2.tar.gz
Requirement already satisfied: robotframework in c:\python37\lib\site-packages (from robotframework-requests) (3.1.2)
Requirement already satisfied: requests in c:\python37\lib\site-packages (from robotframework-requests) (2.21.0)
Requirement already satisfied: certifi>=2017.4.17 in c:\python37\lib\site-packages (from requests->robotframework-requests) (2018.11.29)
Requirement already satisfied: idna<2.9,>=2.5 in c:\python37\lib\site-packages (from requests->robotframework-requests) (2.8)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in c:\python37\lib\site-packages (from requests->robotframework-requests) (1.24.1)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\python37\lib\site-packages (from requests->robotframework-requests) (3.0.4)
Installing collected packages: robotframework-requests
  Running setup.py install for robotframework-requests ... done
Successfully installed robotframework-requests-0.6.2

C:\Users\Administrator>pip install requests
Collecting requests
  Downloading https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl (57kB)
    100% |████████████████████████████████| 61kB 10kB/s
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\python37\lib\site-packages (from requests) (1.24.1)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\python37\lib\site-packages (from requests) (3.0.4)
Requirement already satisfied: idna<2.9,>=2.5 in c:\python37\lib\site-packages (from requests) (2.8)
Requirement already satisfied: certifi>=2017.4.17 in c:\python37\lib\site-packages (from requests) (2018.11.29)
Installing collected packages: requests
Successfully installed requests-2.22.0

Python2 Robot Framework 开发环境配置

使用RIDE作为Robot Framework的编译工具:

应为RIDE依赖于wxPython,因此环境的配置必须严格匹配,可以采用如下组合:

wxPython robot Frame work robot framework-ride Python
wxPython2.8-win64-unicode-2.8.12.1-py27.exe robot frame work 3.0.4 robot framework-ride 1.5.2.1 Python2.7

如果环境安装成功,在命令行输入ride则可启动RIDE的GUI
Python自动化测试系列[v1.0.0][Robot Framework]_第1张图片
Python自动化测试系列[v1.0.0][Robot Framework]_第2张图片
Python自动化测试系列[v1.0.0][Robot Framework]_第3张图片

使用Sublime3作为Robot Framework编译工具

打开Sublime,使用快捷键Ctrl+` (键盘数字1前边的按键)调出控制台,逐行将如下代码黏贴到控制台并回车,执行完成后重启软件

import urllib.request,os; 
pf = 'Package Control.sublime-package'; 
ipp = sublime.installed_packages_path(); 
urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); 
open(os.path.join(ipp, pf), 'wb').write(urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ','%20')).read())
286331
reloading plugin Package Control.1_reloader
reloading plugin Package Control.2_bootstrap
reloading plugin Package Control.Package Control
Package Control: Not running package cleanup since bootstrapping is not yet complete
reloading settings Packages/Package Control/Package Control.sublime-settings
reloading settings Packages/User/Package Control.sublime-settings
reloading plugin 0_package_control_loader.00-package_control

重新打开软件后,选择Preferences选择Package Control 或者Ctrl+Shift+P,调出命令面板输入install
Python自动化测试系列[v1.0.0][Robot Framework]_第4张图片
然后在输入窗口输入install Package,然后鼠标左键点击第一个选项
Python自动化测试系列[v1.0.0][Robot Framework]_第5张图片
在新窗口中输入robot frameworkAssistant,检索出来后,然后点击该选项即可安装
安装完成后,会有如下提示信息:

Package Control Messages
========================

RobotFrameworkAssistant
-----------------------

  Features
  --------
  
  * Syntax highlighting/automatic detection/activation for Robot Framework '.robot' files
  * `Alt+Enter` or `Alt+Click` to go to source of item at cursor (either *Library*, *Resource*, *Keyword* or *Variable*)
  * `Ctrl+Alt+Enter` or `Ctrl+Alt+Click` on item to log *Keyword* documentation into output panel.
  * `Ctrl+Space` to auto complete library/resource name, keywords (can start with any part/word of keyword). Using of '.' after library/resource name is also supported (limits keywords to given library/resource);
  * Autocomplete gives *Keyword* with its *arguments* formatted according to Robot Framework syntax. Jump through arguments with `TAB` key.
  * `$`, `@` or `&` for autocomplete of *Built-in* and *Resource* variables
  * `:` and then `TAB` to get list of special Robot Framework syntax elements (like *[Arguments]*, *[Return]* etc);
  * `:f` and then `TAB` to insert *:FOR* loop template;
  * `*k`, `*s`, `*v` and then `TAB` to insert tables hedings templates
  * Toggle comments with `Cmd+/`
  * `Cmd+B` to run *pybot* with current file
  * `Robot Framework` context menu which allows to run *pybot* with current file, update internal database, insert snippets etc.
  
  
  Please visit https://github.com/andriyko/sublime-robot-framework-assistant for more information.

同时:
Python自动化测试系列[v1.0.0][Robot Framework]_第6张图片
在Sublime中新建一个文件,输入如下内容,并保存为robot格式的文件

***Setting***

***Test Cases***
testcase
    log    robot framework

在windows命令行执行该文件pybot test.robot执行结果为:

C:\Program Files\Sublime Text 3>pybot test.robot
==============================================================================
Test
==============================================================================
testcase                                                              | PASS |
------------------------------------------------------------------------------
Test                                                                  | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  C:\Program Files\Sublime Text 3\output.xml
Log:     C:\Program Files\Sublime Text 3\log.html
Report:  C:\Program Files\Sublime Text 3\report.html
命令 说明
pybot xxx.robot 执行文件名为xxx.robot的文件
pybot *.robot 执行当前目录中.robot格式的测试文件
pybot test_a 执行test_a文件夹中的所有用例
pybot ./ 执行当前目录下的所有用例

如果遇到提示'pybot' 不是内部或外部命令,也不是可运行的程序或批处理文件。,那么找到Python的安装路径
C:\Python37\Scripts,在该路径下新建一个bat文件,命名为pybot.bat,用记事本打开该文件,将如下内容输入并保存即可解决该问题

@Echo off

python -m robot.run %*

执行结束后实际上有三个结果呈现形式

Output:  C:\Program Files\Sublime Text 3\output.xml
Log:     C:\Program Files\Sublime Text 3\log.html
Report:  C:\Program Files\Sublime Text 3\report.html

Python自动化测试系列[v1.0.0][Robot Framework]_第7张图片

Pycharm 作为Robot Framework编译工具

为Pycharm安装插件
Python自动化测试系列[v1.0.0][Robot Framework]_第8张图片
安装插件需要重启,然后新建文件便可以选择robot file
Python自动化测试系列[v1.0.0][Robot Framework]_第9张图片
Python自动化测试系列[v1.0.0][Robot Framework]_第10张图片
Python自动化测试系列[v1.0.0][Robot Framework]_第11张图片

代码示例

*** Settings ***
Documentation    Robot Framework
Library     RequestsLibrary
Library     Collections
*** Test Cases ***
test_login_xxxxx
    # 查询发布会(GET请求)
    &{headers}    Create Dictionary    Content-Type=application/x-www-form-urlencoded
    &{payload}    Create Dictionary    userName=yangdawei    password=xxxxx
    Create Session    event    https://cnend.xxxxxx.com
    ${r}    Post Request    event    /cuss-login/login      data=${payload}     headers=${headers}
    Should Be Equal As Strings    ${r.status_code}    200
    log    ${r.json()}
    ${dict}    Set variable    ${r.json()}
    ${sta}    Get From Dictionary    ${dict}    code
    ${status}    Evaluate    "1"
    Should Be Equal    ${sta}    ${status}

测试报告

Python自动化测试系列[v1.0.0][Robot Framework]_第12张图片
更多Robot Framework 关键字
更多例子

*** Settings ***
Documentation    Robot Framework
Library     RequestsLibrary
Library     Collections
*** Test Cases ***
test_login_xxxxxx
    # 登录询盘云(POST请求)
    &{headers}    Create Dictionary    Content-Type=application/x-www-form-urlencoded
    &{payload}    Create Dictionary    userName=yangdawei_xxxx    password=xxxxx
    Create Session    event    https://cnend.xxxxxx.com
    ${r}    Post Request    event    /cuss-login/login      data=${payload}     headers=${headers}
    Should Be Equal As Strings    ${r.status_code}    200
    log    ${r.json()}
    ${dict}    Set variable    ${r.json()}
    ${sta}    Get From Dictionary    ${dict}    code
    ${status}    Evaluate    "1"
    Should Be Equal    ${sta}    ${status}


*** Keywords ***
Provided precondition
    Setup system under test

你可能感兴趣的:(玩转环境配置,Robot,Framework)