LoginSignup
25
24

More than 5 years have passed since last update.

Nightmare2・ES6によるWebGUIテストを始めよう

Last updated at Posted at 2015-09-19

まえがき

今回はNightmareのVersion2で実行するまでに勘違いがあったり、ハマったりしたので簡単なサンプルを動かすまでのメモを残しておこうと思います。

Nightmareとは

JavaScriptのスクレイピングライブラリ
最近Version2と一新され、いままでPhantom.jsベースで動いていたものがElectronベースで動くので高速になっていたり、
ES6でAPIを叩くようになったり、そもそもAPIの仕様がガラッと変わったりしている。
また、CasperJS・Phantom.jsと比べてもAPIがシンプルである。

必要なライブラリのinstall

npm install --save espower-babel
npm install --save co-mocha
npm install --save nightmare
npm install --save power-assert

簡単なサンプルテストプロジェクト作成

test/mocha.opts
--reporter spec
--recursive
--growl
--timeout 30000
--require co-mocha
test/sample.js

var Nightmare = require('Nightmare');
var assert = require('power-assert');
require('babel/register');

describe('Nightmare2を使ってみる', function () {

  var driver;

  beforeEach(function() {
    driver = Nightmare();
  });

  afterEach(function*() {
    yield driver.end();
  });

  it('yahooさんのタイトルを取得することができる', function*() {
    var title = yield driver
      .goto('http://yahoo.com')
      .title();

    assert(title === 'Yahoo');
  });

  it('qiitaさんのタイトルを取得することができる', function*() {
    var title = yield driver
      .goto('http://qiita.com')
      .title();

    assert(title === 'Qiita - A technical knowledge sharing platform for programmers.');
  });

  it('qiitaのorganizationに登録されている企業数はNである', function*() {
    var items = yield driver
      .goto('http://qiita.com/organizations')
      .evaluate(function(){
        return $('.organizationsList_item');
      });

    assert(171 === items.length);
  });
});

テストの実行

./node_modules/.bin/mocha --compilers js:espower-babel/guess test/


  Nightmare2を使ってみる
    ✓ yahooさんのタイトルを取得することができる (4908ms)
    ✓ qiitaさんのタイトルを取得することができる (2836ms)
    ✓ qiitaのorganizationに登録されている企業数はNである (2934ms)


  3 passing (11s)

感想

・yield便利すぎて怖い
・セレクタ関係のAPIを使って要素をつかむよりevaluateしてjQueryでつかむ方が個人的には楽でいいなぁと

25
24
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
24