自动化测试准备 之 python & robot framework 2016.01

来源:互联网 发布:怎么做淘宝返利链接 编辑:程序博客网 时间:2024/06/16 20:40

2016上半年准备在开发的间隙,完整重构下整个产品的测试体系,但鉴于精力问题,由自己准备方案并不断完善,由当前的测试部门准备全套测试用例,保持高效工作的同时,促进单位走出迈向现代化测试的第一步。

凡事需要循序渐进,鉴于以前的部分经验和接口测试的高性价比,自动化测试作为第一步。

学习的前提:一定要好好阅读官方手册,空闲时阅读下源码,解决问题很管用,还能促进python的能力,在后期的安全测试和运维中都能大展拳脚。

研发,测试,运维,每项工作都不轻松,好学上进是根本。

更新

20170216

更新到selenium 3.0.2后,需要增加geckodriver的支持

#更新seleniumpip install -U selenium#注意:mac下不要用brew去更新了,那个版本非常低

geckodriver下载地址:
https://github.com/mozilla/geckodriver/releases
下载&解压后将geckodriverckod 存放至 /usr/local/bin/ 路径下即可

20161016

#在树莓派下安装lxml时需要先安装以下支持库sudo apt-get install python-dev  # for python2.x installssudo apt-get install python3-dev  # for python3.x installssudo apt-get install libxml2-dev libxslt1-dev另外,安装xml时的编译过程会需要一点时间,谁让树莓派的性能还是比较弱的呢sudo apt-get install tesseract-ocr #树莓派需要单独安装tesseract,mac并不需要,估计ubuntu也是要安装的sudo pip install cssselect #树莓派在安装pyquery后并不会自动安装cssselect库

20161010

sudo easy_install pip   #mac下正确安装pip的方式(用brew需要再次安装python,会导致安装2个python)brew install chromedriver   #由于selenium总是跟不上Firefox的更新步伐,在mac上测试时改用Chrome更方便些#mac测试mysql从mysql官网下载mysql-connector-python-2.1.4-osx10.11.dmg并安装即可brew install tesseract #进行一些简单的验证码识别,例如执行:tesseract image.png output -l eng  可以将image.png中的验证码输出到output文件中sudo pip install pillow  # PIL,是pytesseract的依赖项sudo easy_install pytesseract #pytesseract对tesseract进行了python 封装,可以简单理解为执行上述tesseract命令并读取了output文件中的验证码sudo pip install lxml #支持xpath和xslt,我喜欢sudo pip install beautifulsoup4 #支持多种xml解析器,比较通用sudo pip install pyquery #jquery的python实现,其依赖于cssselect库(如果没有自动安装,一定要手动安装)

20160615

sudo apt-get install libgtk2.0-dev #ride要求wxPython 2.8.12.1 ,编译该库时需要先安装gtk2.0#由于升级到ubuntu16.04,其wxPython已经更新为3.0,故ride暂时无法使用#没有找到对应的wxPython版本:Wrong wxPython version.You need to install wxPython 2.8.12.1 with unicode support to run RIDE.

基本安装

//由于python安装在/usr下,需要root权限,下面不表yum install python-pip.noarch //安装python package 管理工具pip install robotframework pip install --upgrade robotframework  //更新pip uninstall robotframework //删除yum install wxPython.x86_64  //centos 安装wxpython图形库apt-get install python-wxgtk2.8  //ubuntu下安装wxpythonpip install robotframework-ride  //安装ridepip install robotframework-selenium2library  //操作浏览器的库,ui测试pip install robotframework-databaselibrary   //操作数据库的库,数据验证pip install pymysql   //安装databaselibrary需要使用的db api,这里是mysql,其他可以查询pip install requests            //pip install -U robotframework-requests  //http 库,接口测试//这里还需要pip install --upgrade robotframework-httplibraryride.py  //run ide!!!!

简单验证了一下后,依然还是那熟悉的感觉,挺好。

内部变量

名称 作用 备注 ${CURDIR} 当前目录 加载文件等等场合使用 ${TEST MESSAGE} 与Set Test Message相关 test teardown中可以访问 ${EMPTY} 空scalar @{EMPTY} 空list &{EMPTY} 空dict

注意事项

使用pymysql时中文出现??的问题

database='test', user='root', password='', host='localhost', port=3306,charset="utf8" //此处解决了中文编码

变量及创建

命令 描述 Set Variable 创建scalar变量(也可以创建list变量,但不推荐),例如${name} Create List 创建list变量,例如@{list1} Create Dictionary 创建dict变量,例如&{dict1}

Set xxx Variable 系列命令

命令 描述 Set Suite Variable 设定Suite范围变量 Set Test Variable 设定Test Case范围变量 Set Global Variable 设定Global范围变量,等同于命令行加options –variable 和 –variablefile参数(我建议少用该命令)
范例 Set Suite Variable ${scalar} Hello, world! children=true Set Suite Variable @{list} First Item Second Item Set Suite Variable &{dict} key1=value1 key2=value2 Set Suite Variable ${scalar} ${EMPTY} 赋空值 Set Test Variable ${scalar} 只修改scope,不进行赋值操作

Note:当某个变量已经存在时,也可以不赋值,只设定scope(不要设定值即可)

Escape format范例 ${Name} Set Variable \${index} 设定name指向index,类似别名的效果 Set Test Variable ${Name} Value 实际修改的是index的值 Set Test Variable \${Name} new value 123 重新设定name指向普通变量

Note:If the variable has value which itself is a variable (escaped or not), you must always use the escaped format to set the variable

os和sys模块在RobotFramework2.8 之后,进行evaluate操作时已经被自动导入

Run Keyword If os.sep == ‘/’ Unix Keyword … ELSE IF sys.platform.startswith(‘java’) … ELSE Windows Keyword
0 0
原创粉丝点击