1

I want to compare Array values to be greater than some value.

I tried following :

this.AllElements = element.all(by.css('[style="display: none"]'));
expect(this.AllElements.getText()).toBeGreaterThan(30);

I want to verify that all values returned by this.AllElements.getText() should be greater than 30.

above expect statement fails every time even all the values are greater than 30.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
ssharma
  • 935
  • 2
  • 19
  • 43

2 Answers2

2

One straightforward option would be to use each() to check every single text in an array:

this.AllElements.each(function (elm) {
     elm.getText().then(function (text) {
         expect(parseInt(text)).toBeGreaterThan(30);
     });
});
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

You have different ways. The first one is resolving the promise returned by protractor and use standard Array methods, as 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(); });

Otherwise you could work with protractor methods like reduce but being careful to resolving promises (same considerations for the next callback to be called):

this.AllElementsText = element.all(by.css('[style="display: none"]')).getText();
let baseValue = true;
this.AllElementsText.reduce((acc, text) => baseValue && ((+text) > 30), baseValue).then((value) => { expect(value).toBe(true); next(); })

In both the cases, you can avoid the next callback returning the promises directly from the expectation blocks.

Be also careful to manage fine all the edge cases, like you don't have texts at all etc...

quirimmo
  • 9,800
  • 3
  • 30
  • 45