在Mocha中使用PhantomJS的问题

时间:2014-04-30 20:50:22

标签: phantomjs mocha gulp

我尝试从我的Mocha测试套件向服务器发送PhantomJS调用时遇到问题。

问题

我尝试使用PhantomJS对端点进行调用。我第一次打电话。

但我有两个问题:

  • 当前脚本第一次运行良好,但在观看某些文件时连续运行会使测试失败。我能解决这个问题吗?
  • 这似乎不是一个很好的方法来进行这些测试,还有更好的选择吗?

设定:

  • Gulp as watcher
  • gulp-mocha运行测试
  • 幻像同步做一些end2end测试

Mocha测试文件(简化):

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(简化):

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']);
});

错误:

➜  [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 个答案:

答案 0 :(得分:0)

它结束了状态是邪恶和幻影没有正确清除它的缓存&amp;饼干被问到时。