Protractor: Allow "more than one element found for locator" to throw instead of warn

Created on 11 Aug 2015  路  2Comments  路  Source: angular/protractor

When a selector that we expect to match exactly one element matches multiple I want that to be an error instead of a warning to ensure the test author notices it.

Can we allow that to be configurable? Here is the code from element.js:

var getWebElements = function() {
    return elementArrayFinder.getWebElements().then(function(webElements) {
      if (webElements.length === 0) {
        throw new webdriver.error.Error(
            webdriver.error.ErrorCode.NO_SUCH_ELEMENT,
            'No element found using locator: ' +
                elementArrayFinder.locator_.toString());
      } else {
        if (webElements.length > 1) {
          log.warn('more than one element found for locator ' +
              elementArrayFinder.locator_.toString() +
              ' - the first result will be used');
        }
        return [webElements[0]];
      }
    });
  };

Most helpful comment

@scriby Would you mind sharing your solution? I'd love to see this as a feature in Protractor. Having multiple elements selected and then simply using the first is asking for trouble.

All 2 comments

We ended up adding a layer between our testing code and protractor, and we implemented our own error for this case.

@scriby Would you mind sharing your solution? I'd love to see this as a feature in Protractor. Having multiple elements selected and then simply using the first is asking for trouble.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gamecheck80 picture gamecheck80  路  3Comments

mvolkmann picture mvolkmann  路  3Comments

koshkarov picture koshkarov  路  3Comments

amedvedjev picture amedvedjev  路  3Comments

vishalshivnath picture vishalshivnath  路  3Comments