3

I try to use wait() method instead sleep(), but it isn't working. I had code:

 browser.actions().click(filter_field).perform();
 browser.sleep(3000);
 if (baloon_info.isPresent()) { //some expections }
 else { expect(true).toBe(false); }

Now I want to do something like:

 var present_pri = browser.wait(function () {
   return balloon_info.isPresent();
 }, 3000);
 if (present_pri) { //some expections }
 else { expect(true).toBe(false); }

But if balloon isn't present I have the error message: Wait timed out after 3117ms instead expected true to be false (present_pri == false)

I tryed to write:

var EC = protractor.ExpectedConditions;
browser.wait(EC.presenceOf(balloon_warning), 3000);
expect(balloon_warning.isPresent()).toBeTruthy();

But I always have the same error. What I doing wrong?

  • 1
    The first approach will always fail because the browser.wait is returning a promise and not a boolean as you are expecting.(Read API docs for more info) – Rahul Vig Aug 05 '15 at 13:45

3 Answers3

5

You need to handle the wait timeout error:

browser.wait(EC.presenceOf(balloon_warning), 3000).then(function () {
    // success handler
}, function (error) {
    expect(true).toBe(false);
});
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
3

Finally I found one more solution:

browser.wait(function () {
   return balloon_info.isPresent();
}, 3000).then(function () {
   // success handler
}).thenCatch(function () {
   expect(true).toBe(false);
});
3

As per your question, what i understand is that you are trying to find if an element is present in the DOM (however, it doesn't necessarily mean that it should be displayed). You are getting a wait error because you are waiting for an element that is not present in the DOM. So it throws an error as you have shown above. To resolve it, try to expect the presence of the element without waiting for it. Because by default protractor has a predefined wait time out for checking for the presence of an element in DOM. Here's a small snippet -

it('Check for presence of the element', function(){
    expect(balloon_warning.isPresent()).toBe(true);
}, 60000); //extra timeout of 60000 so that async error doesn't show up

Now if you want to use wait at any cost then checkout below example -

it('Check for element with wait time of 3000 ms', function(){
    var EC = protractor.ExpectedConditions;
    browser.wait(EC.presenceOf(balloon_warning), 3000).then(function(){
        expect(balloon_warning.isPresent()).toBeTruthy();
    },function(err){
        console.log('error');
    });
}, 60000);

Here if element is not found then wait function will throw error and gets printed in console. Hope it helps.

giri-sh
  • 6,934
  • 2
  • 25
  • 50