skyhirider · GitHub

I've ran into a case where triggering javascript scrollintoView has no effect, not triggering an event that is supposed to dynamically load the next page of data.

Here is a basic test case:

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
public class ScrollTest {
    public static final String PAGE;
    static {
        try {
            PAGE = Files.readString(Path.of(ScrollTest.class.getClassLoader().getResource("testPage1.html").toURI()));
        } catch (IOException | URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
    private static final HtmlUnitDriver huDriver = new HtmlUnitDriver(true);
    private static final WebDriver chromeDriver = new ChromeDriver();
    @AfterAll
    static void closeAll(){
        huDriver.close();
        chromeDriver.close();
    }
    @Test
    void givenPageWithTable_whenScrollTo_TriggerEvent(){
        runTest(chromeDriver);
        runTest(huDriver);
    }
    private static void runTest(WebDriver driver) {
        driver.get(PAGE);
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView()", driver.findElement(By.id("last")));
        Assertions.assertEquals(1L,
                ((JavascriptExecutor) driver).executeScript("return count"));
    }
}

and here is the html file I used for testing, feel free to add more table rows if needed

data:text/html,
<table>
    <tr>row</tr>
    <br>
    <tr id="last"></tr>
</table>
<script>
    var count = 0;
    const observer = new IntersectionObserver((entries) => {
    entries.forEach((entry) => {
        console.log("triggered");
        count++;
    })
});
observer.observe(document.getElementById("last"));
</script>

The results are incosistent:

org.opentest4j.AssertionFailedError:
Expected :1
Actual   :0
<Click to see difference>
at ScrollTest.runTest(ScrollTest.java:45)
at ScrollTest.givenPageWithTable_whenScrollTo_TriggerEvent(ScrollTest.java:39)
...removed rest of generic stack trace...

Read the original on github.com ↗