Protractor: wait method doesn't work

I am trying to use wait () method instead of sleep (), but it doesn't work. I had the 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 the balloon is missing, I have an error: Wait timed out after 3117ms

instead of expected true to be false

(present_pri == false)

I tried 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 am I doing wrong?

+3


source to share


3 answers


You need to handle the wait wait error:



browser.wait(EC.presenceOf(balloon_warning), 3000).then(function () {
    // success handler
}, function (error) {
    expect(true).toBe(false);
});

      

+5


source


Finally I found another solution:



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

      

+3


source


As per your question, I understand that you are trying to find if an element is present in the DOM (however that does not necessarily mean that it should be displayed). You are getting an expectation error because you are expecting an element that is not in the DOM. Therefore it throws an error as you showed above. To fix this problem, try waiting for an item instead of waiting for it. Because by default protractor has a predefined timeout to check if an element is in the 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, see 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 the item is not found, the wait function will throw an error and print to the console. Hope it helps.

+3


source







All Articles