前端自动化测试解决方案探析

发表于:2016-11-22 11:32

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:极限前端    来源:51Testing软件测试网采编

分享:
  三、集成化测试解决方案
  除了模块单元的测试驱动开发,在系统功能测试阶段,我们希望自动化完成业务功能正确性的检测,此时我们就要考虑集成测试方案了。目前前端集成化测试自动化工具也有比较多。例如CasperJS、Nighmare、Nightwatch、Dalekjs,我们来逐个看下。
  · casperJS
  casperJS基于PhantomJS或SlimerJS(PhantomJS或SlimerJS都是用于web测试的自动化无界面浏览器),可以模拟完成页面内系统级的自动化操作行为测试。
  var casper = require('casper').create();
  casper.start('http://casperjs.org/');
  casper.then(function() {
  this.echo('First Page: ' + this.getTitle());
  });
  casper.thenOpen('http://phantomjs.org', function() {
  this.echo('Second Page: ' + this.getTitle());
  });
  casper.run();
  输出内容为:
  $ casperjs sample.js
  First Page: CasperJS - a navigation scripting & testing utility for PhantomJS and SlimerJS written in Javascript
  Second Page: PhantomJS | PhantomJS
  页面内的操作结合casper的操作就可以这样来实现。
var casper = require('casper').create();
var links;
function getLinks() {
// Scrape the links from top-right nav of the website
var links = document.querySelectorAll('ul.navigation li a');
return Array.prototype.map.call(links, function (e) {
return e.getAttribute('href')
});
}
// Opens casperjs homepage
casper.start('http://casperjs.org/');
casper.then(function () {
links = this.evaluate(getLinks);
});
casper.run(function () {
for(var i in links) {
console.log(links[i]);
}
casper.done();
});
  · Nightmare
  类似的,nightmare也是一个模拟还原浏览器上业务操作的强大工具,而且更易于使用。同时可以使用chrome的插件daydreem自动录制生成用户行为操作的事件序列,更加方便我们进行实际的测试。
  yield Nightmare()
  .goto('http://yahoo.com')
  .type('input[title="Search"]', 'github nightmare')
  .click('.searchsubmit');
  Nightmare也支持异步操作,并支持多种断言库,这里结合chai.js就可以这样来使用。
var Nightmare = require('nightmare');
var expect = require('chai').expect; // jshint ignore:line
describe('test yahoo search results', function() {
it('should find the nightmare github link first', function(done) {
var nightmare = Nightmare()
nightmare
.goto('http://yahoo.com')
.type('form[action*="/search"] [name=p]', 'github nightmare')
.click('form[action*="/search"] [type=submit]')
.wait('#main')
.evaluate(function () {
return document.querySelector('#main .searchCenterMiddle li a').href
})
.end()
.then(function(link) {
expect(link).to.equal('https://github.com/segmentio/nightmare');
done();
})
});
});
  · Nightwatch
  Nightwatch则可以使用node书写端对端的测试用例,并在Selenium server服务端运行测试,同样支持同步和异步。
this.demoTestGoogle = function (browser) {
browser
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', 'nightwatch')
.waitForElementVisible('button[name=btnG]', 1000)
.click('button[name=btnG]')
.pause(1000)
.assert.containsText('#main', 'The Night Watch')
.end();
};
  · Dalekjs
  DalekJS是一个跨浏览器平台的前端集成测试框架,可以自动配置启动本地的浏览器,也可以模拟填写提交表单、点击、截屏、运行单元测试等丰富的操作。
module.exports = {
'Amazon does its thing': function (test) {
test
.open('http://www.amazon.com/')
.type('#twotabsearchtextbox', 'Blues Brothers VHS')
.click('.nav-submit-input')
.waitForElement('#result_0')
.assert.text('#result_0 .newaps a span').is('The Blues Brothers')
.done();
}
};
test.open('http://adomain.com')
.click('#aquestion')
.answer('Rose')
.assert.text('#aquestion').is('Rose', 'Awesome she was!')
.done();
  小结一下,和单元测试相同的是,集成测试和单元测试类似,一般也会对测试预期输出进行断言和判断,不同的是,集成测试的输入设计和功能流程中涉及到浏览器本身的行为模拟,用以代替测试人员手动操作的过程,从而能够提高测试效率。
  四、总结与注意事项
  通过对单元测试工具和集成测试工具的概述介绍,我们基本了解了单元测试和集成测试的核心部分和特点,尽管目前主流的测试工具各不相同,但是基本的流程原理确实相同的,上面小结里面也为大家做了分析。
  当然,还有一些仍需要我们注意的问题。自动化测试不可避免地要求我们去编写测试用例,会花去一定的事件,我们在实际的项目开发过程中,决定要不要使用自动化的测试方案应该根据具体的场景来决定,如果业务规模并不复杂,而且系统功能流程清晰,则不建议使用测试用例,因为这样得不偿失;但如果业务达到一定规模,需要在原有较大项目继续维护开发的情况下,编写测试用例有利于我们较快暴露和定位问题,并极有助于后期的维护。
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号