Javascript expect(数组值)。toBeGreaterThan(某些值)
我想比较数组值是否大于某个值 我尝试了以下几点:Javascript expect(数组值)。toBeGreaterThan(某些值),javascript,selenium,protractor,Javascript,Selenium,Protractor,我想比较数组值是否大于某个值 我尝试了以下几点: this.AllElements = element.all(by.css('[style="display: none"]')); expect(this.AllElements.getText()).toBeGreaterThan(30); 我想验证this.allegements.getText()返回的所有值都应该大于30 即使所有值都大于30,上述expect语句每次都会失败 你有不同的方法。第一种方法是解析量角器返回的承诺,并使用标
this.AllElements = element.all(by.css('[style="display: none"]'));
expect(this.AllElements.getText()).toBeGreaterThan(30);
我想验证this.allegements.getText()
返回的所有值都应该大于30
即使所有值都大于30,上述expect语句每次都会失败 你有不同的方法。第一种方法是解析量角器返回的承诺,并使用标准的
Array
方法,如every
:
// in this case you need to pass next callback in the block and call it after resolving the method for keeping the sync properly and communicate when the
expectation is finished after resolving the promises
this.AllElementsText = element.all(by.css('[style="display: none"]')).getText();
this.AllElementsText.then((texts) => texts.every((text) => (+text) > 30)).then((value) => { expect(value).toBe(true); next(); });
否则,您可以使用诸如reduce
之类的量角器方法,但要小心解析承诺(与调用下一个回调时的注意事项相同):
在这两种情况下,您都可以避免直接从期望块返回承诺的next
回调
还要小心管理所有边缘情况,例如根本没有文本等。一个简单的选项是检查数组中的每个文本:
this.AllElements.each(function (elm) {
elm.getText().then(function (text) {
expect(parseInt(text)).toBeGreaterThan(30);
});
});
对于一些getText()函数,我得到了一个错误:期望NaN大于30。如何避免这种情况?@ssharma您希望这些NaN
值导致测试失败还是跳过它们?跳过它们。@ssharma然后您可以在调用expect()
之前应用。希望这会有帮助。谢谢@alecxe,这很有帮助。
this.AllElements.each(function (elm) {
elm.getText().then(function (text) {
expect(parseInt(text)).toBeGreaterThan(30);
});
});