simonw / shot-scraper

A command-line utility for taking automated screenshots of websites
https://shot-scraper.datasette.io
Apache License 2.0
1.57k stars 70 forks source link

Failing test: Selector with a wait #134

Closed nielthiart closed 4 months ago

nielthiart commented 6 months ago

Running ./tests/run_examples.sh on my network often fails at the Selector with a wait test. This happens because sometimes two seconds isn't enough for the selected element to load.

Steps to reproduce

  1. Be on a moderately slow internet connection, possibly far from the the server for www.owlsnearme.com.
  2. Run ./tests/run_examples.sh

Alternatively, run the test directly:

# Selector with a wait
shot-scraper 'https://www.owlsnearme.com/?place=127871' \
  --selector 'section.secondary' \
  -o examples/owlsnearme-wait.jpg \
  --wait 2000

Expected result

All tests pass.

Actual result

The Selector with a wait test fails with the following output.

Error: TypeError: Cannot read properties of null (reading 'getBoundingClientRect')
    at eval (eval at evaluate (:226:30), <anonymous>:11:23)
    at Array.forEach (<anonymous>)
    at eval (eval at evaluate (:226:30), <anonymous>:10:9)
    at new Promise (<anonymous>)
    at eval (eval at evaluate (:226:30), <anonymous>:1:1)
    at eval (<anonymous>)
    at UtilityScript.evaluate (<anonymous>:226:30)
    at UtilityScript.<anonymous> (<anonymous>:1:44)

Proposed fix

Replace the --wait 2000 argument with --wait-for "!!document.querySelector('section.secondary')", which should pass if the selected element loads within 30 seconds, but the test runner only waits until the element loads.

nielthiart commented 6 months ago

Realised my first commit for this was not the right solution.

There was already a --wait-for test, so replacing the --wait test with --wait-for was a bad idea.

This commit reuses the --wait-for test's HTML with a delayed div instead.

This way --wait with --selector is still tested. I also added this test to the multi test.