protractor custom expected condition fails with error

I'm trying to wait the browser with browser.wait with a custom ExpectedCondition like this The FunctionReturningANumber returns only a number and the numberToCheck is the number to check the number for.

var conditionFn = function () {
    return functionReturningANumber(param) === numberToCheck;
};

var condition = EC.and(conditionFn);
browser.wait(condition, 50000);

But if I execute this, I get the error: fn(...).then is not a function which basically says, that it expects an promise. I have looked up the documentation about ExpectedConditions, and the example for a custom one is like this:

// You can define your own expected condition, which is a function that
// takes no parameter and evaluates to a promise of a boolean.
var urlChanged = function() {
     return browser.getCurrentUrl().then(function(url) {
      return url === 'http://www.angularjs.org';
   });
};

And I do not see how here a promise is created. I only see, that a boolean is returned, and the documentation says evaluates to a promise of a boolean which confuses me even more.

This above is for waiting a response from an API, this is caused, because the test triggers a backend process, which protractor then needs to wait for. If there is any better way of doing this, I would greatly appreciate a better way.

I am using protractor 3.1.1.

Any help really apprectiated.

Edit:

I found a way to solve this, for some reason the logical solution by @alecxe didn't work, even if it makes sense:

var numberFound = 0;
var done = false;
var check = function () {
    numberFound = functionReturnungANumber(param);
    if (numberFound != numberToCheck) {
        setTimeout(check, 4000);
    } else {
        done = true;
    }
};
check();
return done;

If I add this to the function and retrieve the return value in the test, which calls this function, and add a browser.wait(function () { return done; }); there it works.

It's not beautiful, but for some reason, its the only thing working.... for me at least.

Answers


It's just that you don't need to wrap your Expected Condition function into EC.and:

browser.wait(conditionFn, 5000);

Try this one.

browser.wait(conditionFn () {
    return url === 'http://www.angularjs.org';
}, 8000);

Need Your Help

Global partial behaviour in Controllers

php templates symfony

I have a base controller for most of the controllers in my app like so:

UIButton inside UITableViewController

iphone objective-c uibutton uitableview

I have a UIButton that is created inside of each table cell. I want to hook up a touch event like so: