Bug 1840999 - Add gecko specific tests for "origin" argument for "browsingContext.captureScreenshot" command. r=webdriver-reviewers,whimboo

Depends on D192455

Differential Revision: https://phabricator.services.mozilla.com/D192456
This commit is contained in:
Alexandra Borovova 2023-11-07 10:52:55 +00:00
parent 84b8e7b212
commit d2f4714466
4 changed files with 212 additions and 0 deletions

View File

@ -0,0 +1,31 @@
from math import floor
from tests.bidi import get_device_pixel_ratio, remote_mapping_to_dict
from webdriver.bidi.modules.script import ContextTarget
async def get_document_dimensions(bidi_session, context: str):
expression = """
({
height: document.documentElement.scrollHeight,
width: document.documentElement.scrollWidth,
});
"""
result = await bidi_session.script.evaluate(
expression=expression,
target=ContextTarget(context["context"]),
await_promise=False,
)
return remote_mapping_to_dict(result["value"])
async def get_physical_document_dimensions(bidi_session, context):
"""Get the physical dimensions of the context's document.
:param bidi_session: BiDiSession
:param context: Browsing context ID
:returns: Tuple of (int, int) containing document width, document height.
"""
document = await get_document_dimensions(bidi_session, context)
dpr = await get_device_pixel_ratio(bidi_session, context)
return (floor(document["width"] * dpr), floor(document["height"] * dpr))

View File

@ -0,0 +1,104 @@
import pytest
from tests.bidi import get_viewport_dimensions
from tests.bidi.browsing_context.capture_screenshot import (
get_element_coordinates,
get_physical_element_dimensions,
get_reference_screenshot,
)
from tests.support.image import png_dimensions
from webdriver.bidi.modules.browsing_context import BoxOptions, ElementOptions
from webdriver.bidi.modules.script import ContextTarget
pytestmark = pytest.mark.asyncio
async def test_params_clip_element_with_document_origin(
bidi_session, top_context, inline, compare_png_bidi
):
element_styles = "background-color: black; width: 50px; height:50px;"
# Render an element inside of viewport for the reference.
reference_data = await get_reference_screenshot(
bidi_session,
inline,
top_context["context"],
f"""<div style="{element_styles}"></div>""",
)
viewport_dimensions = await get_viewport_dimensions(bidi_session, top_context)
# Render the same element outside of viewport.
url = inline(
f"""<div style="{element_styles} margin-top: {viewport_dimensions["height"]}px"></div>"""
)
await bidi_session.browsing_context.navigate(
context=top_context["context"], url=url, wait="complete"
)
element = await bidi_session.script.evaluate(
await_promise=False,
expression="document.querySelector('div')",
target=ContextTarget(top_context["context"]),
)
expected_size = await get_physical_element_dimensions(
bidi_session, top_context, element
)
data = await bidi_session.browsing_context.capture_screenshot(
context=top_context["context"],
clip=ElementOptions(element=element),
origin="document",
)
assert png_dimensions(data) == expected_size
comparison = await compare_png_bidi(reference_data, data)
assert comparison.equal()
async def test_params_clip_box_with_document_origin(
bidi_session, top_context, inline, compare_png_bidi
):
element_styles = "background-color: black; width: 50px; height:50px;"
# Render an element inside of viewport for the reference.
reference_data = await get_reference_screenshot(
bidi_session,
inline,
top_context["context"],
f"""<div style="{element_styles}"></div>""",
)
viewport_dimensions = await get_viewport_dimensions(bidi_session, top_context)
# Render the same element outside of viewport.
url = inline(
f"""<div style="{element_styles} margin-top: {viewport_dimensions["height"]}px"></div>"""
)
await bidi_session.browsing_context.navigate(
context=top_context["context"], url=url, wait="complete"
)
element = await bidi_session.script.call_function(
await_promise=False,
function_declaration="""() => document.querySelector('div')""",
target=ContextTarget(top_context["context"]),
)
element_coordinates = await get_element_coordinates(
bidi_session, top_context, element
)
element_dimensions = await get_physical_element_dimensions(
bidi_session, top_context, element
)
data = await bidi_session.browsing_context.capture_screenshot(
context=top_context["context"],
clip=BoxOptions(
x=element_coordinates[0],
y=element_coordinates[1],
width=element_dimensions[0],
height=element_dimensions[1],
),
origin="document",
)
assert png_dimensions(data) == element_dimensions
comparison = await compare_png_bidi(reference_data, data)
assert comparison.equal()

View File

@ -0,0 +1,19 @@
import pytest
from webdriver.bidi import error
pytestmark = pytest.mark.asyncio
@pytest.mark.parametrize("value", [False, 42, [], {}])
async def test_params_origin_invalid_type(bidi_session, top_context, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.browsing_context.capture_screenshot(
context=top_context["context"], origin=value
)
async def test_params_origin_invalid_value(bidi_session, top_context):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.browsing_context.capture_screenshot(
context=top_context["context"], origin="test"
)

View File

@ -0,0 +1,58 @@
import pytest
from tests.bidi.browsing_context.capture_screenshot import (
get_physical_viewport_dimensions,
)
from tests.support.image import png_dimensions
from . import get_physical_document_dimensions
pytestmark = pytest.mark.asyncio
async def test_origin(bidi_session, top_context, inline):
await bidi_session.browsing_context.navigate(
context=top_context["context"],
url=inline("<div style='margin-top:2000px'>foo</div>"),
wait="complete",
)
viewport_dimensions = await get_physical_viewport_dimensions(
bidi_session, top_context
)
document_dimensions = await get_physical_document_dimensions(
bidi_session, top_context
)
assert not viewport_dimensions == document_dimensions
document_screenshot = await bidi_session.browsing_context.capture_screenshot(
context=top_context["context"], origin="document"
)
viewport_screenshot = await bidi_session.browsing_context.capture_screenshot(
context=top_context["context"], origin="viewport"
)
assert png_dimensions(document_screenshot) == document_dimensions
assert png_dimensions(viewport_screenshot) == viewport_dimensions
@pytest.mark.parametrize("origin", ["document", "viewport"])
async def test_origin_consistency(bidi_session, top_context, inline, origin):
await bidi_session.browsing_context.navigate(
context=top_context["context"],
url=inline("<div style='margin-top:2000px'>foo</div>"),
wait="complete",
)
screenshot_a = await bidi_session.browsing_context.capture_screenshot(
context=top_context["context"], origin=origin
)
await bidi_session.browsing_context.navigate(
context=top_context["context"],
url=inline("<div style='margin-top:2000px'>foo</div>"),
wait="complete",
)
screenshot_b = await bidi_session.browsing_context.capture_screenshot(
context=top_context["context"], origin=origin
)
assert screenshot_a == screenshot_b