Skip to content

Commit 4009127

Browse files
Jean-Berudunglas
authored andcommitted
[Panther] add explicit error messages in "wait*" methods
1 parent 8a7d33d commit 4009127

File tree

3 files changed

+101
-10
lines changed

3 files changed

+101
-10
lines changed

src/Client.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ public function waitFor(string $locator, int $timeoutInSecond = 30, int $interva
348348
$by = self::createWebDriverByFromLocator($locator);
349349

350350
$this->wait($timeoutInSecond, $intervalInMillisecond)->until(
351-
WebDriverExpectedCondition::presenceOfElementLocated($by)
351+
WebDriverExpectedCondition::presenceOfElementLocated($by),
352+
\sprintf('Element "%s" not found within %d seconds.', $locator, $timeoutInSecond),
352353
);
353354

354355
return $this->crawler = $this->createCrawler();
@@ -367,7 +368,8 @@ public function waitForStaleness(string $locator, int $timeoutInSecond = 30, int
367368
$element = $this->findElement($by);
368369

369370
$this->wait($timeoutInSecond, $intervalInMillisecond)->until(
370-
WebDriverExpectedCondition::stalenessOf($element)
371+
WebDriverExpectedCondition::stalenessOf($element),
372+
\sprintf('Element "%s" did not become stale within %d seconds.', $locator, $timeoutInSecond),
371373
);
372374

373375
return $this->crawler = $this->createCrawler();
@@ -384,7 +386,8 @@ public function waitForVisibility(string $locator, int $timeoutInSecond = 30, in
384386
$by = self::createWebDriverByFromLocator($locator);
385387

386388
$this->wait($timeoutInSecond, $intervalInMillisecond)->until(
387-
WebDriverExpectedCondition::visibilityOfElementLocated($by)
389+
WebDriverExpectedCondition::visibilityOfElementLocated($by),
390+
\sprintf('Element "%s" did not become visible within %d seconds.', $locator, $timeoutInSecond),
388391
);
389392

390393
return $this->crawler = $this->createCrawler();
@@ -401,7 +404,8 @@ public function waitForInvisibility(string $locator, int $timeoutInSecond = 30,
401404
$by = self::createWebDriverByFromLocator($locator);
402405

403406
$this->wait($timeoutInSecond, $intervalInMillisecond)->until(
404-
WebDriverExpectedCondition::invisibilityOfElementLocated($by)
407+
WebDriverExpectedCondition::invisibilityOfElementLocated($by),
408+
\sprintf('Element "%s" did not become invisible within %d seconds.', $locator, $timeoutInSecond),
405409
);
406410

407411
return $this->crawler = $this->createCrawler();
@@ -418,7 +422,8 @@ public function waitForElementToContain(string $locator, string $text, int $time
418422
$by = self::createWebDriverByFromLocator($locator);
419423

420424
$this->wait($timeoutInSecond, $intervalInMillisecond)->until(
421-
WebDriverExpectedCondition::elementTextContains($by, $text)
425+
WebDriverExpectedCondition::elementTextContains($by, $text),
426+
\sprintf('Element "%s" did not contain "%s" within %d seconds.', $locator, $text, $timeoutInSecond),
422427
);
423428

424429
return $this->crawler = $this->createCrawler();
@@ -435,7 +440,8 @@ public function waitForElementToNotContain(string $locator, string $text, int $t
435440
$by = self::createWebDriverByFromLocator($locator);
436441

437442
$this->wait($timeoutInSecond, $intervalInMillisecond)->until(
438-
PantherWebDriverExpectedCondition::elementTextNotContains($by, $text)
443+
PantherWebDriverExpectedCondition::elementTextNotContains($by, $text),
444+
\sprintf('Element "%s" still contained "%s" after %d seconds.', $locator, $text, $timeoutInSecond),
439445
);
440446

441447
return $this->crawler = $this->createCrawler();
@@ -452,7 +458,8 @@ public function waitForAttributeToContain(string $locator, string $attribute, st
452458
$by = self::createWebDriverByFromLocator($locator);
453459

454460
$this->wait($timeoutInSecond, $intervalInMillisecond)->until(
455-
PantherWebDriverExpectedCondition::elementAttributeContains($by, $attribute, $text)
461+
PantherWebDriverExpectedCondition::elementAttributeContains($by, $attribute, $text),
462+
\sprintf('Element "%s" attribute "%s" did not contain "%s" within %d seconds.', $locator, $attribute, $text, $timeoutInSecond),
456463
);
457464

458465
return $this->crawler = $this->createCrawler();
@@ -469,7 +476,8 @@ public function waitForAttributeToNotContain(string $locator, string $attribute,
469476
$by = self::createWebDriverByFromLocator($locator);
470477

471478
$this->wait($timeoutInSecond, $intervalInMillisecond)->until(
472-
PantherWebDriverExpectedCondition::elementAttributeNotContains($by, $attribute, $text)
479+
PantherWebDriverExpectedCondition::elementAttributeNotContains($by, $attribute, $text),
480+
\sprintf('Element "%s" attribute "%s" still contained "%s" after %d seconds.', $locator, $attribute, $text, $timeoutInSecond),
473481
);
474482

475483
return $this->crawler = $this->createCrawler();
@@ -486,7 +494,8 @@ public function waitForEnabled(string $locator, int $timeoutInSecond = 30, int $
486494
$by = self::createWebDriverByFromLocator($locator);
487495

488496
$this->wait($timeoutInSecond, $intervalInMillisecond)->until(
489-
PantherWebDriverExpectedCondition::elementEnabled($by)
497+
PantherWebDriverExpectedCondition::elementEnabled($by),
498+
\sprintf('Element "%s" did not become enabled within %d seconds.', $locator, $timeoutInSecond),
490499
);
491500

492501
return $this->crawler = $this->createCrawler();
@@ -503,7 +512,8 @@ public function waitForDisabled(string $locator, int $timeoutInSecond = 30, int
503512
$by = self::createWebDriverByFromLocator($locator);
504513

505514
$this->wait($timeoutInSecond, $intervalInMillisecond)->until(
506-
PantherWebDriverExpectedCondition::elementDisabled($by)
515+
PantherWebDriverExpectedCondition::elementDisabled($by),
516+
\sprintf('Element "%s" did not become disabled within %d seconds.', $locator, $timeoutInSecond),
507517
);
508518

509519
return $this->crawler = $this->createCrawler();

tests/ClientTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Facebook\WebDriver\Exception\InvalidSelectorException;
1717
use Facebook\WebDriver\Exception\StaleElementReferenceException;
18+
use Facebook\WebDriver\Exception\TimeoutException;
1819
use Facebook\WebDriver\JavaScriptExecutor;
1920
use Facebook\WebDriver\WebDriver;
2021
use Facebook\WebDriver\WebDriverExpectedCondition;
@@ -188,6 +189,73 @@ public function testWaitForStalenessElement(string $locator): void
188189
$this->assertInstanceOf(Crawler::class, $crawler);
189190
}
190191

192+
public static function waitForExceptionsProvider(): iterable
193+
{
194+
yield 'waitFor' => [
195+
'waitFor',
196+
['locator' => '#not_found'],
197+
'Element "#not_found" not found within 1 seconds.',
198+
];
199+
yield 'waitForStaleness' => [
200+
'waitForStaleness',
201+
['locator' => '#price'],
202+
'Element "#price" did not become stale within 1 seconds.',
203+
];
204+
yield 'waitForVisibility' => [
205+
'waitForVisibility',
206+
['locator' => '#hidden'],
207+
'Element "#hidden" did not become visible within 1 seconds.',
208+
];
209+
yield 'waitForInvisibility' => [
210+
'waitForInvisibility',
211+
['locator' => '#price'],
212+
'Element "#price" did not become invisible within 1 seconds.',
213+
];
214+
yield 'waitForElementToContain' => [
215+
'waitForElementToContain',
216+
['locator' => '#price', 'text' => '36'],
217+
'Element "#price" did not contain "36" within 1 seconds.',
218+
];
219+
yield 'waitForElementToNotContain' => [
220+
'waitForElementToNotContain',
221+
['locator' => '#price', 'text' => '42'],
222+
'Element "#price" still contained "42" after 1 seconds.',
223+
];
224+
yield 'waitForAttributeToContain' => [
225+
'waitForAttributeToContain',
226+
['locator' => '#price', 'attribute' => 'data-old-price', 'text' => '42'],
227+
'Element "#price" attribute "data-old-price" did not contain "42" within 1 seconds.',
228+
];
229+
yield 'waitForAttributeToNotContain' => [
230+
'waitForAttributeToNotContain',
231+
['locator' => '#price', 'attribute' => 'data-old-price', 'text' => '36'],
232+
'Element "#price" attribute "data-old-price" still contained "36" after 1 seconds.',
233+
];
234+
yield 'waitForEnabled' => [
235+
'waitForEnabled',
236+
['locator' => '#disabled'],
237+
'Element "#disabled" did not become enabled within 1 seconds.',
238+
];
239+
yield 'waitForDisabled' => [
240+
'waitForDisabled',
241+
['locator' => '#enabled'],
242+
'Element "#enabled" did not become disabled within 1 seconds.',
243+
];
244+
}
245+
246+
/**
247+
* @dataProvider waitForExceptionsProvider
248+
*/
249+
public function testWaitForExceptions(string $method, array $args, string $message): void
250+
{
251+
$this->expectException(TimeoutException::class);
252+
$this->expectExceptionMessage($message);
253+
254+
$client = self::createPantherClient();
255+
$client->request('GET', '/waitfor-exceptions.html');
256+
$client->$method(...($args + ['timeoutInSecond' => 1]));
257+
}
258+
191259
public function testExecuteScript(): void
192260
{
193261
$client = self::createPantherClient();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>WaitFor - Exception</title>
6+
</head>
7+
<body>
8+
<p id="price" data-old-price="36">42</p>
9+
<p id="hidden" style="opacity: 0;">Hidden</p>
10+
<input id="enabled" />
11+
<input id="disabled" disabled />
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)