使用Zombie.js进行Web自动化测试
作者:网络转载 发布时间:[ 2016/2/19 16:40:00 ] 推荐标签:自动化测试 WEB测试
Zombie.js 是一个node.js环境下,非常小巧高效率的Web UI Automation Test库.本文将介绍如何使用Zombie.js对Web UI进行自动化测试.
本文使用到的环境:
node.js + coffeescript
mocha
gulp.js
express.js
快速预览Zombie.js测试
const Browser = require('zombie');
Browser.localhost('example.com', 3000);
describe('User visits signup page', function() {
const browser = new Browser();
before(function(done) {
browser.visit('/signup', done);
});
describe('submits form', function() {
before(function(done) {
browser
.fill('email', 'zombie@underworld.dead')
.fill('password', 'eat-the-living')
.pressButton('Sign Me Up!', done);
});
it('should be successful', function() {
browser.assert.success();
});
it('should see welcome page', function() {
browser.assert.text('title', 'Welcome To Brains Depot');
});
});
});
Zombie.js适用场景
好的方面
Zombie.js与传统的Selenium和PhandomJS不同,它不会启动真正的浏览器,使得测试运行效率媲美单元测试。 Zombile.js默认采用mocha风格编写测试,无需再为WebDriver做额外的配置,如果你熟悉mocha, Zombie.js将是开箱即用的库.
Features
模拟浏览器行为
Assertions, 可以采用jQuery的Selector对dom进行assert
Cookies
Ajax & WebSocket
限制
由于Zombie.js没有真正启动传统的WebDriver, 因此过于复杂的场景将会无法测试. 例如,如何对高德地图经测试,我还没有找到好的方法.
安装Zombie.js
npm install -g mocha npm install zombie --save-dev
由于本文使用coffeescript代替javascript, 还需要安装coffeescript环境
npm install -g coffee-script
编写第一个测试
Brower = require 'zombie'
Brower.localhost('yourdomain.com', 5000)
describe 'User visits login page', () ->
browser = new Brower()
before (done) ->
browser.visit '/login', done
describe 'submits login form', () ->
before (done) ->
browser
.fill 'username', 'xxxx@mail.com'
.fill 'password', 'password'
.pressButton('登录', done)
it 'should be successful', () ->
browser.assert.success()
it 'should visit admin page', () ->
browser.assert.url /^http://yourdomain.com/users/d/admin/
it 'should see profile button with email', () ->
browser.assert.link('#profile-button', 'xxxx@mail.com', '#')
运行测试:
开启你的web server
运行测试
mocha --harmony --compilers coffee:coffee-script/register login_spec.coffee
由于Zombie.js使用到了ECMA 6,需要使用 --harmony 参数开启 node.js 对ECMA 6语法支持.
使用Gulp.js构建Build Pipeline
通常我们会讲自动化测试加入到build pipeline中. 这里将介绍将 Express.js + Zombie.js + gulp.js的配置方法.
Build pipeline策略
checkstyle, 检查代码格式
运行unit test
compile coffeescript -> javascript
启动server
运行automation test
出错或者结束测试,停止server
由于gulp-develop-server无法用coffeescript启动server,所以需要添加compile步骤.
express.js项目结构组织
├── acceptence-test // automation test代码 │ ├── admin_spec.coffee │ └── login_spec.coffee ├── app //后台app代码 coffeescript ├── bin ├── config ├── dist //coffeescript编译后的js代码 ├── gulpfile.coffee ├── gulpfile.js ├── node_modules ├── package.json ├── public ├── spec // unit test代码 │ ├── activities_spec.coffee │ ├── auth_spec.coffee │ └── projects_spec.coffee └── views
安装依赖
npm install -g gulp npm install gulp-mocha gulp-coffee gulp-coffeelint gulp-sync gulp-task-listing gulp-develop-server harmonize --save-dev

sales@spasvo.com