-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test: Add missing Behat tests for Scroll-spy implementation, solves #86.
- Loading branch information
Showing
3 changed files
with
101 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,14 +58,12 @@ public function dom_element_should_have_computed_style($selector, $style, $value | |
} | ||
|
||
/** | ||
* Scroll the page. | ||
* Scroll the page to a given coordinate. | ||
* | ||
* @copyright 2016 Shweta Sharma on https://stackoverflow.com/a/39613869. | ||
* @Then /^I scroll page to x "(?P<posx_number>\d+)" y "(?P<posy_number>\d+)"$/ | ||
* | ||
* @param string $posx The x coordinate to scroll to. | ||
* @param string $posy The y coordinate to scroll to. | ||
* | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
|
@@ -76,4 +74,91 @@ public function i_scroll_page_to_x_y_coordinates_of_page($posx, $posy) { | |
throw new \Exception("Scrolling the page to given coordinates failed"); | ||
} | ||
} | ||
|
||
/** | ||
* Scroll the page to the DOM element with the given ID. | ||
* | ||
* @copyright 2023 Alexander Bias <[email protected]> | ||
* @Then I scroll page to DOM element with ID :arg1 | ||
* @param string $selector | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public function i_scroll_page_to_dom_element_with_id($selector) { | ||
$scrolljs = "(function(){ | ||
let element = document.getElementById('$selector'); | ||
let y = element.offsetTop; | ||
document.getElementById('page').scrollTo(0, y); | ||
})();"; | ||
try { | ||
$this->getSession()->executeScript($scrolljs); | ||
} catch (Exception $e) { | ||
throw new \Exception('Scrolling the page to the \''.$selector.'\' DOM element failed'); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the top of the page is at the top of the viewport. | ||
* | ||
* @copyright 2023 Alexander Bias <[email protected]> | ||
* @Then page top is at the top of the viewport | ||
* @throws ExpectationException | ||
*/ | ||
public function page_top_is_at_top_of_viewport() { | ||
$posviewportjs = " | ||
return ( | ||
document.getElementById('page').scrollTop | ||
) | ||
"; | ||
$positionviewport = $this->evaluate_script($posviewportjs); | ||
if ($positionviewport != 0) { | ||
throw new ExpectationException('The page top is not at the top of the viewport', $this->getSession()); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the top of the page is not at the top of the viewport. | ||
* | ||
* @copyright 2023 Alexander Bias <[email protected]> | ||
* @Then page top is not at the top of the viewport | ||
* @throws ExpectationException | ||
*/ | ||
public function page_top_is_not_at_top_of_viewport() { | ||
$posviewportjs = " | ||
return ( | ||
document.getElementById('page').scrollTop | ||
) | ||
"; | ||
$positionviewport = $this->evaluate_script($posviewportjs); | ||
if ($positionviewport == 0) { | ||
throw new ExpectationException('The page top is at the top of the viewport', $this->getSession()); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the given DOM element is at the top of the viewport. | ||
* | ||
* @copyright 2023 Alexander Bias <[email protected]> | ||
* @Then DOM element :arg1 is at the top of the viewport | ||
* @param string $selector | ||
* @throws ExpectationException | ||
*/ | ||
public function dom_element_is_at_top_of_viewport($selector) { | ||
$poselementjs = " | ||
return ( | ||
document.getElementById('$selector').offsetTop | ||
) | ||
"; | ||
$positionelement = $this->evaluate_script($poselementjs); | ||
$posviewportjs = " | ||
return ( | ||
document.getElementById('page').scrollTop | ||
) | ||
"; | ||
$positionviewport = $this->evaluate_script($posviewportjs); | ||
if ($positionelement > $positionviewport + 50 || | ||
$positionelement < $positionviewport - 50) { // Allow some deviation of 50px of the scrolling position. | ||
throw new ExpectationException('The DOM element \''.$selector.'\' is not a the top of the page', $this->getSession()); | ||
} | ||
} | ||
} |
This file contains 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