【问题标题】:Problems using PhantomJS inside Mocha在 Mocha 中使用 PhantomJS 的问题
【发布时间】:2014-06-17 08:15:43
【问题描述】:

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

问题

我正在尝试使用 PhantomJS 对端点进行调用。我的第一个电话正常工作。

但是我有两个问题:

  • 当前脚本第一次运行良好,但连续运行看一些文件导致测试失败。我要解决这个问题吗?
  • 这似乎不是进行这些测试的好方法,有更好的替代方法吗?

设置:

  • Gulp 作为观察者
  • gulp-mocha 运行测试
  • phantom-sync 做一些端到端测试

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

【问题讨论】:

    标签: phantomjs mocha.js gulp


    【解决方案1】:

    它最终状态为邪恶,Phantom 在被要求时没有正确清除它的缓存和 cookie。

    【讨论】:

      猜你喜欢
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 2014-08-17
      • 1970-01-01
      相关资源
      最近更新 更多