OLD | NEW |
(Empty) | |
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import logging |
| 6 import os |
| 7 import py_utils |
| 8 import time |
| 9 |
| 10 from telemetry.page import legacy_page_test |
| 11 from telemetry.util import image_util |
| 12 |
| 13 class Screenshot(legacy_page_test.LegacyPageTest): |
| 14 """Takes a PNG screenshot of the page.""" |
| 15 |
| 16 def __init__(self, png_outdir, wait_time=0): |
| 17 super(Screenshot, self).__init__() |
| 18 self._png_outdir = png_outdir |
| 19 self._wait_time = wait_time |
| 20 |
| 21 def ValidateAndMeasurePage(self, page, tab, results): |
| 22 if not tab.screenshot_supported: |
| 23 raise legacy_page_test.MeasurementFailure( |
| 24 'Screenshotting not supported on this platform') |
| 25 |
| 26 try: |
| 27 tab.WaitForDocumentReadyStateToBeComplete() |
| 28 except py_utils.TimeoutException: |
| 29 logging.warning("WaitForDocumentReadyStateToBeComplete() timeout, " + |
| 30 "page: %s", page.display_name) |
| 31 return |
| 32 |
| 33 time.sleep(self._wait_time) |
| 34 |
| 35 if not os.path.exists(self._png_outdir): |
| 36 logging.info("Creating directory %s", self._png_outdir) |
| 37 try: |
| 38 os.makedirs(self._png_outdir) |
| 39 except OSError: |
| 40 logging.warning("Directory %s could not be created", self._png_outdir) |
| 41 raise |
| 42 |
| 43 outpath = os.path.abspath( |
| 44 os.path.join(self._png_outdir, page.file_safe_name)) + '.png' |
| 45 # Replace win32 path separator char '\' with '\\'. |
| 46 outpath = outpath.replace('\\', '\\\\') |
| 47 |
| 48 screenshot = tab.Screenshot() |
| 49 |
| 50 # TODO(lchoi): Add logging to image_util.py and/or augment error handling of |
| 51 # image_util.WritePngFile |
| 52 logging.info("Writing PNG file to %s. This may take awhile.", outpath) |
| 53 start = time.time() |
| 54 image_util.WritePngFile(screenshot, outpath) |
| 55 logging.info("PNG file written successfully. (Took %f seconds)", |
| 56 time.time()-start) |
OLD | NEW |