Skip to content

Commit

Permalink
Test: Add missing Behat tests for Scroll-spy implementation, solves #86.
Browse files Browse the repository at this point in the history
  • Loading branch information
abias committed Oct 29, 2023
1 parent 375d2a0 commit 9085ace
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changes

### Unreleased

* 2023-10-29 - Test: Add missing Behat tests for Scroll-spy implementation, solves #86.
* 2023-10-14 - Add automated release to moodle.org/plugins

### v4.2-r3
Expand Down
91 changes: 88 additions & 3 deletions tests/behat/behat_theme_boost_union_base_general.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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());
}
}
}
26 changes: 12 additions & 14 deletions tests/behat/theme_boost_union_feelsettings_navigation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,15 @@ Feature: Configuring the theme_boost_union plugin for the "Navigation" tab on th
And I navigate to "Development > Purge caches" in site administration
And I press "Purge all caches"
And I am on "Course 1" course homepage
And I scroll page to x "0" y "250"
And I scroll page to DOM element with ID "section-4"
And I turn editing mode on
# The rest of this scenario isn't tested yet as the necessary steps still have to be implemented:
# Then The page will be reloaded
# And The page view will scroll back to x "0" y "250"
# And I turn editing mode off
# Then The page will be reloaded
# And The page view will scroll back to x "0" y "250"
And I wait "2" seconds
Then DOM element "section-4" is at the top of the viewport
And page top is not at the top of the viewport
And I turn editing mode off
And I wait "2" seconds
Then DOM element "section-4" is at the top of the viewport
And page top is not at the top of the viewport

@javascript
Scenario: Setting: Scrollspy - Disable "Scrollspy" (countercheck)
Expand All @@ -156,13 +157,10 @@ Feature: Configuring the theme_boost_union plugin for the "Navigation" tab on th
And I am on "Course 1" course homepage
And I scroll page to x "0" y "250"
And I turn editing mode on
# The rest of this scenario isn't tested yet as the necessary steps still have to be implemented:
# Then The page will be reloaded
# And The page view will remain at x "0" y "0"
# And I turn editing mode off
# Then The page will be reloaded
# And The page view will scroll back to x "0" y "250"
# And The page view will remain at x "0" y "0"
Then page top is at the top of the viewport
And I scroll page to x "0" y "250"
And I turn editing mode off
Then page top is at the top of the viewport

@javascript
Scenario: Setting: Activity navigation - Enable "Activity navigation"
Expand Down

0 comments on commit 9085ace

Please sign in to comment.