1# Copyright 2014 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.
4import os
5
6from telemetry.page import page_test
7from telemetry.page import page_test
8from telemetry.value import scalar
9
10
11class Screenshot(page_test.PageTest):
12  def __init__(self):
13    super(Screenshot, self).__init__(
14        action_name_to_run = 'RunPrepareForScreenshot',
15        is_action_name_to_run_optional=True)
16
17  @classmethod
18  def AddCommandLineArgs(cls, parser):
19    parser.add_option('--png-outdir',
20                      help='Output directory for the PNG files')
21
22  @classmethod
23  def ProcessCommandLineArgs(cls, parser, args):
24    if not args.png_outdir:
25      parser.error('Please specify --png-outdir')
26    cls._png_outdir = args.png_outdir
27
28  def ValidateAndMeasurePage(self, page, tab, results):
29    if not tab.screenshot_supported:
30      raise page_test.TestNotSupportedOnPlatformFailure(
31          'Browser does not support screenshotting')
32
33    tab.WaitForDocumentReadyStateToBeComplete()
34    screenshot = tab.Screenshot(60)
35
36    outpath = os.path.abspath(
37        os.path.join(self._png_outdir, page.file_safe_name)) + '.png'
38
39    if os.path.exists(outpath):
40      previous_mtime = os.path.getmtime(outpath)
41    else:
42      previous_mtime = -1
43
44    screenshot.WritePngFile(outpath)
45
46    saved_picture_count = 0
47    if os.path.exists(outpath) and os.path.getmtime(outpath) > previous_mtime:
48      saved_picture_count = 1
49    results.AddValue(scalar.ScalarValue(
50        results.current_page, 'saved_picture_count', 'count',
51        saved_picture_count))
52