2
votes

I'm having issues while trying to send PhantomJS calls to servers from my Mocha testsuite.

Problem

I'm trying to use PhantomJS to do calls against an endpoint. I have the first call working.

But I have two problems:

  • The current script runs well for the first time, but consecutive runs when watching some files make the tests fail. Do do I solve this?
  • This does not seem like a nice way to do these tests, is there a better alternative?

Setup:

  • Gulp as watcher
  • gulp-mocha to run tests
  • phantom-sync to do some end2end testing

Mocha test file (simplified):

if(typeof process != 'undefined') {
  var should = require('chai').should();
  var _ps = require('phantom-sync');
  var phantom = _ps.phantom;
  var sync = _ps.sync;
}

describe('Login', function() {
  this.timeout(5000);

  it('should be able to open CMS', function(done) {

    sync(function() {
      var ph = phantom.create();
      var page = ph.createPage();
      var status = page.open('http://www.google.com'); // Get a default CMS url...
      status.should.equal('success');
      ph.exit();
      return done();
    });

  });

  it('should redirect after successful login');

});

Gulpfile (simplified):

gulp.task('test-develop', ['scripts'], function() {
  return test(null, true);
});

var keepAlive = false;

function test(reporter, _keepAlive) {

  keepAlive = _keepAlive;

  return gulp.src('test/**/*.js')
    .pipe(plugins.plumber())
    .pipe(plugins.mocha({ reporter: reporter || 'spec' })
      .on('error', onError));

}

function onError(err) {

  console.log(err.toString());
  if (keepAlive) {
    this.emit('end');
  } else {
    // if you want to be really specific
    process.exit(1);
  }

}

gulp.task('watch-test', function() {
  gulp.watch('test/**/*.js', ['test-develop']);
});

Errors:

➜  [project_dir] git:(feature/phantomjs-tests) ✗ gulp watch-test
[gulp] Using gulpfile [project_dir]/gulpfile.js
[gulp] Starting 'watch-test'...
[gulp] Finished 'watch-test' after 26 ms
[gulp] Starting 'scripts'...
[gulp] Finished 'scripts' after 530 ms
[gulp] Starting 'test-develop'...


  Homepage
    Menu
      - should open without error
      - should close without error

  Login
    ✓ should be able to open CMS (2126ms)
    - should capture wrong username
    - should capture wrong password
    - should capture wrong username & password
    - should redirect after successful login

  ArtobjectPage
    #ArtobjectPage
      - should save $container
      - should call setupZoom
      - should call setupInfoButton
      - should call setupObjectData


  1 passing (2s)
  10 pending

[gulp] Finished 'test-develop' after 2.42 s
[gulp] Starting 'scripts'...
[gulp] Finished 'scripts' after 94 ms
[gulp] Starting 'test-develop'...


  Homepage
    Menu
      - should open without error
      - should close without error

  Login
    1) should be able to open CMS
    - should capture wrong username
    - should capture wrong password
    - should capture wrong username & password
    - should redirect after successful login

  ArtobjectPage
    #ArtobjectPage
      - should save $container
      - should call setupZoom
      - should call setupInfoButton
      - should call setupObjectData


  0 passing (2ms)
  10 pending
  1 failing

  1) Login should be able to open CMS:
     TypeError: undefined is not a function
      at sync ([project_dir]/node_modules/phantom-sync/node_modules/make-sync/lib/make-sync.js:132:10)
      at Context.<anonymous> ([project_dir]/test/e2e/login.js:13:5)
      at Test.Runnable.run ([project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runnable.js:196:15)
      at Runner.runTest ([project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runner.js:374:10)
      at [project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runner.js:452:12
      at next ([project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runner.js:299:14)
      at [project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runner.js:309:7
      at next ([project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runner.js:247:23)
      at Object._onImmediate ([project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runner.js:276:5)
      at processImmediate [as _immediateCallback] (timers.js:330:15)



[gulp] Error in plugin 'gulp-mocha': 1 test failed.
[gulp] Finished 'test-develop' after 85 ms
1

1 Answers

0
votes

It ended up state being evil and Phantom not properly clearing it's cache & cookies when asked to.