【问题标题】:My nightmare test isnt getting into my evaluate statement我的噩梦测试没有进入我的评估声明
【发布时间】:2016-08-11 16:37:41
【问题描述】:

练习使用 mocha chai 和 nightmare 运行测试。在我进入评估块之前,一切似乎都有效。

var Nightmare = require('nightmare'),
  should = require('chai').should()

describe('Frontend Masters', function() {
  this.timeout(20000);

  it('should show form when loaded', function(done) {
    var nightmare = new Nightmare({show: true})
    nightmare
      .goto('https://frontendmasters.com/')
      .wait('a[href*="https://frontendmasters.com/login/"]')
      .click('a[href*="https://frontendmasters.com/login/"]')
      .wait('#rcp_login_form')
      .evaluate(function() {
        return window.document.title;
      }, function(result) {
        result.should.equal('Login to Frontend Masters');
        done();
      })
      .run(function(){
        console.log('done')
      });
  });
});

我已经输入了控制台日志,但它从未进入评估。我尝试将几个选择器传递给我的 wait() 函数,但它似乎没有效果。我收到的唯一错误是我的超时已超出。但不管我设置多久

【问题讨论】:

  • 好像和evaluate没什么关系。如果wait 超时失败,则可能是上一次单击无效或站点已损坏。顺便问一下,wait 的两个调用中哪个失败了?
  • 使用环境变量 DEBUG="nightmare*" 运行时,您是否得到任何有用的输出?

标签: node.js testing mocha.js chai nightmare


【解决方案1】:

你使用的是什么版本的 Nightmare?

.evaluate() 的签名已更改,我认为这可能是您的问题的根源。您传入的第二个函数 - 曾经用于处理评估结果的函数 - 实际上作为参数传递给第一个 .evaluate() 参数。换句话说,第二个参数永远不会运行,done() 永远不会被调用,并且您的测试将超时。

另外值得一提的是:.run() is not directly supported。请改用.then()

最后,让我们修改您的源以反映上述内容以帮助您开始:

var Nightmare = require('nightmare'),
  should = require('chai')
  .should()

describe('Frontend Masters', function() {
  this.timeout(20000);

  it('should show form when loaded', function(done) {
    var nightmare = new Nightmare({
      show: true
    })
    nightmare
      .goto('https://frontendmasters.com/')
      .wait('a[href*="https://frontendmasters.com/login/"]')
      .click('a[href*="https://frontendmasters.com/login/"]')
      .wait('#rcp_login_form')
      .evaluate(function() {
        return window.document.title;
      })
      .then(function(result) {
        result.should.equal('Login to Frontend Masters');
        console.log('done')
        done();
      })
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    相关资源
    最近更新 更多