There are promises involved, which Protractor adds to the Control Flow queue, you cannot read and handle the for loop + break as you would usually do in a synchronous "top to bottom" code.
The common solution to this problem is to use recursion+closures, see sample solutions here:
- Asynchronous for cycle in JavaScriptAsynchronous for cycle in JavaScript
- AngularJS & Protractor - Perform .click() while an element .isDisplayed()AngularJS & Protractor - Perform .click() while an element .isDisplayed()
The reason I'm using the for loop is because I need to find a matching text in the strong tag, then click on the button in the td tag below it.
One option would be to use the by.xpath() locator:
element(by.xpath("//td[strong = '" + label + "']/following-sibling::td/button")).click();
where label is the strong value you are looking for.
Or, a more protractor-specific approach would be to use filter():
var rows = element.all(by.repeater("something"));
var filteredRow = rows.filter(function (row) {
return row.element(by.binding("x.name")).getText().then(function (name) {
return name === label;
});
}).first();
filteredRow.element(by.binding("x.button")).click();