如何制作"它"在摩卡等待,直到#34;它"得到解决?

时间:2018-01-02 10:10:06

标签: javascript node.js selenium asynchronous mocha

var promise = require('promise');
var {Builder, By, Key, until} = require('selenium-webdriver');
var test = require('selenium-webdriver/testing');
var chai = require('chai');
var getUrl = require('./wdio.conf.js');
var driver = new Builder().forBrowser('chrome').build();

test.describe('Proper Testing', function() {
    test.it('should prompt the server from user', function() {
        return new promise(function(resolve,reject){
            resolve(driver.get("https://www.google.co.in"));
            reject(err);
        })
    })
}) 

对于上面给出的代码,运行mocha proper.js会在chrome浏览器中打开给定的url,但测试失败并显示超时错误。 我已经读过,如果test返回一个promise,则不需要调用done()。 给定代码中出了什么问题?

2 个答案:

答案 0 :(得分:0)

你必须在完成测试后致电done

我不确定你要在这里实现什么,但这个示例代码应该说明我的观点:

describe('Proper Testing', function() {

    it('should prompt the server from user', function(done) {
        return getUrl().then(function(url){
            driver.get(url)
            .then(function () {
              done();
            }).catch(function (err) {
              done(err);
            });
        })
    });

})

参考:https://mochajs.org/#asynchronous-code

答案 1 :(得分:0)

关于您的问题,请尝试添加设置高超时的before语句。普通测试有一个默认超时(我认为它是2秒),如果测试超过这个时间,它会引发一个错误并且它没有通过测试。

before(function (done) {
    this.timeout(5000);
    done();
})

关于完成:如果您要返回承诺,则不需要使用完成。简单地声明没有任何参数的function(),如果它没有引发任何异常并且它完成所有断言,它会发现它通过了测试。

describe('Proper Testing', function() {

    it('should prompt the server from user', function() {
        return getUrl().then(function(url){
            driver.get(url);
        })
    });

})