diff --git a/src/Context/PantherContext.php b/src/Context/PantherContext.php index 2d93205..1053aa1 100644 --- a/src/Context/PantherContext.php +++ b/src/Context/PantherContext.php @@ -12,8 +12,6 @@ use Facebook\WebDriver\Exception\NoSuchCookieException; use Facebook\WebDriver\Exception\NoSuchElementException; use Facebook\WebDriver\Exception\TimeoutException; -use Facebook\WebDriver\WebDriverBy; -use Facebook\WebDriver\WebDriverExpectedCondition; use PantherExtension\Driver\Exception\InvalidArgumentException; use PantherExtension\Driver\Exception\LogicException; use PantherExtension\Driver\PantherDriver; @@ -94,7 +92,7 @@ public function iWaitForElementToBeVisible(string $element): void $driver = $this->getDriver(); try { - $driver->wait(3000, WebDriverExpectedCondition::visibilityOfAnyElementLocated(WebDriverBy::cssSelector($element))); + $driver->getWaitHelper()->waitForVisibility($element); } catch (TimeoutException $exception) { throw new LogicException(sprintf('The desired element cannot be found in the given timeout or seems to appear later than expected.')); } catch (NoSuchElementException $exception) { @@ -113,7 +111,7 @@ public function iWaitForElementToBeVisibleDuring(string $element, int $timeoutIn $driver = $this->getDriver(); try { - $driver->wait($timeoutInMilliseconds, WebDriverExpectedCondition::visibilityOfAnyElementLocated(WebDriverBy::cssSelector($element))); + $driver->getWaitHelper()->waitForVisibility($element, $timeoutInMilliseconds / 1000); } catch (TimeoutException $exception) { throw new LogicException(sprintf('The desired element cannot be found in the given timeout or seems to appear later than expected.')); } catch (NoSuchElementException $exception) { diff --git a/src/Driver/Helper/WaitHelper.php b/src/Driver/Helper/WaitHelper.php index 4fae077..dcd1c05 100644 --- a/src/Driver/Helper/WaitHelper.php +++ b/src/Driver/Helper/WaitHelper.php @@ -40,6 +40,22 @@ public function waitFor(string $element, int $timeoutInSeconds = 30, int $interv $this->client->waitFor($element, $timeoutInSeconds, $intervalInMillisecond); } + public function waitForVisibility(string $elementLocator, int $timeoutInSeconds = 30, int $intervalInMilliseconds = 250): void + { + $locator = trim($elementLocator); + + $by = '' === $locator || '/' !== $locator[0] + ? WebDriverBy::cssSelector($locator) + : WebDriverBy::xpath($locator) + ; + + $this->client->wait($timeoutInSeconds, $intervalInMilliseconds)->until( + WebDriverExpectedCondition::visibilityOfElementLocated($by) + ); + + $this->client->refreshCrawler(); + } + public function waitForText(string $elementLocator, string $text, bool $strict = false, int $timeoutInSeconds = 30, int $intervalInMilliseconds = 250): void { $this->checkResearchContent($text); diff --git a/tests/Driver/PantherDriverTest.php b/tests/Driver/PantherDriverTest.php index b14b53f..e0d494e 100644 --- a/tests/Driver/PantherDriverTest.php +++ b/tests/Driver/PantherDriverTest.php @@ -293,6 +293,18 @@ public function testClientCanWaitForElement(string $locator): void static::assertSame('Hello', $this->driver->getClient()->getCrawler()->filter('#hello')->text()); } + /** + * @dataProvider waitForDataProvider + */ + public function testClientCanWaitForElementVisibility(string $locator): void + { + $this->driver->start(); + $this->driver->visit('/waitfor.html'); + $this->driver->getWaitHelper()->waitForVisibility($locator); + + static::assertSame('Hello', $this->driver->getClient()->getCrawler()->filter('#hello')->text()); + } + public function testClientCanWaitForTextToBeVisible(): void { $this->driver->start();