Scroll to page top/bottom or an element

Amended by @chillu, see https://github.com/silverstripe-labs/silverstripe-behat-extension/pull/74
This commit is contained in:
Jeffrey Guo 2014-10-03 17:30:55 +13:00 committed by Ingo Schommer
parent 13271a8a6d
commit df4a859dbc

View File

@ -613,7 +613,7 @@ JS;
*
* @Given /^I fill in "(?P<field>[^"]*)" with "(?P<value>[^"]*)" in the "(?P<region>[^"]*)" region$/
*/
public function iFillinTheRegion($field, $value, $region) {
public function iFillinTheRegion($field, $value, $region){
$context = $this->getMainContext();
$regionObj = $context->getRegionObj($region);
assertNotNull($regionObj, "Region Object is null");
@ -796,30 +796,6 @@ JS;
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);
@ -832,43 +808,61 @@ JS;
}
/**
* 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 "([^"]*)"$/
* @Given /^I scroll to the bottom$/
*/
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);
});
public function iScrollToBottom() {
$javascript = 'window.scrollTo(0, Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, document.documentElement.clientHeight));';
$this->getSession()->executeScript($javascript);
}
/**
* 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
* @Given /^I scroll to the top$/
*/
public function spin($lambda, $wait = 5, $sleep = 0.5) {
for ($i = 0; $i < $wait; $i++){
try {
if ($lambda($this)) return true;
} catch (\Exception $e) {
// do nothing
public function iScrollToTop() {
$this->getSession()->executeScript('window.scrollTo(0,0);');
}
sleep($sleep);
/**
* Scroll to a certain element by label.
* Requires an "id" attribute to uniquely identify the element in the document.
*
* Example: Given I scroll to the "Submit" button
* Example: Given I scroll to the "My Date" field
*
* @Given /^I scroll to the "([^"]*)" (field|link|button)$/
*/
public function iScrollToField($locator, $type) {
$page = $this->getSession()->getPage();
$el = $page->find('named', array($type, "'$locator'"));
assertNotNull($el, sprintf('%s element not found', $locator));
$id = $el->getAttribute('id');
if(empty($id)) {
throw new \InvalidArgumentException('Element requires an "id" attribute');
}
throw new \Exception ("Timeout thrown: Callback does not return true");
$js = sprintf("document.getElementById('%s').scrollIntoView(true);", $id);
$this->getSession()->executeScript($js);
}
/**
* Scroll to a certain element by CSS selector.
* Requires an "id" attribute to uniquely identify the element in the document.
*
* Example: Given I scroll to the ".css_element" element
*
* @Given /^I scroll to the "(?P<locator>(?:[^"]|\\")*)" element$/
*/
public function iScrollToElement($locator) {
$el = $this->getSession()->getPage()->find('css', $locator);
assertNotNull($el, sprintf('The element "%s" is not found', $locator));
$id = $el->getAttribute('id');
if(empty($id)) {
throw new \InvalidArgumentException('Element requires an "id" attribute');
}
$js = sprintf("document.getElementById('%s').scrollIntoView(true);", $id);
$this->getSession()->executeScript($js);
}
}