Created
July 28, 2017 07:04
-
-
Save pix-art/31f5be07fa807fe6712819294f4630e2 to your computer and use it in GitHub Desktop.
A timeout context to help you when you have to wait for elements on your page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace FrontendBundle\Features\Context; | |
use Behat\Behat\Hook\Scope\BeforeScenarioScope; | |
use Behat\Mink\Exception\ElementNotFoundException; | |
use Behat\Mink\Exception\ElementTextException; | |
use Behat\Mink\Exception\ResponseTextException; | |
use Behat\MinkExtension\Context\RawMinkContext; | |
class TimeoutContext extends RawMinkContext | |
{ | |
const TIMEOUT = 12; | |
/** @var \Behat\MinkExtension\Context\MinkContext */ | |
private $minkContext; | |
/** @BeforeScenario */ | |
public function gatherContexts(BeforeScenarioScope $scope) | |
{ | |
$environment = $scope->getEnvironment(); | |
$this->minkContext = $environment->getContext('FrontendBundle\Features\Context\FrontendContext'); | |
} | |
/** | |
* @When /^I wait for (\d+) seconds$/ | |
*/ | |
public function iWaitForSeconds($seconds) | |
{ | |
$this->getSession()->wait($seconds * 1000); | |
} | |
/** | |
* Wait for a element. | |
* | |
* @When (I )wait :count second(s) until I see :element element | |
*/ | |
public function iWaitSecondsForElement($seconds, $element) | |
{ | |
$i = 0; | |
while ($i < $seconds) { | |
try { | |
$this->minkContext->assertElementOnPage($element); | |
return; | |
} catch (ElementNotFoundException $e) { | |
++$i; | |
sleep(1); | |
} | |
} | |
$message = "The element '$element' was not found after a $seconds seconds timeout"; | |
throw new ResponseTextException($message, $this->getSession()); | |
} | |
/** | |
* @When (I )wait until I see :element element | |
*/ | |
public function iWaitForElement($element) | |
{ | |
$this->iWaitSecondsForElement(self::TIMEOUT, $element); | |
} | |
/** | |
* Checks, that the element contains specified text after timeout. | |
* | |
* @When (I )wait :count second(s) until I see :text in the :element element | |
*/ | |
public function iWaitSecondsUntilISeeInTheElement($seconds, $text, $element) | |
{ | |
$i = 0; | |
while ($i < $seconds) { | |
try { | |
$this->minkContext->assertElementContainsText($element, $text); | |
return; | |
} catch (ElementTextException $e) { | |
++$i; | |
sleep(1); | |
} | |
} | |
$message = "The text '$text' was not found after a $seconds seconds timeout"; | |
throw new ResponseTextException($message, $this->getSession()); | |
} | |
/** | |
* Checks, that the page should contains specified text after given timeout. | |
* | |
* @When (I )wait :count second(s) until I see :text | |
*/ | |
public function iWaitSecondsUntilISee($seconds, $text) | |
{ | |
$this->iWaitSecondsUntilISeeInTheElement($seconds, $text, 'html'); | |
} | |
/** | |
* Checks, that the element contains specified text after timeout. | |
* | |
* @When (I )wait until I see :text in the :element element | |
*/ | |
public function iWaitUntilISeeInTheElement($text, $element) | |
{ | |
$this->iWaitSecondsUntilISeeInTheElement(self::TIMEOUT, $text, $element); | |
} | |
/** | |
* @When (I )wait until I see :text | |
*/ | |
public function iWaitUntilISee($text) | |
{ | |
$this->iWaitSecondsUntilISeeInTheElement(self::TIMEOUT, $text, 'html'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment