diff --git a/wrolpi/cmd.py b/wrolpi/cmd.py index bb1a10b8..8e135594 100644 --- a/wrolpi/cmd.py +++ b/wrolpi/cmd.py @@ -79,6 +79,9 @@ CHROMIUM = which('chromium-browser', 'chromium', '/usr/bin/chromium-browser', # rpi os '/usr/bin/chromium', # debian ) +CHROMEDRIVER_BIN = which('chromedriver', + '/usr/bin/chromedriver', # rpi os / debian + ) FIREFOX = which('firefox', 'firefox-esr', '/usr/bin/firefox', '/usr/bin/firefox-esr', diff --git a/wrolpi/common.py b/wrolpi/common.py index d5b8e18d..7356b017 100644 --- a/wrolpi/common.py +++ b/wrolpi/common.py @@ -2544,17 +2544,23 @@ def format_html_file(file: pathlib.Path): def html_screenshot(html: bytes | str) -> bytes: """Return a PNG screenshot of the provided HTML.""" + from wrolpi import cmd # Avoid circular import. + # Set Chromium to headless. Use a wide window size so that screenshot will be the "desktop" version of the page. options = webdriver.ChromeOptions() options.add_argument('headless') options.add_argument('disable-gpu') options.add_argument('window-size=1280x720') + # Use the system chromedriver; Selenium Manager downloads drivers from the internet, and has no binary for + # linux/aarch64 (Raspberry Pi). + service = webdriver.ChromeService(executable_path=str(cmd.CHROMEDRIVER_BIN)) if cmd.CHROMEDRIVER_BIN else None + with tempfile.NamedTemporaryFile('wb', suffix='.html') as fh: fh.write(html.encode() if isinstance(html, str) else html) fh.flush() - with webdriver.Chrome(options=options) as driver: + with webdriver.Chrome(options=options, service=service) as driver: driver.get(f'file://{fh.name}') driver.set_window_size(1280, 720) screenshot = driver.get_screenshot_as_png() diff --git a/wrolpi/test/test_common.py b/wrolpi/test/test_common.py index 7f9c0fb0..b4705608 100644 --- a/wrolpi/test/test_common.py +++ b/wrolpi/test/test_common.py @@ -894,15 +894,32 @@ def test_html_screenshot(singlefile_contents_factory): def test_html_screenshot_webdriver_kwargs(singlefile_contents_factory): - """Selenium 4.10 removed the `chrome_options` kwarg; `webdriver.Chrome` must be called with `options`.""" - with mock.patch('wrolpi.common.webdriver.Chrome') as mock_chrome: + """Selenium 4.10 removed the `chrome_options` kwarg; `webdriver.Chrome` must be called with `options`. + + The system chromedriver must be passed explicitly because Selenium Manager downloads drivers from the + internet, and has no binary for linux/aarch64 (Raspberry Pi).""" + contents = singlefile_contents_factory() + + with mock.patch('wrolpi.common.webdriver.Chrome') as mock_chrome, \ + mock.patch('wrolpi.cmd.CHROMEDRIVER_BIN', pathlib.Path('/usr/bin/chromedriver')): driver = mock_chrome.return_value.__enter__.return_value driver.get_screenshot_as_png.return_value = b'PNG' - assert common.html_screenshot(singlefile_contents_factory()) == b'PNG' + assert common.html_screenshot(contents) == b'PNG' - assert mock_chrome.call_args.kwargs.keys() == {'options'} + assert mock_chrome.call_args.kwargs.keys() == {'options', 'service'} assert isinstance(mock_chrome.call_args.kwargs['options'], common.webdriver.ChromeOptions) + assert mock_chrome.call_args.kwargs['service'].path == '/usr/bin/chromedriver' + + # Without a system chromedriver (development machines), fall back to Selenium Manager. + with mock.patch('wrolpi.common.webdriver.Chrome') as mock_chrome, \ + mock.patch('wrolpi.cmd.CHROMEDRIVER_BIN', None): + driver = mock_chrome.return_value.__enter__.return_value + driver.get_screenshot_as_png.return_value = b'PNG' + + assert common.html_screenshot(contents) == b'PNG' + + assert mock_chrome.call_args.kwargs['service'] is None @pytest.mark.parametrize('color,expected', [