Skip to content

document.body == null exceptions when used in Selenium test failure handler #119

@nsw42

Description

@nsw42

Hi,

selenium-shutterbug v1.6, used with Firefox as part of a Selenium test suite. Selenium 4.36.0, in case that's relevant. We've got a wrapper that takes screenshots at various stages of test execution, and also when a test failure occurs.

We've just hit an issue where a flaky test, lacking a timeout, is reporting org.openqa.selenium.JavascriptException: TypeError: can't access property "scrollWidth", document.body is null

Here's the relevant stacktrace:

java.lang.NullPointerException
 at com.assertthat.selenium_shutterbug.utils.web.Browser.getDocWidth(Browser.java:799)
 at com.assertthat.selenium_shutterbug.utils.web.Browser.takeFullPageScreenshotScroll(Browser.java:206)
 at com.assertthat.selenium_shutterbug.core.Shutterbug.shootPage(Shutterbug.java:163)
 at com.assertthat.selenium_shutterbug.core.Shutterbug.shootPage(Shutterbug.java:83)
 at com.assertthat.selenium_shutterbug.core.Shutterbug.shootPage(Shutterbug.java:69)

That's from a call Shutterbug.shootPage(driver, Capture.FULL_SCROLL).withName(filenameBase).save(outputFolder.getAbsolutePath());

Obviously, the root cause here is the missing timeout in our test, resulting in trying to take a screenshot at an inappropriate time, but it'd be great if shutterbug were tolerant of being asked to take a screenshot of a non-existent page.

I tried adding a check that document.body != null before calling shootPage, but that failed when the browser state changed after the check.

It's difficult to create a minimal example that reproduces this (but I am trying) and I realise that there are probably many code paths that can result in similar errors.

For this particular case, though, I wonder whether it'd be sufficient to make the JS paranoid so that max-document-width.js does something like Math.max(document.body?.scrollWidth ?? 0, document.body?.offsetWidth ?? 0, ...etc...), and similar for max-document-height.js, and then throw an UnableTakeSnapshotException if docWidth == 0 || docHeight == 0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions