Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider sticky header size when scrolling element into view #4819

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion docs/modules/plugins/partials/plugin-web-app-steps.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ When I scroll context to BOTTOM edge

==== Scroll element into view

Scrolls an element into the view.
Scrolls an element into the view with centred positioning.

NOTE: If the element to scroll is located inside an https://developer.mozilla.org/en-US/docs/Web/CSS/overflow[overflow] container then native https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView[scrollIntoView] JS method with top alignment is used.

[source,gherkin]
----
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -62,17 +62,22 @@ public void scrollContextIn(ScrollDirection scrollDirection)
}

/**
* Scroll the element into view by calling API:
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView">Scroll into view</a>
* @param locator to locate an element
* Finds an element and scrolls it into view with centred positioning.
* <br>
* Please note that if the element to scroll is located inside an
* <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/overflow">overflow</a> container then native JS
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView">scrollIntoView</a> method
* with top alignment is used.
*
* @param locator The locator of the element to scroll into view
*/
@When("I scroll element located by `$locator` into view")
public void scrollIntoView(Locator locator)
{
List<WebElement> toScroll = baseValidations.assertIfElementsExist("Element to scroll into view", locator);
if (!toScroll.isEmpty())
List<WebElement> scrollTargets = baseValidations.assertIfElementsExist("Element to scroll into view", locator);
if (!scrollTargets.isEmpty())
{
javascriptActions.scrollIntoView(toScroll.get(0), true);
javascriptActions.scrollElementIntoViewportCenter(scrollTargets.get(0));
uarlouski marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ let currentWindow = window;
const stickyHeaderSize = arguments[1] / 100

try {
if (isElementInsideOverflowContainer(elementToScroll)) {
elementToScroll.scrollIntoView(true);
exit(true);
}

// Check, if we have an access to top level document
window.top.document;
while (currentWindow !== window.top) {
Expand All @@ -30,4 +35,16 @@ function clearTimeoutAndWait(event) {
wait();
}

function isElementInsideOverflowContainer(element) {
let container = element.parentElement;
while (container) {
const style = window.getComputedStyle(container);
if (style.overflow !== 'visible' && (style.overflowX !== 'visible' || style.overflowY !== 'visible')) {
return true;
}
container = container.parentElement;
}
return false;
}

wait();
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -126,14 +126,14 @@ void shouldThorowExceptionForScrollContextInRightDirectionWhenContextIsPage()
}

@Test
void shouldScrollElementIntoViewAlignedToATop()
void shouldScrollElementIntoView()
{
var webElement = mock(WebElement.class);
var locator = mock(Locator.class);
when(baseValidations.assertIfElementsExist(ELEMENT_TO_SCROLL_INTO_VIEW, locator)).thenReturn(
List.of(webElement));
scrollSteps.scrollIntoView(locator);
verify(javascriptActions).scrollIntoView(webElement, true);
verify(javascriptActions).scrollElementIntoViewportCenter(webElement);
}

@Test
Expand Down
Loading