Skip to content

Instantly share code, notes, and snippets.

@MaksymKrut
Created August 7, 2016 16:54
Show Gist options
  • Save MaksymKrut/88d98584565a6dbcbab7d178be50a945 to your computer and use it in GitHub Desktop.
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.
/**
* 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