protractor icon indicating copy to clipboard operation
protractor copied to clipboard

How to wait to load the element in browser

Open prakashomp opened this issue 12 years ago • 5 comments

hi i created a common function to wait to load page but not working the code.

this.waitForElements=function(element){
for (var sec = 0;; sec++) {
if(sec>120){ break; } if(this.checkingElement(element)==true){ break; } browser.sleep(1000);
} };

    this.checkingElement=function(availableElement){        
        val=false;
        availableElement.isPresent().then(function(){
            val=true;
        });
    return val;
    };

when i call 'waitForElements(pageobject element)' i am getting error while waiting for Protractor to sync with the page how to control this one. script was executing with out wait .

Thanks& Regards OmPrakash

prakashomp avatar Jul 08 '14 14:07 prakashomp

As Julie usually suggest, please use github markdown to make code easier to read: https://github.github.com/github-flavored-markdown/

This is a common error that happens on non-angular pages, has your page AngularJS?

error while waiting for Protractor to sync with the page

If not an angular page then add browser.ignoreSynchronization = true; at the beginning of your test case.

I would never name element my stuff since there is already element on the global scope.

Regarding your 120 seconds wait mechanism, you'll be better off by using browser.driver.wait() the example usage below supports retrying on stale elements and outputs warnings on each retry:

var i = 0;
var _retryOnErr = function(err) {
    console.log(' <<< warning: wait retrying iteration: ' + i + ' >>> ');
    browser.sleep(500);
    return false;
};

browser.driver.wait(function() {
    i++;
    return someElmFinder.isPresent().then(function(present) {
        if (present) {
            return true;
        } else {
            return _retryOnErr();
        }
    }, _retryOnErr);
}, 120*1000, 'Error waiting for element present: ').
then(function(waitRetValue) {
    return waitRetValue; // usually just `true`
}, function(err) {
    throw err + ' after ' + i + ' iterations.';
});

elgalu avatar Jul 08 '14 15:07 elgalu

Thanks for the detailed answer, @elgalu

Please post questions like this on Stack Overflow so that we can keep the issue tracker clean for bugs and feature requests. Thanks!

juliemr avatar Jul 08 '14 17:07 juliemr

Thank you for your replay ,@elgalu

prakashomp avatar Jul 10 '14 05:07 prakashomp

I think a more efficient way to implement a waitForElement method would be (as mentioned in https://github.com/angular/protractor/issues/202#issuecomment-39857641):

var waitForElement = function(selector, waitFor) {
  waitFor = waitFor || 5000;
  browser.driver.manage().timeouts().implicitlyWait(waitFor);
  browser.driver.findElement(by.css(selector));
  browser.driver.manage().timeouts().implicitlyWait(0);
}

//in your tests
//waits up to 5 seconds for the element to be present in the DOM
waitForElement(someSelector);
//...
//waits up to 10 seconds for the element to be present in the DOM
waitForElement(anotherSelector, 10000);

If I am correct in how implicit wait works in the JSON Wire Protocol, this way will only wait to maximum amount of time needed and only makes 1 call to find the element.

ryanzec avatar Jul 24 '14 00:07 ryanzec

But here it will wait until the wait time expires, i think it is not a good way. Any other way to wait for the locator upto the presence or availability of that ?

gijop avatar Mar 09 '18 10:03 gijop