Skip to content

Commit

Permalink
Merge 99c32d1 into 014b738
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelpp committed Dec 18, 2017
2 parents 014b738 + 99c32d1 commit 97a09cb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/codeborne/selenide/Selenide.java
Expand Up @@ -831,6 +831,15 @@ private static List<LogEntry> getLogEntries(String logType, Level logLevel) {
}
}

/**
* Return true if bottom of the page is reached
*
* Useful if you need to scroll down by x pixels unknown number of times.
*/
public static boolean atBottom() {
return Selenide.executeJavaScript("return window.scrollY + window.innerHeight >= document.body.scrollHeight");
}

private static <T> List<String> listToString(List<T> objects) {
if (objects == null || objects.isEmpty()) {
return emptyList();
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/integration/PageAtBottomTest.java
@@ -0,0 +1,39 @@
package integration;

import com.codeborne.selenide.Selenide;
import org.junit.Before;
import org.junit.Test;

import static com.codeborne.selenide.Selenide.atBottom;
import static com.codeborne.selenide.WebDriverRunner.isHtmlUnit;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;

public class PageAtBottomTest extends IntegrationTest {
@Before
public void createScrollablePage() {
assumeFalse("Scrolling not supported in htmlunit", isHtmlUnit());
openFile("empty.html");
for (int i = 0; i < 200; i++) {
Selenide.executeJavaScript(
"var el = document.createElement(\"li\");" +
"el.textContent=\"Element " + (i + 1) + "\";" +
"document.body.appendChild(el);"
);
}
}

@Test
public void checkPageBottomIsReached() {
assertFalse("oops, we shouldn't be at the bottom yet! " + printScrollParams(), atBottom());
Selenide.executeJavaScript("return window.scrollTo(0, document.body.scrollHeight);");
assertTrue("oops we should have reached the bottom already! " + printScrollParams(), atBottom());
}

private String printScrollParams() {
return "\nwindow.scrollY=" + Selenide.executeJavaScript("return window.scrollY;") +
"\nwindow.innerHeight=" + Selenide.executeJavaScript("return window.innerHeight;") +
"\ndocument.body.scrollHeight=" + Selenide.executeJavaScript("return document.body.scrollHeight;");
}
}

0 comments on commit 97a09cb

Please sign in to comment.