Skip to content

Commit

Permalink
Fixes silverstripe#27: I wait for the text/element
Browse files Browse the repository at this point in the history
  • Loading branch information
srizzling committed Sep 30, 2014
1 parent 060f45f commit 6f648a4
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/SilverStripe/BehatExtension/Context/BasicContext.php
Expand Up @@ -800,4 +800,90 @@ public function theTextBeforeAfter($textBefore, $order, $textAfter, $element) {
assertTrue(strpos($text, $textBefore) > strpos($text, $textAfter));
}
}

/**
* Wait until a certain amount of seconds till I see an element identified by a CSS selector.
*
* Example: Given I wait for 10 seconds until I see the ".css_element" element
*
* @Given /^I wait for (\d+) seconds until I see the "([^"]*)" element$/
**/
public function iWaitXUntilISee($wait, $selector) {
$page = $this->getSession()->getPage();

$this->spin(function($page) use ($page, $selector){
$element = $page->find('css', $selector);

if(empty($element)) {
return false;
} else {
return $element->isVisible();
}

}, $wait);
}

/**
* Waits until it can see an element identified by a CSS selector,
* or until it timeouts (default is 60 seconds)
*
* Example: Given I wait until I see the ".css_element" element
*
* @Given /^I wait until I see the "([^"]*)" element$/
*
*/
public function iWaitUntilISee($selector) {
$page = $this->getSession()->getPage();

$this->spin(function($page) use ($page, $selector){
$element = $page->find('css', $selector);

if(empty($element)) {
return false;
} else {
return $element->isVisible();
}
});
}

/**
* Waits until it can see a certain text in the page, or until it timeouts (default is 60 seconds)
*
* Example: Given I wait until I see the text "Hello World"
*
* @Given /^I wait until I see the text "([^"]*)"$/
*/
public function iWaitUntilISeeText($text) {
$page = $this->getSession()->getPage();


$this->spin(function($page) use ($page, $text){
// returns true if text is contained within the page
return (strpos($page->getText(), $text) !== false);
});
}

/**
* Continuously poll until callback returns true. Read more about the use of
* the spin function (@link http://docs.behat.org/en/v2.5/cookbook/using_spin_functions.html)
* If the callback doesn't return true within $wait, timeout and throw error
*
* @param callback $lambda function to run continuously
* @param integer $wait Timeout, default is 60 secs.
*
* @return boolean Returns true or false depending on the spin function
*/
public function spin($lambda, $wait = 5, $sleep = 0.5) {

This comment has been minimized.

Copy link
@jeffreyguo

jeffreyguo Oct 9, 2014

@srizzling 5*0.5=2.5 seconds, it's too short, almost failed to load CMS.

This comment has been minimized.

Copy link
@srizzling

srizzling Oct 9, 2014

Author Owner

You can use

@Given /^I wait for (\d+) seconds until I see the "([^"]*)" element$/ which allows you to extend the time
for ($i = 0; $i < $wait; $i++){
try {
if ($lambda($this)) return true;
} catch (\Exception $e) {
// do nothing
}

sleep($sleep);
}

throw new \Exception ("Timeout thrown: Callback does not return true");
}
}

0 comments on commit 6f648a4

Please sign in to comment.