Commit 132fb473 authored by ernstm@chromium.org's avatar ernstm@chromium.org

telemetry: add screenshot measurement

R=tonyg@chromium.org,nduca@chromium.org
BUG=373045

Review URL: https://codereview.chromium.org/271733008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@276623 0039d316-1c4b-4281-b951-d872f2087c98
parent 6cc6c707
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
from telemetry.page import page_measurement
from telemetry.page import page_test
class Screenshot(page_measurement.PageMeasurement):
def __init__(self):
super(Screenshot, self).__init__(
action_name_to_run = 'RunPrepareForScreenshot',
is_action_name_to_run_optional=True)
@classmethod
def AddCommandLineArgs(cls, parser):
parser.add_option('--png-outdir',
help='Output directory for the PNG files')
@classmethod
def ProcessCommandLineArgs(cls, parser, args):
if not args.png_outdir:
parser.error('Please specify --png-outdir')
cls._png_outdir = args.png_outdir
def MeasurePage(self, page, tab, results):
if not tab.screenshot_supported:
raise page_test.TestNotSupportedOnPlatformFailure(
'Browser does not support screenshotting')
tab.WaitForDocumentReadyStateToBeComplete()
screenshot = tab.Screenshot(60)
outpath = os.path.abspath(
os.path.join(self._png_outdir, page.file_safe_name)) + '.png'
if os.path.exists(outpath):
previous_mtime = os.path.getmtime(outpath)
else:
previous_mtime = -1
screenshot.WritePngFile(outpath)
saved_picture_count = 0
if os.path.exists(outpath) and os.path.getmtime(outpath) > previous_mtime:
saved_picture_count = 1
results.Add('saved_picture_count', 'count', saved_picture_count)
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
import shutil
import tempfile
from measurements import screenshot
from telemetry.page import page_measurement_unittest_base
from telemetry.page import page_test
from telemetry.unittest import options_for_unittests
class ScreenshotUnitTest(
page_measurement_unittest_base.PageMeasurementUnitTestBase):
def setUp(self):
self._options = options_for_unittests.GetCopy()
self._options.png_outdir = tempfile.mkdtemp('_png_test')
def tearDown(self):
shutil.rmtree(self._options.png_outdir)
def testScreenshot(self):
ps = self.CreatePageSetFromFileInUnittestDataDir('blank.html')
measurement = screenshot.Screenshot()
try:
results = self.RunMeasurement(measurement, ps, options=self._options)
except page_test.TestNotSupportedOnPlatformFailure as failure:
logging.warning(str(failure))
return
saved_picture_count = results.FindAllPageSpecificValuesNamed(
'saved_picture_count')
self.assertEquals(len(saved_picture_count), 1)
self.assertGreater(saved_picture_count[0].GetRepresentativeNumber(), 0)
......@@ -35,17 +35,24 @@ class PageMeasurement(page_test.PageTest):
body_child_count = tab.EvaluateJavaScript(
'document.querySelector('%s').children.length')
results.Add('children', 'count', child_count)
is_action_name_to_run_optional determines what to do if action_name_to_run is
not empty but the page doesn't have that action. The page will run (without
any action) if is_action_name_to_run_optional is True, otherwise the page will
fail.
"""
def __init__(self,
action_name_to_run='',
needs_browser_restart_after_each_page=False,
discard_first_result=False,
clear_cache_before_each_run=False):
clear_cache_before_each_run=False,
is_action_name_to_run_optional=False):
super(PageMeasurement, self).__init__(
action_name_to_run,
needs_browser_restart_after_each_page,
discard_first_result,
clear_cache_before_each_run)
clear_cache_before_each_run,
is_action_name_to_run_optional=is_action_name_to_run_optional)
def ValidatePage(self, page, tab, results):
results.WillMeasurePage(page)
......
......@@ -32,7 +32,8 @@ class PageTest(command_line.Command):
clear_cache_before_each_run=False,
attempts=3,
max_failures=None,
max_errors=None):
max_errors=None,
is_action_name_to_run_optional=False):
super(PageTest, self).__init__()
self.options = None
......@@ -50,6 +51,7 @@ class PageTest(command_line.Command):
self._attempts = attempts
self._max_failures = max_failures
self._max_errors = max_errors
self._is_action_name_to_run_optional = is_action_name_to_run_optional
assert self._attempts > 0, 'Test attempts must be greater than 0'
# If the test overrides the TabForPage method, it is considered a multi-tab
# test. The main difference between this and a single-tab test is that we
......@@ -167,7 +169,7 @@ class PageTest(command_line.Command):
def CanRunForPage(self, page): # pylint: disable=W0613
"""Override to customize if the test can be ran for the given page."""
if self._action_name_to_run:
if self._action_name_to_run and not self._is_action_name_to_run_optional:
return hasattr(page, self._action_name_to_run)
return True
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment