Sevfuria
Sevfuria

Reputation: 56

Index out of bounds when using element.all(by.binding)

User flow: Search for a case >> Search for an item within that case >> expect returned number of results matches value

describe('Search', function () {

  beforeEach(function () {
    loginPage.signIn();
    loginPage.login(username, password);
  });

  afterEach(function () {
    homePage.logOut();
  });

  it('alecxes suggestion code', function () {
    var presenceOfAll = function(elementArrayFinder) {
      return elementArrayFinder.count().then(function (count) {
        return count > 0;
      });
    };

    homePage.searchForCase(test_case);
    filterSearchMenu.searchWithinCase(search_argument);
    var hits = element.all(by.binding('response.hits.total'));
    browser.wait(presenceOfAll(hits), TIMEOUT);

    expect(element.all(by.binding('response.hits.total')).count()).toEqual(4);
  });
});

This fails with expected 4 but was 0

describe('Search', function () {

  searchResultTotal = element(by.binding('response.hits.total'));

  beforeEach(function () {
    loginPage.signIn();
    loginPage.login(username, password);
  });

  afterEach(function () {
    homePage.logOut();
  });

  it('should be able to search', function () {
    homePage.searchForCase(test_case);
    filterSearchMenu.searchWithinCase(search_argument);
    browser.wait(EC.visibilityOf(searchResultTotal), TIMEOUT).then(function () {
      expect(element.all(by.binding('response.hits.total')).count()).toEqual(4);
    });
  });
});

This works but comes back with a warning that more then one element found for locator by.binding('response.hits.total').

describe('Search', function () {

  beforeEach(function () {
    loginPage.signIn();
    loginPage.login(username, password);
  });

  afterEach(function () {
    homePage.logOut();
  });

  it('should be able to search', function () {
    homePage.searchForCase(test_case);
    filterSearchMenu.searchWithinCase(search_argument);
    browser.wait(EC.visibilityOf(element.all(by.binding('response.hits.total')).first()), TIMEOUT).then(function () {
      expect(element.all(by.binding('response.hits.total')).count()).toEqual(4);
    });
  });
});

This Fails and throws an index out of bounds.

Second set of eyes and any help would be appreciated.

Upvotes: 1

Views: 322

Answers (2)

alecxe
alecxe

Reputation: 473873

You can also solve it with a custom Expected Condition that would check if there are more than 0 elements matching a locator found:

var presenceOfAll = function(elementArrayFinder) {
  return elementArrayFinder.count().then(function (count) {
    return count > 0;
  });
};

var hits = element.all(by.binding('response.hits.total'));
browser.wait(presenceOfAll(hits), TIMEOUT);

expect(element.all(by.binding('response.hits.total')).count()).toEqual(4);

Upvotes: 1

Brine
Brine

Reputation: 3731

I'm thinking visibilityOf doesn't like multiple elements. Try:

browser.wait(EC.visibilityOf(element.all(by.binding('response.hits.total')).first()), TIMEOUT);

Upvotes: 0

Related Questions