Tree55Topz
Tree55Topz

Reputation: 1142

waitForElementsToBePresent in Protractor

So I found this little bit of code online that works pretty well for waiting for a unique identifier to be loaded on the page before you interact with it..

this.waitForElementToBePresent = function(element) {
    browser.wait(function() {
      return element.isPresent();
    }, 60000);
  };

I am new to JS and Protractor.. I was wondering how this code could be changed to wait for the presence of an element where there are multiples. I know you use $$ for the identifier when there are multiples, but how can I change this method to recognize that so I would then do something like..

utility.waitForElementsToBePresent(myElement).get(0);

Upvotes: 1

Views: 164

Answers (2)

mvndaai
mvndaai

Reputation: 3831

Rather than making a new function I would probably just last element in a group then wait for it.

var els = elements.all(by.css("#id"));
waitForElementToBePresent(els.last());

As something to remember ther is "isPresent" and "isDisplayed", present mean that an element exists on the page, visible or not. If you want to wait for it to actually show on the page, first wait for it to be present then wait for it to be displayed.

http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.last

Upvotes: 0

Gunderson
Gunderson

Reputation: 3268

Look at Alecxe's answer on this question. I've been using it for awhile and it works perfectly. Here's my slightly modified version:

// wait for X number of elements
presenceOfAll = function (elem, num, timeout) {
    var time = timeout || 5000;
    console.log('Waiting for elements ' + elem.locator() + ' to have a count of ' + num);
    return browser.wait(function () {
        return elem.count().then(function (count) {
            return count >= num;
        });
    }, time, 'Failed waiting for ' + elem.locator() + ' to have ' + num + ' total items');
};

Upvotes: 1

Related Questions