Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

ExpectedCondition methods should support element.all().first() #2314

Closed
@marklagendijk

Description

@marklagendijk

The ExpectedConditions methods are very nice when testing stuff outside of Angular. However, there is a limitation. Currently it is impossible to wait for the first element matching a certain selector.

I would expect the following to work:

function waitForElementByLocator(locator, timeout){
    var elem = element.all(locator).first();
    var isClickable = protractor.ExpectedConditions.elementToBeClickable(elem);
    browser.wait(isClickable, timeout, 'Waiting for element  \'' + locator + '\' timed out');
    return elem;
}

// There are multiple .myElement elements, we want to click the first one
waitForElementByLocator(by.css('.myElement')).click();

However, this fails if .myElement is not present at the moment the wait is started. We get an error:

Failed: Index out of bound. Trying to access element at index: 0, but there are only 0 elements that match locator LOCATOR

It does work when we use element(locator) instead of element.all(locator).first():

function waitForElementByLocator(locator, timeout){
    var elem = element(locator);
    var isClickable = protractor.ExpectedConditions.elementToBeClickable(elem);
    browser.wait(isClickable, timeout, 'Waiting for element  \'' + locator + '\' timed out');
    return elem;
}

// There are multiple .myElement elements, we want to click the first one
waitForElementByLocator(by.css('.myElement')).click();

However, when we do this, we get warnings about the fact that there are multiple matches:

WARNING - more than one element found for locator LOCATOR - the first result will be used.

I see two possible solutions for this:

  1. Prevent / swallow the Index out of bound exception in the methods.
  2. Support passing an ArrayElementFinder to the methods. E.g: protractor.ExpectedConditions.elementToBeClickable(element.all(by.css('.myElement')))

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions