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
Description
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:
- Prevent / swallow the
Index out of bound
exception in the methods. - Support passing an ArrayElementFinder to the methods. E.g:
protractor.ExpectedConditions.elementToBeClickable(element.all(by.css('.myElement')))