NightWatch 自动化测试框架
Table of Contents TOC
安装
安装 nodejs
访问nodjejs 官网下载安装文件 http://nodejs.org/
安装nightwatch 例子
git clone https://github.com/bisone/easy-web-test.git
使用
cd easy-web-test node nightwatch.js -t baidu.js
执行上面的命令,就可以打开firefox访问百度。如果想使用IE或者Chrome来运行测试,需要下载相应的driver。具体步骤可以参考官方文档。
具体的API可以参照http://nightwatchjs.org/api。
示例
这里提供一个订会议室的例子
config.json
{
"userName":"username",
"passwd":"TODO",
"roomId":"20",
"startTime":"09:30",
"endTime":"11:30"
}
meeting.js
config = require("./config");
module.exports = {
'Book meetingroom' : function (client) {
client
.url('http://mrbs.dev.com/meetingRoom/')
.waitForElementVisible('body', 1000)
.maximizeWindow()
.setValue('input#Name', config.userName)
.setValue('input#Password', config.passwd)
.click('#Logon')
.waitForElementVisible('body', 1000)
.click('#dateId2')
.setValue('#roomName',config.meetingRoomName)
.click('#search')
.waitForElementVisible('body', 1000)
.click("ul.list li:nth-child(1) a")
.setValue('#startTime',config.startTime)
.setValue('#endTime',config.endTime)
.setValue('#titleId','Scrum Meeting')
//.end();
;
}
};
config.json里配置了登录系统的用户名和密码,会议室时间等信息。meeting.js里通过require(./config)获得配置信息,然后访问网站系统,自动 输入相关信息完成会议预定。
总结
本文作为入门文章,给大家介绍了如何使用nightwatch。写脚本的核心问题,是要理清测试流程和如何使用选择器选择页面元素。
附录(详细安装过程)
详细步骤这个页面里写的已经很详细了: http://nightwatchjs.org/guide。
下面是我在机器上的安装步骤。我已经安装了nodejs,所以我只需要使用如下命令安装nightwatch
$ npm install nightwatch
在国内不能直接访问http://selenium-release.storage.googleapis.com/index.html 下载selenium-server-standalone-{VERSION}.jar。我直接从 github上拿到的源码编译的selenium server.
git clone https://github.com/SeleniumHQ/selenium.git cd selenium ./go release
执行完上面的命令后,会在build/dist目录下生成 selenuim-server-standalone-2.44.jar.