7

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!

3 Answers 3

12

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.
    });
1
  • Why does protractor not just reject the error and have it end up in the .catch handler?
    – realappie
    Oct 24, 2017 at 12:34
1

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.'));
});
2
  • Thanks for the answer! For some reason though, adding the .catch() causes an error: "undefined is not a function." When I leave the catch off it works without hitches, besides obviously nothing will be 'caught'.
    – JasoonS
    Jun 17, 2015 at 15:31
  • Ok, so how I have got it to work is to use the third parameter of browser.wait, which is a string to be used as an error message. Seems I should have read the docs more carefully...Not sure why catch is not working.
    – JasoonS
    Jun 17, 2015 at 15:41
1

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();
1
  • While this works, I wonder what the expect is doing under the hood and whether this will be block the thread or not.
    – realappie
    Oct 24, 2017 at 12:37

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.