【发布时间】:2016-02-29 20:41:56
【问题描述】:
我一遍又一遍地测试了这个,试图找出一个解决方案,但不能。我已经尝试用谷歌搜索所有内容,但找不到任何产生任何结果的内容。我只使用 mocha 模块。问题是,当我运行我的测试时,它根本不会在我的函数中调用 .then 语句。我已经使用 console.log(); 测试了测试脚本之外的方法;他们按要求执行。
这是两个 cmets 的测试。
it('Should pass if the currently set frame name is top_page', function () {
chromeDriver.get('http://www.site_with_frame.com');
frameHandler.switchToFrame('top_page');
frameHandler.getCurrentFrameName(function (name) {
console.log(name); //This is never called.
done(); //Should this be here?
});
});
这是我用回调调用的函数
this.getCurrentFrameName = function(callback) {
//This is called once in test
console.log('getCurrentFrameName function ');
driver.executeScript('return self.name').then(function(name)
{
//This is never called in test
console.log('getCurrentFrameName .then ');
return callback(name);
});
};
【问题讨论】:
-
您需要在顶级函数中传递 done(即 it(..., function(done) { - 检查 mocha 文档的异步代码部分:mochajs.org - 是的,您的done 调用在正确的位置。
-
@gabdallah 我已经按照你所说的添加完成,我收到错误错误:超过 2000 毫秒的超时。确保在此测试中调用了 done() 回调。在 null.
(C:\Users\charles.sexton\WebstormProjects\JS-Selenium-Toolkit\node_modules\mocha\lib\runnable.js:215:19) -
您需要编辑此问题以显示minimal reproducible example。如果我使用您的代码,它不起作用,但失败的方式与您在问题中显示的不同。因此,为了避免人们只是猜测可能的问题,请将问题设为minimal reproducible example。
标签: javascript mocha.js