Reputation: 309
In non-Angular page using Protractor I have a situation where I have to loop trough some list of elements on the page and once I find the element I'm looking for, I have to stop the loop and click on that element.
Page object file:
var homePage = function() {
this.elementLists = browser.element(by.id('list')).all(by.css('div.list'));
this.elementLink = browser.element(by.id('list')).element(by.css('div[data-code="GAA"]'));
this.paginationRight = browser.element(by.css('.navi-right'));
...
}
module.exports = homePage;
Spec file:
// page objects
var homePage = require('../home_page/home_page.js');
// variables
var maxLoopCount = '';
var i = 0;
describe(...
it(...
// initialize page object
var home = new homePage();
// store number of listsinto a variable
home.elementLists.count().then(function(num) {
maxLoopCount = num;
// click on right paginator arrow until element is displayed in the list
while (i < maxLoopCount) {
home.paginationRight.click();
browser.sleep(1500);
if (expect(home.elementLink.isPresent()).toBeTruthy()) {
break;
}
i++;
};
});
});
I know I should use if (elementFound === true)
, but how can I do that?
Upvotes: 1
Views: 4390
Reputation: 283
Just use:
browser.wait(protractor.ExpectedConditions.visibilityOf($('.page-list-content')), 6000);
Upvotes: 0
Reputation: 941
you can use 'isDisplayed() or isPresent()' according to your need, something like this:
home.elementLists.each(function(elem){
home.paginationRight.click();
home.elementLink.isDisplayed().then(function(value){
if(value === true){
break;
}else {
console.log("element not visible");
}
});
});
Upvotes: 0
Reputation: 363
I'd suggest something a little more recursive.
protractor.expectedCondition is also a gold mind when waiting for things to happen
// page objects
var homePage = require('../home_page/home_page.js');
// variables
var maxLoopCount = '';
var i = 0;
describe(...
it(...
// initialize page object
var home = new homePage();
// store number of listsinto a variable
home.elementLists.count().then(function(num) {
maxLoopCount = num;
// click on right paginator arrow until element is displayed in the list
nextPageAndCheck(1, maxLoopCount);
});
});
nextPageAndCheck = function(page, pageCount) {
if(page <= pageCount) {
home.paginationRight.click();
browser.wait(protractor.expectedCondition.visibilityOf(home.elementLink),1500, "not on this page").then(
function(passed){return passed},
function(error) {
nextPageAndCheck(page++, pageCount);
})
} else {
throw("couldn't find it error")
}
}
Upvotes: 1