Skip to content

Commit

Permalink
test_print: add save_pdf method for debugging (#842)
Browse files Browse the repository at this point in the history
Bug: #518
  • Loading branch information
Thiago Perrotta authored Jun 13, 2023
1 parent 73da055 commit ac9d8be
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 6 additions & 3 deletions tests/screenshot/test_screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def assert_images_equal(img1: Image, img2: Image):
def save_png(png_bytes_or_str: bytes | str, output_file: str):
"""Save the given PNG (bytes or base64 string representation) to the given output file."""
png_bytes = png_bytes_or_str if isinstance(
png_bytes_or_str, bytes) else base64.b64decode(png_bytes_or_str)
png_bytes_or_str, bytes) else base64.b64decode(png_bytes_or_str,
validate=True)
Image.open(io.BytesIO(png_bytes)).save(output_file, 'PNG')


Expand All @@ -57,7 +58,8 @@ def save_png(png_bytes_or_str: bytes | str, output_file: str):
],
ids=["gradient with alpha channel", "gradient without alpha channel"])
async def test_screenshot(websocket, context_id, png_filename):
with open(Path(__file__).parent / png_filename, 'rb') as image_file:
with open(Path(__file__).parent.resolve() / png_filename,
'rb') as image_file:
png_base64 = base64.b64encode(image_file.read()).decode('utf-8')

await goto_url(websocket, context_id,
Expand Down Expand Up @@ -151,7 +153,8 @@ async def test_screenshot_oopif(websocket, context_id, html, iframe):
assert resp["result"] == {'data': ANY_STR}

png_filename = "oopif.png"
with open(Path(__file__).parent / png_filename, 'rb') as image_file:
with open(Path(__file__).parent.resolve() / png_filename,
'rb') as image_file:
png_base64 = base64.b64encode(image_file.read()).decode('utf-8')

img1 = Image.open(io.BytesIO(base64.b64decode(resp["result"]["data"])))
Expand Down
13 changes: 13 additions & 0 deletions tests/test_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import base64

import pytest
from anys import ANY_INT, ANY_STR
from test_helpers import goto_url, read_JSON_message, send_JSON_command


def save_pdf(pdf_bytes_or_str: bytes | str, output_file: str):
pdf_bytes = pdf_bytes_or_str if isinstance(
pdf_bytes_or_str, bytes) else base64.b64decode(pdf_bytes_or_str,
validate=True)
if pdf_bytes[0:4] != b'%PDF':
raise ValueError('Missing the PDF file signature')

with open(output_file, 'wb') as f:
f.write(pdf_bytes)


@pytest.mark.asyncio
async def test_print(websocket, context_id):
await goto_url(websocket, context_id, 'about:blank')
Expand Down

0 comments on commit ac9d8be

Please sign in to comment.