Skip to content

Commit

Permalink
refactor(context): PantherContext improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Guikingone committed May 6, 2020
1 parent 4da188c commit 849fd2c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/Context/PantherContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions src/Driver/Helper/WaitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions tests/Driver/PantherDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 849fd2c

Please sign in to comment.