Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit ca1fd0c

Browse files
committed
Added method for scrolling the window until a certain element is displayed
1 parent 366ce08 commit ca1fd0c

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

webtester-core/src/main/java/info/novatec/testit/webtester/browser/operations/JavaScript.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Browser execute(String script, PageFragment fragment, Object... parameter
5555
* @see JavascriptExecutor#executeScript(String, Object...)
5656
* @since 2.0
5757
*/
58-
public Object executeWithReturn(String script, PageFragment fragment, Object... parameters) {
58+
public <T> T executeWithReturn(String script, PageFragment fragment, Object... parameters) {
5959
Object[] parameterArray = new Object[parameters.length + 1];
6060
parameterArray[0] = fragment.webElement();
6161
System.arraycopy(parameters, 0, parameterArray, 1, parameters.length);
@@ -87,12 +87,12 @@ public Browser execute(String script, Object... parameters) {
8787
* @see JavascriptExecutor#executeScript(String, Object...)
8888
* @since 2.0
8989
*/
90-
public Object executeWithReturn(String script, Object... parameters) {
90+
public <T> T executeWithReturn(String script, Object... parameters) {
9191
if (!(webDriver() instanceof JavascriptExecutor)) {
9292
throw new UnsupportedOperationException("WebDriver does not support JavaScript execution!");
9393
}
9494
JavascriptExecutor javascriptExecutor = ( JavascriptExecutor ) webDriver();
95-
return javascriptExecutor.executeScript(script, parameters);
95+
return (T) javascriptExecutor.executeScript(script, parameters);
9696
}
9797

9898
}

webtester-core/src/main/java/info/novatec/testit/webtester/browser/operations/Window.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import info.novatec.testit.webtester.events.browser.SetWindowPositionEvent;
1717
import info.novatec.testit.webtester.events.browser.SetWindowSizeEvent;
1818
import info.novatec.testit.webtester.internal.ActionTemplate;
19+
import info.novatec.testit.webtester.pagefragments.PageFragment;
1920

2021

2122
/**
@@ -142,6 +143,23 @@ public Browser setSize(int width, int height) {
142143
return browser();
143144
}
144145

146+
/**
147+
* Scrolls the window to the given {@link PageFragment}.
148+
* <p>
149+
* This is done by using the {@code scrollIntoView(true)} JavaScript function on the underlying element.
150+
* Since all JavaScript functionality depends heavily on the used browser this might not work in all environments.
151+
* See <a href="https://developer.mozilla.org/en/docs/Web/API/Element/scrollIntoView">MDN Web API</a> for details.
152+
*
153+
* @param fragment the fragment to scroll into view
154+
* @return the original browser of this operation
155+
* @see JavaScript
156+
* @since 2.0
157+
*/
158+
public Browser scrollTo(PageFragment fragment) {
159+
log.debug("scrolling [{}] into view", fragment.getName().orElse(fragment.toString()));
160+
return browser().javaScript().execute("arguments[0].scrollIntoView(true)", fragment);
161+
}
162+
145163
/**
146164
* Closes the currently focused window. If that window was the last open window, the browser wil be closed as well
147165
* making it unusable in the future. Use this method with care!
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package integration.browser.operations;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
import integration.BaseIntegrationTest;
8+
9+
import info.novatec.testit.webtester.pagefragments.Button;
10+
import info.novatec.testit.webtester.pagefragments.annotations.IdentifyUsing;
11+
import info.novatec.testit.webtester.pages.Page;
12+
13+
14+
public class WindowIntegrationTest extends BaseIntegrationTest {
15+
16+
@Override
17+
protected String getHTMLFilePath() {
18+
return "html/browser/window.html";
19+
}
20+
21+
@Test
22+
public void scrollIntoView() {
23+
Button outOfView = create(TestPage.class).outOfView();
24+
assertThat(elementIsInView()).isFalse();
25+
browser().currentWindow().scrollTo(outOfView);
26+
assertThat(elementIsInView()).isTrue();
27+
}
28+
29+
Boolean elementIsInView() {
30+
return browser().javaScript().executeWithReturn("return isScrolledIntoView()");
31+
}
32+
33+
private interface TestPage extends Page {
34+
35+
@IdentifyUsing("#outOfView")
36+
Button outOfView();
37+
38+
}
39+
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" href="../../_style.css">
5+
<script type="text/javascript">
6+
function isScrolledIntoView() {
7+
var el = document.getElementById('outOfView');
8+
var elemTop = el.getBoundingClientRect().top;
9+
var elemBottom = el.getBoundingClientRect().bottom;
10+
11+
var isVisible = (elemTop >= 0) && (elemBottom <= window.innerHeight);
12+
return isVisible;
13+
}
14+
</script>
15+
</head>
16+
<body>
17+
18+
<!-- Many breaks to move button out of view -->
19+
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
20+
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
21+
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
22+
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
23+
<button id="outOfView" onshow="alert('Foo')">Out of View</button>
24+
<br><br><br><br><br>
25+
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)