Created
July 13, 2018 15:00
-
-
Save mglaman/08eec46cc4619343b994fa0ba1481b68 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
use Behat\Mink\Driver\GoutteDriver; | |
use Behat\Mink\Exception\ElementNotFoundException; | |
use Behat\Mink\Exception\ExpectationException; | |
use Drupal\DrupalExtension\Context\RawDrupalContext; | |
use Behat\Behat\Context\SnippetAcceptingContext; | |
/** | |
* Defines application features from the specific context. | |
*/ | |
class FeatureContext extends RawDrupalContext implements SnippetAcceptingContext { | |
/** | |
* @Given I wait for the :region region | |
*/ | |
public function iWaitForTheRegion($region) { | |
$this->spin(function (FeatureContext $context) use ($region) { | |
try { | |
$session = $this->getSession(); | |
$region_obj = $session->getPage()->find('region', $region); | |
if (!$region_obj) { | |
throw new \Exception(sprintf('No region "%s" found on the page %s.', $region, $session->getCurrentUrl())); | |
} | |
return TRUE; | |
} | |
catch (\Exception $e) { | |
return FALSE; | |
} | |
}); | |
} | |
/** | |
* @Then /^(?:|I )wait to see "(?P<text>(?:[^"]|\\")*)"$/ | |
*/ | |
public function iWaitToSee($text) { | |
$this->spin(function (FeatureContext $context) use ($text) { | |
try { | |
$this->assertSession()->pageTextContains($text); | |
return TRUE; | |
} | |
catch (ElementNotFoundException $e) { | |
return FALSE; | |
} | |
}); | |
} | |
/** | |
* Based on Behat's own example. | |
* | |
* @see http://docs.behat.org/en/v2.5/cookbook/using_spin_functions.html#adding-a-timeout | |
* @param $lambda | |
* @param int $wait | |
* @throws \Exception | |
*/ | |
public function spin($lambda, $wait = 60) { | |
$time = time(); | |
$stopTime = $time + $wait; | |
while (time() < $stopTime) { | |
try { | |
if ($lambda($this)) { | |
return; | |
} | |
} | |
catch (\Exception $e) { | |
// do nothing. | |
} | |
usleep(250000); | |
} | |
throw new \Exception("Spin function timed out after {$wait} seconds"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment