so here is code i have forEach and im putting a lot of data in an array and then looping again and verify every $. THe goal is to make one loop `` like so
it('should be able to search rolex in ebay and every price should have $ in it', () => {
browser.url('./')
$('[class="gh-tb ui-autocomplete-input"]').click();
$('[class="gh-tb ui-autocomplete-input"]').setValue("rolex");
browser.keys("Enter");
const searchResult = []
$$('[class="s-item__title"]').forEach((element) => {
if (element.getText().length > 50) searchResult.push(element.getText().toLowerCase());
});
searchResult.every((i) => expect(i).to.have.lengthOf.above(5))
const priseOfSearch = []
$$('[class="s-item__price"]').forEach((element) => {
if (element.getText().length > 0) priseOfSearch.push(element.getText().toLowerCase());
});
priseOfSearch.every((i) => expect(i).to.contain('$'));
new verion
it('should be able to search rolex in ebay and every price should have $ in it', () => {
browser.url('./')
$('[class="gh-tb ui-autocomplete-input"]').click();
$('[class="gh-tb ui-autocomplete-input"]').setValue("rolex");
browser.keys("Enter");
$$('[class="s-item__title"]').forEach((element) => {
if (element.getText().length > 50) {
expect(element.getText().toLowerCase().to.have.lengthOf.above(5))
}
$$('[class="s-item__price"]').forEach((element) => {
if (element.getText().length > 0) {
expect(element.getText().toLowerCase().to.contain('$'));
}
});
})
})
})
It does not make sense to use .every() because that method returns a boolean and the return value is not being used in the code.
It also does not make sense to test the length of the text with expect, because the condition already confirms that all text will be longer than 5 characters (also longer than 50 characters).
In most applications it's fine to loop over an array multiple times. It does not usually impact performance in a noticeable way.
To answer your question now, if you want to iterate once it's possible by calling the expect method in the forEach loop directly instead of filtering and then iterating.
$$('[class="s-item__title"]').forEach((element) => {
if (element.getText().length > 50) {
expect(element.getText().toLowerCase()).to.have.lengthOf.above(5)
}
});