JavaScript: UI test with Nightmare, mocha, and chai

with mocha-generators plugin

require('mocha-generators').install();

var Nightmare = require('nightmare');
var chai = require('chai');
var expect = chai.expect;
var should = chai.should();

var settings = {
  url: 'http://ats-min.com/dailymagiceye',
  timeout: 50000
};

describe('Test Daily MagicEye', function() {

  var nightmare;
  this.timeout(settings.timeout);

  beforeEach(function*() {
    nightmare = Nightmare({
       // show: true,
    }).goto(settings.url);
  });

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

  it('The title should be "Daily MagicEye"', function*() {
    var title = yield nightmare.title();
    expect(title).to.equal('Daily MagicEye');
  });

  it('The refresh button should be clickable and change the words', function*() {
    var before_click = yield nightmare
      .evaluate(function () {
        els = document.querySelectorAll('[name=textRadio]');
        return Array.prototype.map.call(els, function (el) {
            return el.value;
        });
      });
    var after_click = yield nightmare
      .click('#refresh')
      .evaluate(function () {
        els = document.querySelectorAll('[name=textRadio]');
        return Array.prototype.map.call(els, function (el) {
            return el.value;
        });
      });
    expect(before_click).to.not.deep.equal(after_click);
  });
});

We have to use to.deep.equal instead of to.equal when comparing Arrays in chai.

github

Leave a Reply

Your email address will not be published. Required fields are marked *