在我的测试代码中,我希望实现以下目标:
it('Updates label text', function(done) {
page.testLabelText();
assert.equal(page.testLabelText().pageLabel, page.testLabelText().iFrameLabel);
done();
});
在我的页面对象中,这里是testLabelText();
:
page.testLabelText = function () {
var pageLabel = function () {
return driver.findElement(By.css('#element')).getText().then(function(text) {
return text;
});
};
var iFrameLabel = function () {
return driver.findElement(By.css('#element')).getText().then(function(text) {
return text;
});
};
return {
pageLabel: pageLabel(),
iFrameLabel: iFrameLabel()
};
};
但是这会返回' Undefined'当打印到控制台时...我是javascript的新手,所以虽然我已经通过常规javascript管理了这个,但我尝试过的所有内容都因selenium WebdriverJS承诺而失败...
答案 0 :(得分:2)
您的assert.equal()
正在比较两个不同的承诺对象,因此永远不会成立。为了理解原因,这里是一步一步的。你需要做的是在解决后从promises中获取值,然后比较值。
page.testLabelText();
本身只返回一个对象,因此它本身调用它而没有返回值的赋值或引用返回值什么都不做。
page.testLabelText().pageLabel
本身就是一种承诺。
page.testLabelText().iFrameLabel
本身就是一种承诺。
并且,它们是不同的承诺对象,因此您的assert.equal()
将不会成立。
如果你想比较承诺中的两个值,你必须做这样的事情:
var obj = page.testLabelText();
Promise.all(obj.pageLabel, obj.iFrameLabel).then(function(results) {
assert.equal(results[0], results[1]);
done();
});
答案 1 :(得分:1)
解决方案是使用可以解决测试中的promise的断言库,因为使用常规异步断言是不可能的。在这种情况下,我使用了Chai as Promised.
要求以下内容:
chai = require('chai'),
chaiAsPromised = require("chai-as-promised"),
should = chai.should();
并在mocha的chai.use(chaiAsPromised);
钩子中包含before
,然后我就可以写了
it('Updates label text', function() {
var label = FormsPage.testLabelText();
label.labelHeading.should.eventually.contain(label.userInput);
});
我在此here
上找到了一篇博文