Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 3dc6e0d

Browse files
authored
Merge pull request #494 from qheaden/visibility-multiple-elements
Add condition to check whether visibility of any of elements was located
2 parents d59d2c1 + 4eee996 commit 3dc6e0d

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This project versioning adheres to [Semantic Versioning](http://semver.org/).
55
### Changed
66
- Drop PHP 5.5 support, the minimal required version of PHP is now PHP 5.6.
77

8+
### Added
9+
- Added a visibilityOfAnyElementsLocated method to WebDriverExpectedCondition.
10+
811
## 1.4.1 - 2017-04-28
912
### Fixed
1013
- Do not throw notice `Constant CURLOPT_CONNECTTIMEOUT_MS already defined`.

lib/WebDriverExpectedCondition.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,35 @@ function (WebDriver $driver) use ($by) {
193193
);
194194
}
195195

196+
/**
197+
* An expectation for checking than at least one element in an array of elements is present on the
198+
* DOM of a page and visible.
199+
* Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
200+
*
201+
* @param WebDriverBy $by The located used to find the element.
202+
* @return WebDriverExpectedCondition<WebDriverElement> Condition returns the elements that are located and visible.
203+
*/
204+
public static function visibilityOfAnyElementLocated(WebDriverBy $by)
205+
{
206+
return new static(
207+
function (WebDriver $driver) use ($by) {
208+
$elements = $driver->findElements($by);
209+
$visibleElements = [];
210+
211+
foreach ($elements as $element) {
212+
try {
213+
if ($element->isDisplayed()) {
214+
$visibleElements[] = $element;
215+
}
216+
} catch (StateElementReferenceException $e) {
217+
}
218+
}
219+
220+
return count($visibleElements) > 0 ? $visibleElements : null;
221+
}
222+
);
223+
}
224+
196225
/**
197226
* An expectation for checking that an element, known to be present on the DOM of a page, is visible.
198227
* Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

tests/unit/WebDriverExpectedConditionTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,36 @@ public function testShouldDetectVisibilityOfElementLocatedCondition()
185185
$this->assertSame($element, $this->wait->until($condition));
186186
}
187187

188+
public function testShouldDetectVisibilityOfAnyElementLocated()
189+
{
190+
$elementList = [
191+
$this->createRemoteWebElementMock(),
192+
$this->createRemoteWebElementMock(),
193+
$this->createRemoteWebElementMock(),
194+
];
195+
196+
$elementList[0]->expects($this->once())
197+
->method('isDisplayed')
198+
->willReturn(false);
199+
200+
$elementList[1]->expects($this->once())
201+
->method('isDisplayed')
202+
->willReturn(true);
203+
204+
$elementList[2]->expects($this->once())
205+
->method('isDisplayed')
206+
->willReturn(true);
207+
208+
$this->driverMock->expects($this->once())
209+
->method('findElements')
210+
->with($this->isInstanceOf(WebDriverBy::class))
211+
->willReturn($elementList);
212+
213+
$condition = WebDriverExpectedCondition::visibilityOfAnyElementLocated(WebDriverBy::cssSelector('.foo'));
214+
215+
$this->assertSame([$elementList[1], $elementList[2]], $this->wait->until($condition));
216+
}
217+
188218
public function testShouldDetectInvisibilityOfElementLocatedConditionOnNoSuchElementException()
189219
{
190220
$element = $this->createRemoteWebElementMock();

0 commit comments

Comments
 (0)