mirror of
https://github.com/silverstripe/silverstripe-behat-extension
synced 2024-10-22 17:05:32 +02:00
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:
parent
13271a8a6d
commit
df4a859dbc
@ -796,30 +796,6 @@ JS;
|
|||||||
public function iWaitXUntilISee($wait, $selector) {
|
public function iWaitXUntilISee($wait, $selector) {
|
||||||
$page = $this->getSession()->getPage();
|
$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){
|
$this->spin(function($page) use ($page, $selector){
|
||||||
$element = $page->find('css', $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)
|
* @Given /^I scroll to the bottom$/
|
||||||
*
|
|
||||||
* Example: Given I wait until I see the text "Hello World"
|
|
||||||
*
|
|
||||||
* @Given /^I wait until I see the text "([^"]*)"$/
|
|
||||||
*/
|
*/
|
||||||
public function iWaitUntilISeeText($text) {
|
public function iScrollToBottom() {
|
||||||
$page = $this->getSession()->getPage();
|
$javascript = 'window.scrollTo(0, Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, document.documentElement.clientHeight));';
|
||||||
|
$this->getSession()->executeScript($javascript);
|
||||||
|
|
||||||
$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
|
* @Given /^I scroll to the top$/
|
||||||
* 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) {
|
public function iScrollToTop() {
|
||||||
for ($i = 0; $i < $wait; $i++){
|
$this->getSession()->executeScript('window.scrollTo(0,0);');
|
||||||
try {
|
|
||||||
if ($lambda($this)) return true;
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
// do nothing
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user