问题描述
我将Nightwatch.js与Typescript一起使用。在测试中,首先,我需要从页面上的表中的列中读取一些值。然后,我需要遍历刚得到的值并使用一个值应用过滤器,因此该表仅显示包含该过滤器使用的值的行。我决定将这些值作为一个集合进行收集,以避免重复,并将该集合作为Promise兑现:
getNumbers(browser: Nightwatchbrowser) {
return new Promise<Set<string>>(function (resolve,reject) {
browser
.useXpath()
.waitForElementPresent('//div[contains(text(),"Num")]')
.useCss()
.elements('xpath','//div[contains(text(),"Num")]',function (result) {
const numSet: Set<string> = new Set();
Object(result.value).forEach(item => {
browser.elementIdText(item.ELEMENT,function (result) {
numSet.add(String(result.value).replace(/\D/g,''));
})
});
resolve(numSet);
});
});
}
那很好。我调试了它,发现该集合包含数据。然后,我尝试循环访问该集合:
'Validate the filter applies correctly.': browser => {
myPage.getNumbers(browser)
.then(function (numSet) {
// debug output -- prints at the very beginning "Set: Set {}"
console.log('Set: ',numSet);
// another debug output -- prints "Set { '2000','0007' }" later,after getNumbers() is executed
browser.perform(done => {
console.log(numSet);
done();
});
for (const digits of numSet) {
log(`Applying filter for: ${digits}`); // -- never printed
myPage
.click('@FILTER_BTN`enter code here`')
.apply(digits)
};
});
});
},
forEach或for循环始终退出,因为在测试执行的那个阶段,该集合为空。因此,我的问题是如何修复代码,以便可以迭代Promise返回的非空集?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)