Saturday 24 June 2023

Dynamic JS content not loading when testing with Selenium Chrome headless browser in Java

I'm trying to test a website I made as part of a Spring Boot project, and I'm using Selenium. I'm able to test basic stuff like loading the page and the title, but I'm struggling with the actual content of the page.

My page has the following section:

<div id="main">
    <div id="div_1"></div>
    <div id="div_2"></div>
    <div id="div_3"></div>
    <div id="div_4"></div>
</div>

The content is loaded from a JS script:

document.addEventListener("DOMContentLoaded", function(event) {
    populateDivs()
})

I'm initialising the WebDriver with the following options gathered from similar questions (other Selenium tests continue passing, so I don't think the options are conflicting):

final ChromeOptions options = new ChromeOptions();
options.addArguments(
    "--headless",
    "--nogpu",
    "--disable-gpu",
    "--enable-javascript",
    "--no-sandbox",
    "--disable-extensions",
    "--disable-blink-features=AutomationControlled",
    "--disable-features=NetworkService,NetworkServiceInProcess",
     "start-maximized",
     "disable-infobars"
);

I've also added a wait in my test case to give time for the content to load:

final WebDriverWait wait = new WebDriverWait(driver, Duration.ofMinutes(1L));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("div_1_content")));

Nothing I try changes the fact that the no content is loaded even after waiting. Not really sure where to go from here - am I using Selenium incorrectly? Or should I load my content in a different way on the JS side?

Thanks!



from Dynamic JS content not loading when testing with Selenium Chrome headless browser in Java

No comments:

Post a Comment