Created
August 7, 2016 16:54
-
-
Save MaksymKrut/88d98584565a6dbcbab7d178be50a945 to your computer and use it in GitHub Desktop.
Behat Mink Selenium WebDriver spin() helper function implementation. Checks if XPath, CSS element exists on the page defined number of times/seconds.
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
/** | |
* Wait for function return true, trying to execute it number of times | |
* @param $lambda | |
* @param int $tries | |
* @return bool | |
* @throws Exception | |
*/ | |
public function spin($lambda, $tries = 60) | |
{ | |
for ($i = 0; $i < $tries; $i++) { | |
try { | |
$lambda(); | |
sleep(1); | |
return; | |
} catch (Exception $e) { | |
sleep(1); | |
} | |
sleep(1); | |
} | |
$backtrace = debug_backtrace(); | |
throw new Exception( | |
"Timeout thrown by " . $backtrace[1]['class'] . "::" . $backtrace[1]['function'] . "()\n" . | |
$backtrace[1]['file'] . ", line " . $backtrace[1]['line'] | |
); | |
} | |
// Usage | |
/** | |
* @When I click :key element | |
* @param string $key | |
*/ | |
public function iClickElement($key) | |
{ | |
// Take XPath, CSS value by key from you key-value store | |
$this->spin(function () { | |
$this->getSession()->getPage()->find($this->locatorType, $this->locatorValue); | |
// or $this->assertSession()->elementExists($this->locatorType, $this->locatorValue); | |
}); | |
$this->element = $this->getSession()->getPage()->find($this->locatorType, $this->locatorValue); | |
$this->element->click(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment