脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-Nightmare基于phantomjs的自动化测试套件,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 今天将介绍一款自动化测试套件名叫nightmare,他是一个基于phantomjs的测试框架,一个基于phantomjs之上为测试应用封装的一套high level API。其API以goto, refresh, click, tyPE…等简单的常用e2e测试动作封装,使得其语义清晰,简洁。其官方在https://www.nightmarejs.org/.
如果你的项目测试不需要想需求和测试人员理解,那么基于nightmare测试或许是一个好的选择,你的降低测试代码的成本,以及测试套件的部署。我们可以选择基于jasmine-node等作为测试套件集成。
安装nightmare:
1
npm install nightmare
下面我们对比与远程phantomjs的对比:
原phantomjs的代码:
phantom.create(function (ph) {
ph.createPage(function (page) {
page.open('https://yahoo.COM', function (status) {
page.evaluate(function () {
VAR el =
document.querySelector('input[tITle="SeArch"]');
el.value = 'github nightmare';
}, function (result) {
page.evaluate(function () {
var el = document.querySelector('.searchsubmit');
var event = document.createEvent(';mouseEvent');
event.initEvent('click', true, false);
el.dispatchEvent(event);
}, function (result) {
ph.exit();
});
});
});
});
});
nightmare代码:
new Nightmare()
.goto('https://yahoo.com')
.type('input[title="Search"]', 'github nightmare')
.click('.searchsubmit')
.run();
一切显而易见,不用多说。
nightmare同时也支持插件方式抽取公用逻辑,以供复用和提高测试代码语意,如下例子:
/**
* Login to a Swiftly account.
*
* @param {String} password
*/
exports.LOGin = function(email, password){
return function(nightmare) {
nightmare
.viewport(800, 1600)
.goto('https://swiftly.com/login')
.type('#username', email)
.type('#password', password)
.click('.button--Primary')
.wait();
};
};
使用代码很简单:
var Swiftly = require('nightmare-swiftly');
new Nightmare()
.use(Swiftly.login(email, password))
.use(Swiftly.task(instructions, uploads, path))
.run();
觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的javascript代码实例教程-Nightmare基于phantomjs的自动化测试套件全部内容,希望文章能够帮你解决javascript代码实例教程-Nightmare基于phantomjs的自动化测试套件所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。