发布于 2015-08-17 00:58:12 | 233 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的PhantomJS教程,程序狗速度看过来!

PhantomJS

PhantomJS 是一个基于WebKit的服务器端 JavaScript API。它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG。PhantomJS可以用于页面自动化,网络监测,网页截屏,以及无界面测试等。


今天将介绍一款自动化测试套件名叫nightmare,他 是一个基于phantomjs的测试框架,一个基于phantomjs之上为测试应用封装的一套high level API。其API以goto, refresh, click, type…等简单的常用e2e测试动作封装,使得其语义清晰,简洁。其官方在http://www.nightmarejs.org/.

如果你的项目测试不需要想需求和测试人员理解,那么基于nightmare测试或许是一个好的选择,你的降低测试代码的成本,以及测试套件的部署。我们可以选择基于jasmine-node等作为测试套件集成。

安装nightmare:

npm install nightmare

下面我们对比与远程phantomjs的对比:

原phantomjs的代码:

phantom.create(function (ph) {
  ph.createPage(function (page) {
    page.open(‘http://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(‘http://yahoo.com‘)
  .type(‘input[title="Search"]‘, ‘github nightmare‘)
  .click(‘.searchsubmit‘)
  .run();

一切显而易见,不用多说。

nightmare同时也支持插件方式抽取公用逻辑,以供复用和提高测试代码语意,如下例子:

/**
 * Login to a Swiftly account.
 *
 * @param {String} email
 * @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();


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务