From 3f9a910ba6943e5954dd618691415213b287741e Mon Sep 17 00:00:00 2001 From: Henrik Skupin Date: Mon, 26 Aug 2019 07:38:49 +0000 Subject: [PATCH] Bug 1559592 - [wdspec] Add same and cross origin iframe tests for "Take (Element) Screenshot". r=webdriver-reviewers,automatedtester,ato Differential Revision: https://phabricator.services.mozilla.com/D42132 --HG-- extra : moz-landing-system : lando --- .../take_full_screenshot/__init__.py | 1 + .../webdriver/take_full_screenshot/iframe.py | 47 ++++++++++++++++ .../tests/take_element_screenshot/__init__.py | 12 ++--- .../tests/take_element_screenshot/iframe.py | 54 +++++++++++++++++++ .../take_element_screenshot/screenshot.py | 7 ++- .../tests/take_screenshot/__init__.py | 11 +++- .../webdriver/tests/take_screenshot/iframe.py | 45 ++++++++++++++++ .../tests/take_screenshot/screenshot.py | 4 +- 8 files changed, 167 insertions(+), 14 deletions(-) create mode 100644 testing/web-platform/mozilla/tests/webdriver/take_full_screenshot/iframe.py create mode 100644 testing/web-platform/tests/webdriver/tests/take_element_screenshot/iframe.py create mode 100644 testing/web-platform/tests/webdriver/tests/take_screenshot/iframe.py diff --git a/testing/web-platform/mozilla/tests/webdriver/take_full_screenshot/__init__.py b/testing/web-platform/mozilla/tests/webdriver/take_full_screenshot/__init__.py index d38c8a0f79f8..fa86372a78e8 100644 --- a/testing/web-platform/mozilla/tests/webdriver/take_full_screenshot/__init__.py +++ b/testing/web-platform/mozilla/tests/webdriver/take_full_screenshot/__init__.py @@ -2,5 +2,6 @@ def document_dimensions(session): return tuple(session.execute_script(""" let {devicePixelRatio} = window; let {width, height} = document.documentElement.getBoundingClientRect(); + return [Math.floor(width * devicePixelRatio), Math.floor(height * devicePixelRatio)]; """)) diff --git a/testing/web-platform/mozilla/tests/webdriver/take_full_screenshot/iframe.py b/testing/web-platform/mozilla/tests/webdriver/take_full_screenshot/iframe.py new file mode 100644 index 000000000000..42fad622aa0f --- /dev/null +++ b/testing/web-platform/mozilla/tests/webdriver/take_full_screenshot/iframe.py @@ -0,0 +1,47 @@ +import pytest + +from tests.support.asserts import assert_success +from tests.support.image import png_dimensions +from tests.support.inline import iframe, inline + +from . import document_dimensions + + +DEFAULT_CSS_STYLE = """ + +""" + +DEFAULT_CONTENT = "
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
" + + +def take_full_screenshot(session): + return session.transport.send( + "GET", + "/session/{session_id}/moz/screenshot/full".format( + session_id=session.session_id)) + + +@pytest.mark.parametrize("domain", ["", "alt"], ids=["same_origin", "cross_origin"]) +def test_source_origin(session, url, domain): + session.url = inline("""{0}{1}""".format(DEFAULT_CSS_STYLE, DEFAULT_CONTENT)) + + response = take_full_screenshot(session) + reference_screenshot = assert_success(response) + assert png_dimensions(reference_screenshot) == document_dimensions(session) + + iframe_content = "{}".format(DEFAULT_CONTENT) + session.url = inline("""{0}{1}""".format( + DEFAULT_CSS_STYLE, iframe(iframe_content, domain=domain))) + + response = take_full_screenshot(session) + screenshot = assert_success(response) + assert png_dimensions(screenshot) == document_dimensions(session) + + assert screenshot == reference_screenshot diff --git a/testing/web-platform/tests/webdriver/tests/take_element_screenshot/__init__.py b/testing/web-platform/tests/webdriver/tests/take_element_screenshot/__init__.py index cf3b21905b77..9de879246079 100644 --- a/testing/web-platform/tests/webdriver/tests/take_element_screenshot/__init__.py +++ b/testing/web-platform/tests/webdriver/tests/take_element_screenshot/__init__.py @@ -1,12 +1,12 @@ def element_rect(session, element): return session.execute_script(""" - let devicePixelRatio = window.devicePixelRatio; - let rect = arguments[0].getBoundingClientRect(); + let {devicePixelRatio} = window; + let {left, top, width, height} = arguments[0].getBoundingClientRect(); return { - x: Math.floor((rect.left + window.pageXOffset) * devicePixelRatio), - y: Math.floor((rect.top + window.pageYOffset) * devicePixelRatio), - width: Math.floor(rect.width * devicePixelRatio), - height: Math.floor(rect.height * devicePixelRatio), + x: Math.floor((left + window.pageXOffset) * devicePixelRatio), + y: Math.floor((top + window.pageYOffset) * devicePixelRatio), + width: Math.floor(width * devicePixelRatio), + height: Math.floor(height * devicePixelRatio), }; """, args=(element,)) diff --git a/testing/web-platform/tests/webdriver/tests/take_element_screenshot/iframe.py b/testing/web-platform/tests/webdriver/tests/take_element_screenshot/iframe.py new file mode 100644 index 000000000000..d081a14cdeb5 --- /dev/null +++ b/testing/web-platform/tests/webdriver/tests/take_element_screenshot/iframe.py @@ -0,0 +1,54 @@ +import pytest + +from tests.support.asserts import assert_success +from tests.support.image import png_dimensions +from tests.support.inline import iframe, inline + +from . import element_rect + + +DEFAULT_CSS_STYLE = """ + +""" + +DEFAULT_CONTENT = "
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
" + + +def take_element_screenshot(session, element_id): + return session.transport.send( + "GET", + "session/{session_id}/element/{element_id}/screenshot".format( + session_id=session.session_id, + element_id=element_id, + ) + ) + + +@pytest.mark.parametrize("domain", ["", "alt"], ids=["same_origin", "cross_origin"]) +def test_source_origin(session, url, domain): + session.url = inline("""{0}{1}""".format(DEFAULT_CSS_STYLE, DEFAULT_CONTENT)) + element = session.find.css("div", all=False) + rect = element_rect(session, element) + + response = take_element_screenshot(session, element.id) + reference_screenshot = assert_success(response) + assert png_dimensions(reference_screenshot) == (rect["width"], rect["height"]) + + iframe_content = "{}".format(DEFAULT_CONTENT) + session.url = inline("""{0}{1}""".format( + DEFAULT_CSS_STYLE, iframe(iframe_content, domain=domain))) + frame_element = session.find.css("iframe", all=False) + frame_rect = element_rect(session, frame_element) + + response = take_element_screenshot(session, frame_element.id) + screenshot = assert_success(response) + assert png_dimensions(screenshot) == (frame_rect["width"], frame_rect["height"]) + + assert screenshot == reference_screenshot diff --git a/testing/web-platform/tests/webdriver/tests/take_element_screenshot/screenshot.py b/testing/web-platform/tests/webdriver/tests/take_element_screenshot/screenshot.py index 35a1ffe75f04..fd460b656ae3 100644 --- a/testing/web-platform/tests/webdriver/tests/take_element_screenshot/screenshot.py +++ b/testing/web-platform/tests/webdriver/tests/take_element_screenshot/screenshot.py @@ -1,4 +1,4 @@ -from tests.support.asserts import assert_error, assert_png, assert_success +from tests.support.asserts import assert_error, assert_success from tests.support.image import png_dimensions from tests.support.inline import inline @@ -35,7 +35,6 @@ def test_format_and_dimensions(session): rect = element_rect(session, element) response = take_element_screenshot(session, element.id) - value = assert_success(response) + screenshot = assert_success(response) - assert_png(value) - assert png_dimensions(value) == (rect["width"], rect["height"]) + assert png_dimensions(screenshot) == (rect["width"], rect["height"]) diff --git a/testing/web-platform/tests/webdriver/tests/take_screenshot/__init__.py b/testing/web-platform/tests/webdriver/tests/take_screenshot/__init__.py index fe057a4932ef..c07f8d167030 100644 --- a/testing/web-platform/tests/webdriver/tests/take_screenshot/__init__.py +++ b/testing/web-platform/tests/webdriver/tests/take_screenshot/__init__.py @@ -1,2 +1,9 @@ -def document_dimensions(session): - return tuple(session.execute_script("return [window.innerWidth, window.innerHeight];")) +def viewport_dimensions(session): + return tuple(session.execute_script(""" + let {devicePixelRatio, innerHeight, innerWidth} = window; + + return [ + Math.floor(innerWidth * devicePixelRatio), + Math.floor(innerHeight * devicePixelRatio) + ]; + """)) diff --git a/testing/web-platform/tests/webdriver/tests/take_screenshot/iframe.py b/testing/web-platform/tests/webdriver/tests/take_screenshot/iframe.py new file mode 100644 index 000000000000..5206f2fa1736 --- /dev/null +++ b/testing/web-platform/tests/webdriver/tests/take_screenshot/iframe.py @@ -0,0 +1,45 @@ +import pytest + +from tests.support.asserts import assert_success +from tests.support.image import png_dimensions +from tests.support.inline import iframe, inline + +from . import viewport_dimensions + + +DEFAULT_CSS_STYLE = """ + +""" + +DEFAULT_CONTENT = "
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
" + + +def take_screenshot(session): + return session.transport.send( + "GET", "session/{session_id}/screenshot".format(**vars(session))) + + +@pytest.mark.parametrize("domain", ["", "alt"], ids=["same_origin", "cross_origin"]) +def test_source_origin(session, url, domain): + session.url = inline("""{0}{1}""".format(DEFAULT_CSS_STYLE, DEFAULT_CONTENT)) + + response = take_screenshot(session) + reference_screenshot = assert_success(response) + assert png_dimensions(reference_screenshot) == viewport_dimensions(session) + + iframe_content = "{}".format(DEFAULT_CONTENT) + session.url = inline("""{0}{1}""".format( + DEFAULT_CSS_STYLE, iframe(iframe_content, domain=domain))) + + response = take_screenshot(session) + screenshot = assert_success(response) + assert png_dimensions(screenshot) == viewport_dimensions(session) + + assert screenshot == reference_screenshot diff --git a/testing/web-platform/tests/webdriver/tests/take_screenshot/screenshot.py b/testing/web-platform/tests/webdriver/tests/take_screenshot/screenshot.py index 07879530120d..ca6e0272637d 100644 --- a/testing/web-platform/tests/webdriver/tests/take_screenshot/screenshot.py +++ b/testing/web-platform/tests/webdriver/tests/take_screenshot/screenshot.py @@ -2,7 +2,7 @@ from tests.support.asserts import assert_error, assert_png, assert_success from tests.support.image import png_dimensions from tests.support.inline import inline -from . import document_dimensions +from . import viewport_dimensions def take_screenshot(session): @@ -22,4 +22,4 @@ def test_format_and_dimensions(session): value = assert_success(response) assert_png(value) - assert png_dimensions(value) == document_dimensions(session) + assert png_dimensions(value) == viewport_dimensions(session)