Protractor: wait method isn't work

怎甘沉沦 提交于 2019-12-12 07:19:40

问题


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:


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);
});



回答2:


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.



来源:https://stackoverflow.com/questions/31828167/protractor-wait-method-isnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!