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中增加引用

在”\node_modules\nightwatch\examples\tests\nightwatch.js”中增加引用“require('../../bin/runner.js');”

2.2  运行Selenium WebDriver server(进入jar所在目录, 我的目录是D:\nodejs\node_modules\nightwatch\bin,运行命令“java -jar 2.53.1-server.jar”

2.3   运行nightwatch.js

命令行下,cd到nightwatch所在的目录(我的目录是D:\nodejs\node_modules\nightwatch),然后运行“node ./examples/tests/nightwatch.js”

我用的chrome浏览器,我将chromedriver.exe放置在目录D:\nodejs\node_modules\nightwatch\bin下, nightwatch.json配置文件如下:

{
"src_folders" : ["./examples/tests"],
"output_folder" : "./examples/reports",
"custom_commands_path" : "./examples/custom-commands",
"page_objects_path" : "./examples/pages",
"custom_assertions_path" : "",
"globals_path" : "",
"live_output" : false,
"parallel_process_delay" : 10,
"disable_colors": false,
"test_workers" : false, "selenium" : {
"start_process" : false,
"server_path" : "",
"log_path" : "",
"host" : "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "./",
"webdriver.ie.driver" : "",
"webdriver.firefox.profile" : ""
}
}, "test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_host" : "127.0.0.1",
"selenium_port" : 4444,
"silent" : true,
"disable_colors": false,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities" : {
"browserName" : "chrome",
"javascriptEnabled" : true,
"acceptSslCerts" : true
}
}, "chrome" : {
"desiredCapabilities": {
"browserName": "chrome"
}
}, "saucelabs" : {
"selenium_host" : "ondemand.saucelabs.com",
"selenium_port" : 80,
"username" : "${SAUCE_USERNAME}",
"access_key" : "${SAUCE_ACCESS_KEY}",
"use_ssl" : false,
"silent" : true,
"output" : true,
"screenshots" : {
"enabled" : false,
"on_failure" : true,
"path" : ""
},
"desiredCapabilities": {
"name" : "test-example",
"browserName": "firefox"
},
"globals" : {
"myGlobal" : "some_sauce_global"
},
"selenium" : {
"start_process" : false
}
}, "phantomjs" : {
"desiredCapabilities" : {
"browserName" : "phantomjs",
"javascriptEnabled" : true,
"acceptSslCerts" : true,
"phantomjs.binary.path" : "/path/to/phantomjs"
}
}, "browserstack" : {
"selenium" : {
"start_process" : false
},
"selenium_host" : "hub.browserstack.com",
"selenium_port" : 80,
"silent" : true,
"desiredCapabilities": {
"name" : "test-example",
"browserName": "firefox",
"browserstack.user" : "...",
"browserstack.key" : "..."
}
}, "testingbot" : {
"selenium_host" : "hub.testingbot.com",
"selenium_port" : 80,
"apiKey" : "${TB_KEY}",
"apiSecret" : "${TB_SECRET}",
"silent" : true,
"output" : true,
"screenshots" : {
"enabled" : false,
"on_failure" : true,
"path" : ""
},
"desiredCapabilities": {
"name" : "test-example",
"browserName": "firefox"
},
"selenium" : {
"start_process" : false
}
}
}
}

  

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应用自动测试的更多相关文章

  1. Nightwatch.js – 轻松实现浏览器的自动测试

    Nightwatch.js 是一个易于使用的,基于 Node.js 平台的浏览器自动化测试解决方案.它使用强大的 Selenium WebDriver API 来在 DOM 元素上执行命令和断言. 语 ...

  2. 在Autodesk应用程序商店发布基于浏览器的Web应用程序

    你一定已经听说过Autodesk应用程序商店了,通过Autodesk应用程序商店,你可以免费下载或购买来自全球的优秀开发者发布的应用程序,来帮助你更快更方便的完成你的工作.而且作为开发者,您也可以在A ...

  3. Android IOS WebRTC 音视频开发总结(七)-- 基于浏览器的开发

    前面写的一系列总结都是讲webrtc如何下载,编译,开发的,有些人可能有点云里雾里了,WEBRTC不是用来搞跨浏览器开发的吗,怎么我讲的这些跟浏览器扯不上任何关系,其实看看下面这个架构图,你就明白了, ...

  4. SlimerJS – Web开发人员可编写 JS 控制的浏览器

    SlimerJS 是一个提供给 Web 开发人员,可通过脚本编程控制的浏览器.它可以让你使用 Javascript 脚本操纵一个网页:打开一个网页,点击链接,修改的内容等,这对于做功能测试,页面自动机 ...

  5. Vis.js – 基于浏览器的动态 JavaScript 可视化库

    Vis.js 是一个动态的,基于浏览器的可视化库.该库被设计为易于使用,能处理大量的动态数据.该库由以下几部分组成:一是数据集和数据视图,基于灵活的键/值数据集,可以添加,更新和删除项目,订阅数据集变 ...

  6. Breach - HTML5 时代,基于 JS 编写的浏览器

    Breach 是一款属于 HTML5 时代的开源浏览器项目,,完全用 Javascript 编写的.免费.模块化.易于扩展.这个浏览器中的一切都是模块,Web 应用程序在其自己的进程运行.通过选择合适 ...

  7. 基于浏览器的开源“管理+开发”工具,Pivotal MySQL*Web正式上线!

    基于浏览器的开源“管理+开发”工具,Pivotal MySQL*Web正式上线! https://www.sohu.com/a/168292858_747818 https://github.com/ ...

  8. 10款基于jquery的web前端特效及源码下载

    1.jQuery时间轴插件:jQuery Timelinr 这是一款可用于展示历史和计划的时间轴插件,尤其比较适合一些网站展示发展历程.大事件等场景.该插件基于jQuery,可以滑动切换.水平和垂直滚 ...

  9. 通过Web Api 和 Angular.js 构建单页面的web 程序

    通过Web Api 和 Angular.js 构建单页面的web 程序 在传统的web 应用程序中,浏览器端通过向服务器端发送请求,然后服务器端根据这个请求发送HTML到浏览器,这个响应将会影响整个的 ...

随机推荐

  1. 九度oj 题目1365:贝多芬第九交响曲

    现在在一块空的场地上会有一个大的二维棋盘,裁判会给你指定初始位置及一座贝多芬雕像所处的位置,你开始时就站在裁判指定的初始位置处,你的目标是跳到贝多芬雕像的位置.为了给比赛增加一定的难度,你在棋盘上行走 ...

  2. iOS--app自定义相册--创建相簿,存储图片到手机

    我们在APP中点击照片,都会显示出大图,然后在大图的上面会有个保存照片的按钮,照片直接保存到了系统的相册中,但是因为公司产品的需要,让你创建和APP同名的相册保存在里面,那么就对了,可以看下具体的代码 ...

  3. Codeforces 899B Months and Years

    题目大意 给定 $n$($1\le n\le 24$)个正整数 $a_1,\dots, a_n$ 判断 $a_1$ 到 $a_n$ 是否可能为连续 $n$ 个月份的天数. 解法 由于 $n\le 24 ...

  4. 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)

    题目1 : String reorder 时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description For this question, your program ...

  5. 刷题总结——二逼平衡树(bzoj3224线段树套splay)

    题目: Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在 ...

  6. [暑假集训--数位dp]LightOJ1140 How Many Zeroes?

    Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...

  7. Java 微信公众号开发--- 接入微信

    开发微信公众号在没有正式的公众平台账号时,我们可以使用测试平台账号--- 测试平台申请地址:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandb ...

  8. JavaWeb学习总结(十二)——Session(转)

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  9. 【HDU3507】Print Article(斜率优化DP)

    单调队列DP复出练手题 朴素方程dp[i]=min(dp[j]+(s[i]-s[j-1])^2+m 你懂得 ..]of int64; a,q:array[..]of longint; n,m,i,t, ...

  10. Android Tools update proxy

    Android Tools Android SDK在线更新镜像服务器 中国科学院开源协会镜像站地址: IPV4/IPV6: http://mirrors.opencas.cn 端口:80 IPV4/I ...