|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# A lot of test logic taken from |
| 3 | +# https://github.com/pdf2htmlEX/pdf2htmlEX/blob/v0.18.8.rc1/pdf2htmlEX/test/browser_tests.py |
| 4 | + |
| 5 | +import argparse |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import time |
| 9 | + |
| 10 | +from PIL import Image, ImageChops |
| 11 | +from selenium import webdriver |
| 12 | +from selenium.webdriver.common.by import By |
| 13 | +from selenium.webdriver.support import expected_conditions |
| 14 | +from selenium.webdriver.support.ui import WebDriverWait |
| 15 | + |
| 16 | + |
| 17 | +def main(): |
| 18 | + parser = argparse.ArgumentParser(description="Render test HTMLs into PNGs and compare against reference PNGs") |
| 19 | + parser.add_argument("--html-dir", action="store", required=True, |
| 20 | + help="Directory with converted HTMLs") |
| 21 | + parser.add_argument("--png-destination-dir", action="store", required=True, |
| 22 | + help="Directory where to save rendered PNGs") |
| 23 | + parser.add_argument("--reference-png-dir", action="store", required=True, |
| 24 | + help="Directory with reference PNGs") |
| 25 | + args = parser.parse_args() |
| 26 | + del parser |
| 27 | + |
| 28 | + tests = ["basic_text", "fontfile3_opentype", "geneve_1564", "invalid_unicode_issue477", "issue_83", "pdf", "sample", "svg_background_with_page_rotation_issue402", "test_fail", "text_visibility", "with_form"] |
| 29 | + |
| 30 | + test_result = True |
| 31 | + |
| 32 | + html_dir = os.path.abspath(args.html_dir) |
| 33 | + reference_png_dir = os.path.abspath(args.reference_png_dir) |
| 34 | + png_destination_dir = os.path.abspath(args.png_destination_dir) |
| 35 | + if not os.path.exists(png_destination_dir): |
| 36 | + os.mkdir(png_destination_dir, 0o755) |
| 37 | + |
| 38 | + os.environ["MOZ_HEADLESS"] = "1" |
| 39 | + for test in tests: |
| 40 | + html_file = os.path.join(html_dir, f"{test}/{test}.html") |
| 41 | + png_file = os.path.join(png_destination_dir, f"{test}.png") |
| 42 | + reference_png_file = os.path.join(reference_png_dir, f"{test}.png") |
| 43 | + diff_png_file = os.path.join(png_destination_dir, f"{test}-diff.png") |
| 44 | + |
| 45 | + if not os.path.exists(html_file): |
| 46 | + test_result = False |
| 47 | + print(f" FAILURE: {test} html file not found!") |
| 48 | + continue |
| 49 | + |
| 50 | + browser = webdriver.Firefox() |
| 51 | + browser.set_window_size(600, 800) |
| 52 | + try: |
| 53 | + browser.get('file://' + html_file) |
| 54 | + WebDriverWait(browser, 5) \ |
| 55 | + .until(expected_conditions.presence_of_element_located((By.ID, 'page-container'))) |
| 56 | + finally: |
| 57 | + time.sleep(1) |
| 58 | + browser.get_full_page_screenshot_as_file(png_file) |
| 59 | + browser.quit() |
| 60 | + |
| 61 | + if not os.path.exists(reference_png_file): |
| 62 | + print(f' IGNORED: {test} reference png file not found') |
| 63 | + continue |
| 64 | + |
| 65 | + out_img = Image.open(png_file).convert('RGB') |
| 66 | + ref_img = Image.open(reference_png_file).convert('RGB') |
| 67 | + |
| 68 | + diff_img = ImageChops.difference(ref_img, out_img) |
| 69 | + diff_img.convert('RGB').save(diff_png_file) |
| 70 | + |
| 71 | + diff_bbox = diff_img.getbbox() |
| 72 | + if test == "test_fail": |
| 73 | + if diff_bbox is None: |
| 74 | + test_result = False |
| 75 | + print(f" FAILURE: {test} should fail, but it did not. " |
| 76 | + f"Test system is potentially broken.") |
| 77 | + else: |
| 78 | + print(f" SUCCESS: {test}") |
| 79 | + elif diff_bbox is None: |
| 80 | + print(f" SUCCESS: {test}") |
| 81 | + else: |
| 82 | + test_result = False |
| 83 | + print(f" FAILURE: {test} diff bounding box: {diff_bbox} should be None") |
| 84 | + diff_size = (diff_bbox[2] - diff_bbox[0]) * (diff_bbox[3] - diff_bbox[1]) |
| 85 | + img_size = ref_img.size[0] * ref_img.size[1] |
| 86 | + print(f'PNG file {png_file} and {reference_png_file} differ by at' |
| 87 | + f'most {diff_size} pixels, ({100.0*diff_size/img_size} of {img_size}' |
| 88 | + f'pixels in total), difference: {diff_png_file}', file=sys.stderr) |
| 89 | + |
| 90 | + sys.exit(0 if test_result else 1) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments