Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript expect(数组值)。toBeGreaterThan(某些值)_Javascript_Selenium_Protractor - Fatal编程技术网

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);
     });
});