Search code examples
webdriverprotractorwait

Protractor browser wait for boolean Condition


I'm trying to create a wait condition that will execute a script and based on the return value will will determined if it need to wait or not. I'm using protractors executeScript functionality and browser wait:

this.activeConnections = function(jsl) {   
console.log("Inside Active Connections");
switch (jsl) {
case checkEnum.jQuery:
console.log("Jquery Enum");
return browser.executeScript("return jQuery.active;").then(function(count) {
console.log("The count is "+count);
return count == 0;
});
default:
browser.logger.info("No asynchronous check performed.");
break;
}   
};

I was expecting the wait condition to wait until the Executed script would evaluate to true but that is not working

this.waitForActiveConnections = function () {
console.log("Inside Wait for Active Connections");
var condition = until.and(this.activeConnections(checkEnum.jQuery),false);
console.log("Whats this condition "+ condition);
return browser.wait(condition,30000);
};

Solution

  • The main issue is that your custom Expected Condition needs to return an executable - a function that browser.wait() is going to execute continuously. Something like:

    this.activeConnections = function(jsl) {   
        return function () {
            switch (jsl) {
                case checkEnum.jQuery:
                    return browser.executeScript("return jQuery.active;").then(function(count) {
                        return count == 0;
                 });
                default:
                    browser.logger.info("No asynchronous check performed.");
                    return true;
                    break;
            }   
        }
    }