A timeout context to help you when you have to wait for elements on your page
<?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