使用Nightwatch.js做基于浏览器的web应用自动测试
2014-03-25 23:48
471 查看
1 安装
1.1 安装node.js
在http://nodejs.org/ 上下载适合本机系统的安装包运行安装,注意安装选项中选择npm tool以用于后续依赖包的安装。
1.2 通过npm工具安装Nightwatch
命令行运行“npm install nightwatch”,如下的提示表明安装成功。
1.3 Npm相关目录结构
所有npm安装的模块都会基于当前cmd窗口的目录,也就是说如果cmd的工作目录是在c:\根目录,则会在该目录下创建node_modules文件夹,并将安装的模块都放到该目录下,如果通过windows附件程序或者win+R启动的,则工作目录在“%USERPROFILE%\”下。
Npm安装所下载的临时文件保存在“%appdata%\npm-cache”下。
1.4 下载Selenium WebDriver server
http://selenium-release.storage.googleapis.com/index.html上下载最新版本的jar包,并将其放到NightWatch的bin目录下。
2 实例使用
2.1 nightwatch.js中增加引用
在”\nightwatch\tests\nightwatch.js”中增加引用“require('../bin/runner.js');”
2.2 运行nightwatch.js
命令行下,cd到nightwatch所在的目录,然后运行“node./tests/nightwatch.js”
2.3 异常处理
如果没意外,执行上述js的时候会抛类似下面的异常,不要慌张,根据异常提示,安装所需要的module即可,安装方法“npm install xxx”。
3 基本原理
4 测试套件
Nightwatch.js makes it possible to organizedyour test scripts into groups and run them as needed. To group tests togetherjust place them in the same sub-folder. The folder name is the name of thegroup.例如下面的目录结构。
5 自己的脚本
在nightwatch根目录下建一个名为test.js的文件:
require('./bin/runner.js');
var nightwatch = require('./index.js');
module.exports = {
"step one" : function (browser) {
browser
.url("http://www.google.com.hk")
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', 'nightwatch')
.waitForElementVisible('button[name=btnG]', 1000)
},
"step two" : function (browser) {
browser
.click('button[name=btnG]')
.pause(1000)
.assert.containsText('#main', 'The Night Watch')
.end();
}
};
然后”node ./test.js”运行:
更多的使用参见其api文档:http://nightwatchjs.org/api
6 产品特征
Ø Simple but powerful syntax which enables you to write tests veryquickly, using only Javascript and CSS selectors. No need to initialize otherobjects and classes, you only need to write the test specs.
Ø Built-in command-line test runner which enables you to run the testseither altogether, by group or single.
Ø Manages the Selenium server automatically; can be disabled ifSelenium runs on another machine.
Ø Continous Integration support: JUnit XML reporting is built-in soyou can integrate your tests in your build process with systems suchs as Hudsonor Teamcity.
Ø Use CSS selectors or Xpath to locate and verify elements on the pageor execute commands.
Ø Easy to extend if you need to implement your own commands specificto your application.
1.1 安装node.js
在http://nodejs.org/ 上下载适合本机系统的安装包运行安装,注意安装选项中选择npm tool以用于后续依赖包的安装。
1.2 通过npm工具安装Nightwatch
命令行运行“npm install nightwatch”,如下的提示表明安装成功。
1.3 Npm相关目录结构
所有npm安装的模块都会基于当前cmd窗口的目录,也就是说如果cmd的工作目录是在c:\根目录,则会在该目录下创建node_modules文件夹,并将安装的模块都放到该目录下,如果通过windows附件程序或者win+R启动的,则工作目录在“%USERPROFILE%\”下。
Npm安装所下载的临时文件保存在“%appdata%\npm-cache”下。
1.4 下载Selenium WebDriver server
http://selenium-release.storage.googleapis.com/index.html上下载最新版本的jar包,并将其放到NightWatch的bin目录下。
2 实例使用
2.1 nightwatch.js中增加引用
在”\nightwatch\tests\nightwatch.js”中增加引用“require('../bin/runner.js');”
2.2 运行nightwatch.js
命令行下,cd到nightwatch所在的目录,然后运行“node./tests/nightwatch.js”
2.3 异常处理
如果没意外,执行上述js的时候会抛类似下面的异常,不要慌张,根据异常提示,安装所需要的module即可,安装方法“npm install xxx”。
3 基本原理
4 测试套件
Nightwatch.js makes it possible to organizedyour test scripts into groups and run them as needed. To group tests togetherjust place them in the same sub-folder. The folder name is the name of thegroup.例如下面的目录结构。
5 自己的脚本
在nightwatch根目录下建一个名为test.js的文件:
require('./bin/runner.js');
var nightwatch = require('./index.js');
module.exports = {
"step one" : function (browser) {
browser
.url("http://www.google.com.hk")
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', 'nightwatch')
.waitForElementVisible('button[name=btnG]', 1000)
},
"step two" : function (browser) {
browser
.click('button[name=btnG]')
.pause(1000)
.assert.containsText('#main', 'The Night Watch')
.end();
}
};
然后”node ./test.js”运行:
更多的使用参见其api文档:http://nightwatchjs.org/api
6 产品特征
Ø Simple but powerful syntax which enables you to write tests veryquickly, using only Javascript and CSS selectors. No need to initialize otherobjects and classes, you only need to write the test specs.
Ø Built-in command-line test runner which enables you to run the testseither altogether, by group or single.
Ø Manages the Selenium server automatically; can be disabled ifSelenium runs on another machine.
Ø Continous Integration support: JUnit XML reporting is built-in soyou can integrate your tests in your build process with systems suchs as Hudsonor Teamcity.
Ø Use CSS selectors or Xpath to locate and verify elements on the pageor execute commands.
Ø Easy to extend if you need to implement your own commands specificto your application.
相关文章推荐
- 使用Nightwatch.js做基于浏览器的web应用自动测试
- 基于vue.js 2.0,不使用webpack的nodejs服务,只在浏览器上单独使用在Element UI的Tree树形控件
- Nightwatch.js – 轻松实现浏览器的自动测试
- 基于vue.js 2.0,不使用webpack,只在浏览器上单独使用Element UI的Table表格控件完成增删改查页面
- 基于Netty5.0高级案例二之WebSocket中关于使用ProtoBuf传输数据介绍js部分
- 基于wke封装的duilib的webkit浏览器控件,可以c++与js互交,源码及demo下载地址
- 使用Node.js + Mongodb构建基于Cloud Foundry的项目
- 解决使用Js赋值在浏览器上显示乱码问题
- JS检测用户使用哪种浏览器
- 在基于vue的webpack脚手架开发中使用了代理转发,结果浏览器发出的请求中不带cookie导致登录时总是session失效怎么办?
- vue.js使用watch监听路由变化的方法
- 一种基于浏览器的自动小票机打印实现方案(js版)
- JS获取当前使用的浏览器名字以及版本号
- 浏览器端图形应用开发——使用基于XML的SVG[SVG综述]
- 使用浏览器控制台写JS
- Vue.js中computed、methods、watch的使用
- JS 判断当前使用浏览器名及版本
- Acticle 23:有关浏览器中使用js跨域获取数据的描述
- 基于wke封装的duilib的webkit浏览器控件,可以c++与js互交,源码及demo下载地址
- 基于backbone.js使用localstorage来缓存请求的json数据。