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
This commit is contained in:
Henrik Skupin 2019-08-26 07:38:49 +00:00
Родитель d6c05078f8
Коммит 3f9a910ba6
8 изменённых файлов: 167 добавлений и 14 удалений

Просмотреть файл

@ -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)];
"""))

Просмотреть файл

@ -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 = """
<style>
div, iframe {
display: block;
border: 1px solid blue;
width: 10em;
height: 10em;
}
</style>
"""
DEFAULT_CONTENT = "<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>"
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 = "<style>body {{ margin: 0; }}</style>{}".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

Просмотреть файл

@ -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,))

Просмотреть файл

@ -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 = """
<style>
div, iframe {
display: block;
border: 1px solid blue;
width: 10em;
height: 10em;
}
</style>
"""
DEFAULT_CONTENT = "<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>"
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 = "<style>body {{ margin: 0; }}</style>{}".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

Просмотреть файл

@ -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"])

Просмотреть файл

@ -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)
];
"""))

Просмотреть файл

@ -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 = """
<style>
div, iframe {
display: block;
border: 1px solid blue;
width: 10em;
height: 10em;
}
</style>
"""
DEFAULT_CONTENT = "<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>"
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 = "<style>body {{ margin: 0; }}</style>{}".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

Просмотреть файл

@ -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)