What does Protractor browser.wait return if the condition never becomes true?

13,786

Solution 1

There are two ways to do this: First you can use the third argument of browser.wait to a string that will be sent as an error message. Like so:

browser.driver.wait(function(){
    return //condition
}, timeout, 'Error message string')
    .then(function(){
        callback();
    });

Or secondly using a second argument to the .then like this:

browser.driver.wait(function(){
    return //condition
}, timeout)
    .then(function(){
        callback();
    }, function(){
        //code to want to execute on failure.
    });

Solution 2

The wait function rejects on timeout. That is - it returns a promise and when the timeout is reached it rejects it.

browser.driver.wait(function(){
    return pgTransactionHistory.transactionHistoryDetails.isPresent();
}, 60000).then(function(){
    callback(); // instead of doing this, you should return a promise
}).catch(function(){
    callback(new Error('The details button was not present.'));
});

Solution 3

Because Protractor gives the capability of writing asynchronous operations synchronous, it is also possible to handle the browser.wait timeout in a more synchronous way.

export class AppPage {

  public static isLoaded() {
    return browser
     .wait(until.presenceOf(by.id('myID')), 5000)
     .then(() => true, () => false);
  }
}

And use it in your e2e.spec as follows:

expect(AppPage.isLoaded()).toBeTruthy();
Share:
13,786
JasoonS
Author by

JasoonS

Blockchain Engineer from South Africa

Updated on June 19, 2022

Comments

  • JasoonS
    JasoonS almost 2 years

    I want to use the browser.wait function to repeatedly check if a button element is present for a certain amount of time then use the relevant callback. Below I have the code that doesn't use the wait.

    detailsButton.isPresent()
        .then(function(present){
            if(!present) {
                callback();
            } else {
                 callback(new Error('The details button was not present.'));
            }
        });
    

    I would like some help fixing this code, as I am not sure how the wait function deals with a falure/timeout. Essentially I am asking what should be in the '.then' part of the below code that is less clunky that what I have currently.

    browser.driver.wait(function(){
        return pgTransactionHistory.transactionHistoryDetails.isPresent();
    }, 60000).then(function(){
        pgTransactionHistory.transactionHistoryDetails.isPresent()
            .then(function(present){
                if(!present) {
                    callback();
                } else {
                    callback(new Error('The details button was not present.'));
                }
            });
    });
    

    Thank you!