我有一个简单的页面对象方法:
page.prototype.copyLink = function() {
this.visit("https://myWebsite.com");
this.login(); // once logged in, presented with list of elements
return {
firstCampaign: this.find('#el-1').getText(), //returns a promise
secondCampaign: this.find('#el-2).getText() // returns a promise
}
}
module.exports = page;
使用Jest
和chai-as-promised
我测试每个#el
中的文字是否相同:
var page = require("./page");
it("Export link icon should link to export html page", function(){
var copyLink = page.copyLink();
return copyLink.firstCampaign.should.eventually.equal(copyLink.secondCampaign);
});
出于某种原因,chai成功地从firstCampaign
中提取了文本,但没有secondCampaign
,因此我收到以下错误:
AssertionError: expected '##TEST STRING##' to equal { Object (flow_, stack_, ...) }
at getBasePromise.then.then.newArgs (node_modules/chai-as-promised/lib/chai-as-promised.js:302:22)
at ManagedPromise.invokeCallback_ (node_modules/selenium-webdriver/lib/promise.js:1384:14)
at TaskQueue.execute_ (node_modules/selenium-webdriver/lib/promise.js:3092:14)
at TaskQueue.executeNext_ (node_modules/selenium-webdriver/lib/promise.js:3075:27)
at asyncRun (node_modules/selenium-webdriver/lib/promise.js:2935:27)
at node_modules/selenium-webdriver/lib/promise.js:676:7
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
为什么chai不从第二个承诺中提取文本?
答案 0 :(得分:0)
您的代码在等同比较之前只等待第一个承诺。
您需要等待执行all promises
var copyLink = page.copyLink();
var actual;
return Promise.all([copyLink.firstCampaign, copyLink.secondCampaign]).then(results => results[0].should.equal(results[1])).should.be.fulfilled;