Skip to content

Commit 310e828

Browse files
fix: offset scrollspy so righthand-nav highlights the correct entry (ietf-tools#3820)
Also rewrites scroll_to_element() using javascript. This seems to be less flaky than the ActionChains implementation. Slight change in behavior - now scrolls the requested element to the middle of the window instead of barely into view.
1 parent 79c7081 commit 310e828

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

ietf/meeting/tests_js.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,10 +1529,8 @@ def test_session_materials_modal(self):
15291529
),
15301530
'Modal open button not found or not clickable',
15311531
)
1532-
# FIXME: no idea why we need js instead of the following:
1533-
# self.scroll_to_element(open_modal_button)
1534-
# open_modal_button.click()
1535-
self.driver.execute_script("arguments[0].click();", open_modal_button)
1532+
self.scroll_to_element(open_modal_button)
1533+
open_modal_button.click()
15361534
WebDriverWait(self.driver, 2).until(
15371535
expected_conditions.visibility_of(modal_div),
15381536
'Modal did not become visible after clicking open button',
@@ -1586,6 +1584,7 @@ def test_session_materials_modal(self):
15861584
)
15871585
self.scroll_to_element(open_modal_button)
15881586
open_modal_button.click()
1587+
# self.driver.execute_script("arguments[0].click();", open_modal_button)
15891588
WebDriverWait(self.driver, 2).until(
15901589
expected_conditions.visibility_of(modal_div),
15911590
'Modal did not become visible after clicking open button for refresh test',

ietf/static/js/ietf.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,13 @@ $(function () {
251251
}
252252
});
253253

254+
// offset the scrollspy to account for the menu bar
255+
const contentOffset = contentElement ? contentElement.offset().top : 0;
256+
254257
$("body")
255258
.attr("data-bs-spy", "scroll")
256259
.attr("data-bs-target", "#righthand-nav")
260+
.attr("data-bs-offset", contentOffset)
257261
.scrollspy("refresh");
258262

259263
}

ietf/utils/jstest.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from selenium import webdriver
1212
from selenium.webdriver.chrome.service import Service
1313
from selenium.webdriver.chrome.options import Options
14-
from selenium.webdriver.common.action_chains import ActionChains
1514
from selenium.webdriver.common.by import By
1615
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
1716
except ImportError as e:
@@ -86,8 +85,16 @@ def login(self, username='plain'):
8685

8786
def scroll_to_element(self, element):
8887
"""Scroll an element into view"""
89-
actions = ActionChains(self.driver)
90-
actions.move_to_element(element).perform()
88+
# Compute the offset to put the element in the center of the window
89+
win_height = self.driver.get_window_rect()['height']
90+
offset = element.rect['y'] + (element.rect['height'] - win_height) // 2
91+
self.driver.execute_script(
92+
'window.scroll({top: arguments[0], behavior: "instant"})',
93+
offset,
94+
)
95+
# The ActionChains approach below seems to be fragile, hence he JS above.
96+
# actions = ActionChains(self.driver)
97+
# actions.move_to_element(element).perform()
9198

9299

93100
class presence_of_element_child_by_css_selector:

0 commit comments

Comments
 (0)