whd
whd

Reputation: 1861

Protractor click element until it's not disabled

I'm trying to write a test, my results are displayed on few pages, I can go to next page by click button, if there is no next page button is disabled, how could i achieve it with protractor? My code fails with allocation failed error

while(element(by.css('[ng-click="vm.nextPage()"]')).isEnabled())
{
element(by.css('[ng-click="vm.nextPage()"]')).click();
}

I have also tried with recursive function called on isEnabled().then but it also does not work.

Upvotes: 1

Views: 768

Answers (1)

vrachlin
vrachlin

Reputation: 817

You should use recursion here:

var nextPage = function () {
    if (element(by.css('[ng-click="vm.nextPage()"]')).isEnabled()) {
        element(by.css('[ng-click="vm.nextPage()"]')).click();
        nextPage(); // next page
    }
    else {
        return; // the element is not enabled, last page
    }
}

and just call it once in your code, if the page is angular (which i assume it is) it will be correct.

The while will not work here, same for "for", since it can't look forward, so it doesn't know how many times the while will be called, hence the allocation failed error.

Upvotes: 3

Related Questions