For protractor test, can we get waitElement object value without browser.wait() function?

387 Views Asked by At

I am newbie with protractor (and async javascript) but my last few weeks of learning I really like it.

Since we deal with objects, I was trying to come up with a function which returns boolean value if the object is visible/exist.

A partial implementation would like this:

function waitObject(elm, timeout) {
    return browser.driver.wait(function() {
        return elm.isPresent().then(function(res) {
            return res;
        });
    }, timeout);
}

I'd like to achieve 2 things here:

1) It return true/false after the timeout.

2) When it's false, it doesn't throw the timeout error but just false. So, I can continue with my remaining test.

1

There are 1 best solutions below

1
On BEST ANSWER

Just use then as browser.wait returns a promise which resolves or rejects depending on the condition passed to browser.wait: http://angular.github.io/protractor/#/api?view=webdriver.WebDriver.prototype.wait

waitObject(element, 1000).then(function () {
    // The condition resolved truthy, element is present
}, function () {
    // Timed out
});

This way you can do different things depending on if the condition timed out or not.

I think it would be tricky though to not return a promise from the function. As everything done in Protractor happens async.