Skip to content

Commit

Permalink
webdriver: add screenshot test utilities
Browse files Browse the repository at this point in the history
This adds some test utilities to the WebDriver WPT tests for dealing
with screenshots and document dimensions.

Depends on D6886

Differential Revision: https://phabricator.services.mozilla.com/D6887

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1431148
gecko-commit: e30ce73ca4f120f38d619c1b626ff9f91f48a2f4
gecko-integration-branch: autoland
gecko-reviewers: whimboo
  • Loading branch information
andreastt authored and moz-wptsync-bot committed Oct 29, 2018
1 parent f105c05 commit ee3ff96
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions webdriver/tests/support/asserts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import base64
import imghdr

from webdriver import Element, NoSuchAlertException, WebDriverException


Expand Down Expand Up @@ -189,3 +192,10 @@ def assert_move_to_coordinates(point, target, events):
assert e["pageX"] == point["x"]
assert e["pageY"] == point["y"]
assert e["target"] == target


def assert_png(screenshot):
"""Test that screenshot is a Base64 encoded PNG file."""
image = base64.decodestring(screenshot)
mime_type = imghdr.what("", image)
assert mime_type == "png", "Expected image to be PNG, but it was {}".format(mime_type)
8 changes: 8 additions & 0 deletions webdriver/tests/support/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,11 @@ def is_element_in_viewport(session, element):
return !(rect.right < 0 || rect.bottom < 0 ||
rect.left > viewport.width || rect.top > viewport.height)
""", args=(element,))


def document_dimensions(session):
return tuple(session.execute_script("""
let {devicePixelRatio} = window;
let {width, height} = document.documentElement.getBoundingClientRect();
return [width * devicePixelRatio, height * devicePixelRatio];
"""))
11 changes: 11 additions & 0 deletions webdriver/tests/support/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import base64
import struct

from tests.support.asserts import assert_png


def png_dimensions(screenshot):
assert_png(screenshot)
image = base64.decodestring(screenshot)
width, height = struct.unpack(">LL", image[16:24])
return int(width), int(height)

0 comments on commit ee3ff96

Please sign in to comment.