tests: a refused screenshot of an element or a locator neither scrolls nor measures the page (red: the refusal came after the scroll)

ElementHandleDispatcher.op_screenshot scrolled the element into view, took its box and read the document's scroll before handing the call to the page's
screenshot, where the refusal of mask, style, animations="disabled", caret="initial" and omitBackground with png is. A refused call therefore moved the page. Unit:
for each refused parameter the element's call sends nothing to the engine, not a scroll, not a measure, not a picture (the page's call is held to the same, and the
call that is taken still scrolls and measures first). E2E: on a page scrolled to a known position with the element below the fold, a refused locator.screenshot and
element.screenshot leave window.scrollY as it was; the same call without the parameter does scroll.
This commit is contained in:
feder-cr committed 2026-11-26 12:45:09 +01:00
1 parent 3b362bab4b
commit 2206289d7e
2 files changed
+86

No files matched your search

@@ -159,3 +159,40 @@ def test_the_first_refusal_wins_and_nothing_is_sent_when_several_are_asked():
with pytest.raises(ProtocolException):
page.op_screenshot({"mask": MASK, "style": "p {}", "animations": "disabled", "caret": "initial", "omitBackground": True})
page.send.assert_not_called()
# ── the refusal comes FIRST: a refused element's or locator's call neither scrolls nor measures the page ────────────────────
REFUSED_ELEMENT = REFUSED + [({"omitBackground": True}, "omitBackground")]
REFUSED_ELEMENT_IDS = REFUSED_IDS + ["omit_background_png"]
@pytest.mark.parametrize("params,name", REFUSED_ELEMENT, ids=REFUSED_ELEMENT_IDS)
def test_a_refused_element_screenshot_sends_nothing_to_the_engine_not_even_a_scroll_or_a_measure(params, name):
page = make_page()
handle = ElementHandleDispatcher(page.server, page.frame, "object-1")
page.injected.bounding_box.return_value = {"x": 10, "y": 20, "width": 30, "height": 40}
with pytest.raises(ProtocolException):
handle.op_screenshot(params)
# the scroll (`scroll_into_view`), the box (`bounding_box`) and the document's scroll (`evaluate`) all go through `injected`,
# the picture through `send`: not one of them was called
assert page.injected.mock_calls == []
page.send.assert_not_called()
@pytest.mark.parametrize("params,name", REFUSED_ELEMENT, ids=REFUSED_ELEMENT_IDS)
def test_a_refused_page_screenshot_measures_nothing_either(params, name):
page = make_page()
with pytest.raises(ProtocolException):
page.op_screenshot(params)
assert page.injected.mock_calls == []
page.send.assert_not_called()
def test_the_element_screenshot_that_is_taken_still_scrolls_and_measures_first():
page = make_page()
page.injected.bounding_box.return_value = {"x": 10, "y": 20, "width": 30, "height": 40}
handle = ElementHandleDispatcher(page.server, page.frame, "object-1")
handle.op_screenshot({})
names = [call[0] for call in page.injected.mock_calls]
assert names[:2] == ["scroll_into_view", "bounding_box"]
assert page.send.call_args[0][0] == "Page.screenshot"
@@ -158,3 +158,52 @@ def test_no_caret_is_in_the_picture_when_the_caller_does_not_write_caret(page):
def test_no_caret_is_in_the_picture_with_caret_hide(page):
assert [_red_pixels(png) for png in _caret_shots(page, caret="hide")] == [0, 0, 0]
# ── a refused call does not touch the page's scroll: the refusal comes before the element is scrolled into view ────────────────
TALL = (
"<!doctype html><html style='background:#fff'><body style='margin:0;background:#fff'>"
"<div style='height:3000px;background:#eee'>above the fold</div>"
"<p id='below' style='margin:0;width:200px;height:40px;background:#0a0'>below the fold</p>"
"<div style='height:3000px;background:#eee'>after</div>"
"</body></html>"
)
SCROLLED_TO = 100
@pytest.fixture
def scrolled(page):
"""A page scrolled to a known position with the element well below the fold."""
other = page.context.new_page()
other.set_content(TALL, wait_until="load")
other.evaluate("window.scrollTo(0, %d)" % SCROLLED_TO)
assert other.evaluate("window.scrollY") == SCROLLED_TO
assert other.evaluate("document.getElementById('below').getBoundingClientRect().top") > 400
yield other
other.close()
REFUSALS = [
("mask", r"screenshot\(mask=", lambda p: {"mask": [p.locator("#below")]}),
("style", r"screenshot\(style=", lambda p: {"style": "p { visibility: hidden }"}),
("animations", r'screenshot\(animations="disabled"\)', lambda p: {"animations": "disabled"}),
("caret", r'screenshot\(caret="initial"\)', lambda p: {"caret": "initial"}),
("omit_background", r"^Not implemented", lambda p: {"omit_background": True}),
]
@pytest.mark.parametrize("name,pattern,kwargs", REFUSALS, ids=[r[0] for r in REFUSALS])
@pytest.mark.parametrize("way", ["locator", "handle"])
def test_a_refused_element_screenshot_does_not_scroll_the_page(scrolled, way, name, pattern, kwargs):
from invisible_playwright.sync_api import Error
shoot = {
"locator": lambda **k: scrolled.locator("#below").screenshot(**k),
"handle": lambda **k: scrolled.query_selector("#below").screenshot(**k),
}[way]
with pytest.raises(Error, match=pattern):
shoot(**kwargs(scrolled))
assert scrolled.evaluate("window.scrollY") == SCROLLED_TO
# the control: the same call without the refused parameter DOES scroll the element in, so the unchanged scroll above is a measurement
assert shoot()[:4] == PNG
assert scrolled.evaluate("window.scrollY") != SCROLLED_TO