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

Commit a05c139

Browse files
authored
Merge pull request #430 from OndraM/feature/deprecate-textToBePresentInElementValue
Deprecate text to be present in element value expected condition
2 parents 48b9c7a + 7a855dd commit a05c139

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project versioning adheres to [Semantic Versioning](http://semver.org/).
77
- Cookies retrieved using `getCookieNamed()` and `getCookies()` methods of `WebDriverOptions` are now encapsulated in `Cookie` object instead of an plain array. The object implements `ArrayAccess` interface to provide backward compatibility.
88
- `ext-zip` is now specified as required dependency in composer.json (but the extension was already required by the code, though).
99
- Deprecate `WebDriverCapabilities::isJavascriptEnabled()` method.
10+
- Deprecate `textToBePresentInElementValue` expected condition in favor of `elementValueContains`.
1011

1112
### Fixed
1213
- Do not throw fatal error when `null` is passed to `sendKeys()`.

lib/WebDriverExpectedCondition.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,25 @@ function (WebDriver $driver) use ($by, $regexp) {
292292
/**
293293
* An expectation for checking if the given text is present in the specified elements value attribute.
294294
*
295+
* @codeCoverageIgnore
296+
* @deprecated Use WebDriverExpectedCondition::elementValueContains() instead
295297
* @param WebDriverBy $by The locator used to find the element.
296298
* @param string $text The text to be presented in the element value.
297299
* @return WebDriverExpectedCondition<bool> Condition returns whether the text is present in value attribute.
298300
*/
299301
public static function textToBePresentInElementValue(WebDriverBy $by, $text)
302+
{
303+
return self::elementValueContains($by, $text);
304+
}
305+
306+
/**
307+
* An expectation for checking if the given text is present in the specified elements value attribute.
308+
*
309+
* @param WebDriverBy $by The locator used to find the element.
310+
* @param string $text The text to be presented in the element value.
311+
* @return WebDriverExpectedCondition<bool> Condition returns whether the text is present in value attribute.
312+
*/
313+
public static function elementValueContains(WebDriverBy $by, $text)
300314
{
301315
return new static(
302316
function (WebDriver $driver) use ($by, $text) {

tests/unit/WebDriverExpectedConditionTest.php

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

188+
public function testShouldDetectInvisibilityOfElementLocatedConditionOnNoSuchElementException()
189+
{
190+
$element = $this->createRemoteWebElementMock();
191+
192+
$this->driverMock->expects($this->at(0))
193+
->method('findElement')
194+
->with($this->isInstanceOf(WebDriverBy::class))
195+
->willReturn($element);
196+
197+
$element->expects($this->at(0))
198+
->method('isDisplayed')
199+
->willReturn(true);
200+
201+
$this->driverMock->expects($this->at(1))
202+
->method('findElement')
203+
->with($this->isInstanceOf(WebDriverBy::class))
204+
->willThrowException(new NoSuchElementException(''));
205+
206+
$condition = WebDriverExpectedCondition::invisibilityOfElementLocated(WebDriverBy::cssSelector('.foo'));
207+
208+
$this->assertTrue($this->wait->until($condition));
209+
}
210+
211+
public function testShouldDetectInvisibilityOfElementLocatedConditionOnStaleElementReferenceException()
212+
{
213+
$element = $this->createRemoteWebElementMock();
214+
215+
$this->driverMock->expects($this->exactly(2))
216+
->method('findElement')
217+
->with($this->isInstanceOf(WebDriverBy::class))
218+
->willReturn($element);
219+
220+
$element->expects($this->at(0))
221+
->method('isDisplayed')
222+
->willReturn(true);
223+
224+
$element->expects($this->at(1))
225+
->method('isDisplayed')
226+
->willThrowException(new StaleElementReferenceException(''));
227+
228+
$condition = WebDriverExpectedCondition::invisibilityOfElementLocated(WebDriverBy::cssSelector('.foo'));
229+
230+
$this->assertTrue($this->wait->until($condition));
231+
}
232+
233+
public function testShouldDetectInvisibilityOfElementLocatedConditionWhenElementBecamesInvisible()
234+
{
235+
$element = $this->createRemoteWebElementMock();
236+
237+
$this->driverMock->expects($this->exactly(2))
238+
->method('findElement')
239+
->with($this->isInstanceOf(WebDriverBy::class))
240+
->willReturn($element);
241+
242+
$element->expects($this->at(0))
243+
->method('isDisplayed')
244+
->willReturn(true);
245+
246+
$element->expects($this->at(1))
247+
->method('isDisplayed')
248+
->willReturn(false);
249+
250+
$condition = WebDriverExpectedCondition::invisibilityOfElementLocated(WebDriverBy::cssSelector('.foo'));
251+
252+
$this->assertTrue($this->wait->until($condition));
253+
}
254+
188255
public function testShouldDetectVisibilityOfCondition()
189256
{
190257
$element = $this->createRemoteWebElementMock();
@@ -292,6 +359,41 @@ public function testShouldDetectElementTextMatchesCondition()
292359
$this->assertTrue($this->wait->until($condition));
293360
}
294361

362+
public function testShouldDetectElementValueContainsCondition()
363+
{
364+
// Set-up the consecutive calls to apply() as follows:
365+
// Call #1: throws NoSuchElementException
366+
// Call #2: return Element, but getAttribute will throw StaleElementReferenceException
367+
// Call #3: return Element, getAttribute('value') will return not-matching text
368+
// Call #4: return Element, getAttribute('value') will return matching text
369+
370+
$element = $this->createRemoteWebElementMock();
371+
372+
$element->expects($this->at(0))
373+
->method('getAttribute')
374+
->with('value')
375+
->willThrowException(new StaleElementReferenceException(''));
376+
377+
$element->expects($this->at(1))
378+
->method('getAttribute')
379+
->with('value')
380+
->willReturn('wrong text');
381+
382+
$element->expects($this->at(2))
383+
->method('getAttribute')
384+
->with('value')
385+
->willReturn('matching text');
386+
387+
$this->setupDriverToReturnElementAfterAnException($element, 4);
388+
389+
$condition = WebDriverExpectedCondition::elementValueContains(
390+
WebDriverBy::cssSelector('.foo'),
391+
'matching'
392+
);
393+
394+
$this->assertTrue($this->wait->until($condition));
395+
}
396+
295397
public function testShouldDetectNumberOfWindowsToBeCondition()
296398
{
297399
$this->driverMock->expects($this->any())

0 commit comments

Comments
 (0)