Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions tests/pages/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import shutil
from typing import Literal
from urllib.parse import urlparse, urlsplit
from urllib.parse import urljoin, urlparse, urlsplit

from retry import retry
from selenium.common.exceptions import (
Expand Down Expand Up @@ -304,25 +304,47 @@ def get_h1_text(self):

class PageWithStickyNavMixin:
def scrollToRevealElement(self, selector=None, xpath=None, stuckToBottom=True):
namespace = "window.GOVUK.stickAtBottomWhenScrolling"
if stuckToBottom is False:
namespace = "window.GOVUK.stickAtTopWhenScrolling"

current_url = self.driver.current_url

if "staging-notify.works" in current_url:
base_url = "https://static.staging-notify.works"
else:
# Fallback to the url from the globally imported config
base_url = config["notify_admin_url"]

module_url = f"{base_url}/assets/javascripts/esm/stick-to-window-when-scrolling.mjs"

# Flattened directly onto window
prop_name = "stickAtBottomWhenScrolling" if stuckToBottom else "stickAtTopWhenScrolling"
namespace = f"window.{prop_name}"

if selector is not None:
js_str = (
f"if ('scrollToRevealElement' in {namespace}){namespace}.scrollToRevealElement(document.querySelector('{selector}'))"
)
self.driver.execute_script(js_str)
element = f"document.querySelector('{selector}')"
elif xpath is not None:
js_str = f"""(function (document) {{
if ('scrollToRevealElement' in {namespace}) {{
var matches = document.evaluate("{xpath}", document, null, XPathResult.ANY_TYPE, null);
if (matches) {{
{namespace}.scrollToRevealElement(matches.iterateNext());
}}
}}
}}(document));"""
self.driver.execute_script(js_str)
element = f'(document.evaluate("{xpath}", document, null, XPathResult.ANY_TYPE, null)).iterateNext()'
else:
return

js_str = f"""
var callback = arguments[arguments.length - 1];

import('{module_url}')
.then(module => {{
window['{prop_name}'] = module.default || module;

if ({namespace} && typeof {namespace}.scrollToRevealElement === 'function') {{
var targetNode = {element};
if (targetNode) {namespace}.scrollToRevealElement(targetNode);
}}
callback(null);
}})
.catch(err => callback('JS Import Error: ' + err.message));
"""

error_msg = self.driver.execute_async_script(js_str)
if error_msg:
raise RuntimeError(error_msg)


class HomePage(BasePage):
Expand Down
Loading