page_runner.py revision cedac228d2dd51db4b79ea1e72c7f249408ee061
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5a3f7b4e666c476898878fa745f637129375cd889Ben Murdochimport collections
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import copy
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import logging
8a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochimport optparse
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import os
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import random
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)import sys
12ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochimport tempfile
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import time
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)from telemetry import decorators
167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)from telemetry.core import browser_finder
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)from telemetry.core import browser_info
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)from telemetry.core import exceptions
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)from telemetry.core import util
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)from telemetry.core import wpr_modes
21a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)from telemetry.core.platform.profiler import profiler_finder
220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochfrom telemetry.page import cloud_storage
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)from telemetry.page import page_filter
24a3f7b4e666c476898878fa745f637129375cd889Ben Murdochfrom telemetry.page import page_runner_repeat
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)from telemetry.page import page_test
263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)from telemetry.page import results_options
273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)from telemetry.page.actions import navigate
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)from telemetry.page.actions import page_action
29effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochfrom telemetry.util import exception_formatter
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class _RunState(object):
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  def __init__(self):
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    self.browser = None
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    self._append_to_existing_wpr = False
377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    self._last_archive_path = None
387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    self._first_browser = True
39a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    self.first_page = collections.defaultdict(lambda: True)
40ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    self.profiler_dir = None
413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    self.repeat_state = None
427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  def StartBrowserIfNeeded(self, test, page_set, page, possible_browser,
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                           credentials_path, archive_path, finder_options):
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    started_browser = not self.browser
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    # Create a browser.
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if not self.browser:
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      test.CustomizeBrowserOptionsForSinglePage(page, finder_options)
497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      self.browser = possible_browser.Create()
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      self.browser.credentials.credentials_path = credentials_path
51a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      # Set up WPR path on the new browser.
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      self.browser.SetReplayArchivePath(archive_path,
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                        self._append_to_existing_wpr,
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                        page_set.make_javascript_deterministic)
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      self._last_archive_path = page.archive_path
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
58a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      test.WillStartBrowser(self.browser)
59a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      self.browser.Start()
60a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      test.DidStartBrowser(self.browser)
617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if self._first_browser:
637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self._first_browser = False
647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self.browser.credentials.WarnIfMissingCredentials(page_set)
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        logging.info('OS: %s %s',
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                     self.browser.platform.GetOSName(),
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                     self.browser.platform.GetOSVersionName())
6868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        if self.browser.supports_system_info:
6968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)          system_info = self.browser.GetSystemInfo()
7068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)          if system_info.model_name:
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            logging.info('Model: %s', system_info.model_name)
7268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)          if system_info.gpu:
7368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)            for i, device in enumerate(system_info.gpu.devices):
744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              logging.info('GPU device %d: %s', i, device)
754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            if system_info.gpu.aux_attributes:
764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              logging.info('GPU Attributes:')
774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              for k, v in sorted(system_info.gpu.aux_attributes.iteritems()):
784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                logging.info('  %-20s: %s', k, v)
794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            if system_info.gpu.feature_status:
804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              logging.info('Feature Status:')
814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              for k, v in sorted(system_info.gpu.feature_status.iteritems()):
824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                logging.info('  %-20s: %s', k, v)
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            if system_info.gpu.driver_bug_workarounds:
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              logging.info('Driver Bug Workarounds:')
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              for workaround in system_info.gpu.driver_bug_workarounds:
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                logging.info('  %s', workaround)
8768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)          else:
8868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)            logging.info('No GPU devices')
897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    else:
907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      # Set up WPR path if it changed.
917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if page.archive_path and self._last_archive_path != page.archive_path:
927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        self.browser.SetReplayArchivePath(
937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            page.archive_path,
947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            self._append_to_existing_wpr,
957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            page_set.make_javascript_deterministic)
967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self._last_archive_path = page.archive_path
977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if self.browser.supports_tab_control and test.close_tabs_before_run:
997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      # Create a tab if there's none.
1007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if len(self.browser.tabs) == 0:
1017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self.browser.tabs.New()
1027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      # Ensure only one tab is open, unless the test is a multi-tab test.
1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      if not test.is_multi_tab_test:
1058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        while len(self.browser.tabs) > 1:
1068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          self.browser.tabs[-1].Close()
1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      # Must wait for tab to commit otherwise it can commit after the next
109f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      # navigation has begun and RenderFrameHostManager::DidNavigateMainFrame()
1104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      # will cancel the next navigation because it's pending. This manifests as
1114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      # the first navigation in a PageSet freezing indefinitly because the
1124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      # navigation was silently cancelled when |self.browser.tabs[0]| was
1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      # committed. Only do this when we just started the browser, otherwise
1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      # there are cases where previous pages in a PageSet never complete
1158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      # loading so we'll wait forever.
1168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      if started_browser:
1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        self.browser.tabs[0].WaitForDocumentReadyStateToBeComplete()
1184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  def StopBrowser(self):
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if self.browser:
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      self.browser.Close()
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      self.browser = None
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      # Restarting the state will also restart the wpr server. If we're
1257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      # recording, we need to continue adding into the same wpr archive,
1267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      # not overwrite it.
1277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      self._append_to_existing_wpr = True
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
129424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  def StartProfiling(self, page, finder_options):
130ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if not self.profiler_dir:
131ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      self.profiler_dir = tempfile.mkdtemp()
13258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    output_file = os.path.join(self.profiler_dir, page.file_safe_name)
133a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    is_repeating = (finder_options.page_repeat != 1 or
134a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                    finder_options.pageset_repeat != 1)
135a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    if is_repeating:
136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      output_file = util.GetSequentialFileName(output_file)
137424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    self.browser.StartProfiling(finder_options.profiler, output_file)
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  def StopProfiling(self):
1404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if self.browser:
1414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      self.browser.StopProfiling()
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class PageState(object):
1451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  def __init__(self, page, tab):
1461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    self.page = page
1471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    self.tab = tab
1481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    self._did_login = False
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  def PreparePage(self, test=None):
1521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if self.page.is_file:
1531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      server_started = self.tab.browser.SetHTTPServerDirectories(
1541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        self.page.page_set.serving_dirs | set([self.page.serving_dir]))
1554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      if server_started and test:
1561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        test.DidStartHTTPServer(self.tab)
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if self.page.credentials:
1591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      if not self.tab.browser.credentials.LoginNeeded(
1601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          self.tab, self.page.credentials):
1611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        raise page_test.Failure('Login as ' + self.page.credentials + ' failed')
1627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      self._did_login = True
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if test:
165eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if test.clear_cache_before_each_run:
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        self.tab.ClearCache(force=True)
1673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  def ImplicitPageNavigation(self, test=None):
1693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    """Executes the implicit navigation that occurs for every page iteration.
1703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    This function will be called once per page before any actions are executed.
1723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    """
173eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if test:
1741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      test.WillNavigateToPage(self.page, self.tab)
1751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      test.RunNavigateSteps(self.page, self.tab)
1761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      test.DidNavigateToPage(self.page, self.tab)
1773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    else:
1783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      i = navigate.NavigateAction()
1791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      i.RunAction(self.page, self.tab, None)
1801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  def CleanUpPage(self, test):
182c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    test.CleanUpAfterPage(self.page, self.tab)
1831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if self.page.credentials and self._did_login:
1841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      self.tab.browser.credentials.LoginNoLongerNeeded(
1851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          self.tab, self.page.credentials)
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)def AddCommandLineArgs(parser):
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  page_filter.PageFilter.AddCommandLineArgs(parser)
1903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  results_options.AddResultsOptions(parser)
1917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
192a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  # Page set options
193a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  group = optparse.OptionGroup(parser, 'Page set ordering and repeat options')
194a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  group.add_option('--pageset-shuffle', action='store_true',
195a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      dest='pageset_shuffle',
196a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      help='Shuffle the order of pages within a pageset.')
197a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  group.add_option('--pageset-shuffle-order-file',
198a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      dest='pageset_shuffle_order_file', default=None,
199a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      help='Filename of an output of a previously run test on the current '
200a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      'pageset. The tests will run in the same order again, overriding '
201a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      'what is specified by --page-repeat and --pageset-repeat.')
202a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  group.add_option('--page-repeat', default=1, type='int',
203a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                   help='Number of times to repeat each individual page '
204a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                   'before proceeding with the next page in the pageset.')
205a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  group.add_option('--pageset-repeat', default=1, type='int',
206a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                   help='Number of times to repeat the entire pageset.')
207a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  parser.add_option_group(group)
208a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
209a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  # WPR options
210a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  group = optparse.OptionGroup(parser, 'Web Page Replay options')
2110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  group.add_option('--use-live-sites',
2120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      dest='use_live_sites', action='store_true',
2130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      help='Run against live sites and ignore the Web Page Replay archives.')
214a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  parser.add_option_group(group)
215a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
2167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
217a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)def ProcessCommandLineArgs(parser, args):
218a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  page_filter.PageFilter.ProcessCommandLineArgs(parser, args)
219a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
220a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  # Page set options
221a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if args.pageset_shuffle_order_file and not args.pageset_shuffle:
222a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    parser.error('--pageset-shuffle-order-file requires --pageset-shuffle.')
223a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
224a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if args.page_repeat < 1:
225a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    parser.error('--page-repeat must be a positive integer.')
226a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if args.pageset_repeat < 1:
227a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    parser.error('--pageset-repeat must be a positive integer.')
228a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
229a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
23058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)def _PrepareAndRunPage(test, page_set, expectations, finder_options,
23158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                       browser_options, page, credentials_path,
23258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                       possible_browser, results, state):
233010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if browser_options.wpr_mode != wpr_modes.WPR_RECORD:
234cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    browser_options.wpr_mode = (
23558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        wpr_modes.WPR_REPLAY
23658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        if page.archive_path and os.path.isfile(page.archive_path)
23758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        else wpr_modes.WPR_OFF)
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  tries = test.attempts
240a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  while tries:
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    tries -= 1
242a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    try:
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      results_for_current_run = copy.copy(results)
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      results_for_current_run.StartTest(page)
245a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      if test.RestartBrowserBeforeEachPage() or page.startup_url:
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        state.StopBrowser()
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        # If we are restarting the browser for each page customize the per page
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        # options for just the current page before starting the browser.
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      state.StartBrowserIfNeeded(test, page_set, page, possible_browser,
250cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                 credentials_path, page.archive_path,
251cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                 finder_options)
252cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      if not page.CanRunOnBrowser(browser_info.BrowserInfo(state.browser)):
253cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        logging.info('Skip test for page %s because browser is not supported.'
254cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                     % page.url)
255cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        results_for_current_run.StopTest(page)
256cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        return results
257a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
2583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      expectation = expectations.GetExpectationForPage(state.browser, page)
2593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
260a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      _WaitForThermalThrottlingIfNeeded(state.browser.platform)
261a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
262424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      if finder_options.profiler:
263424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        state.StartProfiling(page, finder_options)
264a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
265a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      try:
2663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        _RunPage(test, page, state, expectation,
267424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                 results_for_current_run, finder_options)
268a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch        _CheckThermalThrottling(state.browser.platform)
269cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      except exceptions.TabCrashException as e:
2708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        if test.is_multi_tab_test:
271cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          logging.error('Aborting multi-tab test after tab %s crashed',
272cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                        page.url)
2738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          raise
274cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        logging.warning(e)
275cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        state.StopBrowser()
276a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
277424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      if finder_options.profiler:
278a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch        state.StopProfiling()
279a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (test.StopBrowserAfterPage(state.browser, page)):
281a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch        state.StopBrowser()
282a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      results_for_current_run.StopTest(page)
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if state.first_page[page]:
2865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        state.first_page[page] = False
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if test.discard_first_result:
2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return results
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return results_for_current_run
290cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    except exceptions.BrowserGoneException as e:
291a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      state.StopBrowser()
292a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      if not tries:
293cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        logging.error('Aborting after too many retries')
294a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch        raise
2958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      if test.is_multi_tab_test:
296cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        logging.error('Aborting multi-tab test after browser crashed')
2978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        raise
298cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      logging.warning(e)
299a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
300a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
3010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochdef _UpdatePageSetArchivesIfChanged(page_set):
3020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  # Attempt to download the credentials file.
3030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if page_set.credentials_path:
3040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    try:
3050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      cloud_storage.GetIfChanged(
3060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          os.path.join(page_set.base_dir, page_set.credentials_path))
3070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    except (cloud_storage.CredentialsError, cloud_storage.PermissionError):
3080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      logging.warning('Cannot retrieve credential file: %s',
3090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                      page_set.credentials_path)
3100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  # Scan every serving directory for .sha1 files
3110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  # and download them from Cloud Storage. Assume all data is public.
3120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  all_serving_dirs = page_set.serving_dirs.copy()
3130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  # Add individual page dirs to all serving dirs.
3140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for page in page_set:
3150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if page.is_file:
3160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      all_serving_dirs.add(page.serving_dir)
3170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  # Scan all serving dirs.
3180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for serving_dir in all_serving_dirs:
3190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if os.path.splitdrive(serving_dir)[1] == '/':
3200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      raise ValueError('Trying to serve root directory from HTTP server.')
3210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    for dirpath, _, filenames in os.walk(serving_dir):
3220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      for filename in filenames:
3230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        path, extension = os.path.splitext(
3240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            os.path.join(dirpath, filename))
3250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if extension != '.sha1':
3260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          continue
3270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        cloud_storage.GetIfChanged(path)
3280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
330424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)def Run(test, page_set, expectations, finder_options):
3317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  """Runs a given test against a given page_set with the given options."""
332424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  results = results_options.PrepareResults(test, finder_options)
3337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  test.ValidatePageSet(page_set)
3351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  # Create a possible_browser with the given options.
337bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  try:
338424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    possible_browser = browser_finder.FindBrowser(finder_options)
339bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  except browser_finder.BrowserTypeRequiredException, e:
340bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    sys.stderr.write(str(e) + '\n')
341c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    sys.exit(-1)
3427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if not possible_browser:
343bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    sys.stderr.write(
344bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch        'No browser found. Available browsers:\n' +
345424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        '\n'.join(browser_finder.GetAllAvailableBrowserTypes(finder_options)) +
346424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        '\n')
347c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    sys.exit(-1)
3487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
349cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  browser_options = possible_browser.finder_options.browser_options
350f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  browser_options.browser_type = possible_browser.browser_type
351cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  browser_options.platform = possible_browser.platform
352cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  test.CustomizeBrowserOptions(browser_options)
353f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if not decorators.IsEnabled(
355cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      test, browser_options.browser_type, browser_options.platform):
3565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return results
3575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  # Reorder page set based on options.
359424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  pages = _ShuffleAndFilterPageSet(page_set, finder_options)
3607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (not finder_options.use_live_sites and
36258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      browser_options.wpr_mode != wpr_modes.WPR_RECORD):
3630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    _UpdatePageSetArchivesIfChanged(page_set)
3647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    pages = _CheckArchives(page_set, pages, results)
3657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  # Verify credentials path.
3677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  credentials_path = None
3687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if page_set.credentials_path:
3697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    credentials_path = os.path.join(os.path.dirname(page_set.file_path),
3707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                    page_set.credentials_path)
3717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if not os.path.exists(credentials_path):
3727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      credentials_path = None
3737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  # Set up user agent.
375cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  browser_options.browser_user_agent_type = page_set.user_agent_type or None
3767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if finder_options.profiler:
3784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    profiler_class = profiler_finder.FindProfiler(finder_options.profiler)
379cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    profiler_class.CustomizeBrowserOptions(browser_options.browser_type,
380cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                           finder_options)
3817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
382a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  for page in list(pages):
383a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    if not test.CanRunForPage(page):
3845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      logging.debug('Skipping test: it cannot run for %s', page.url)
385a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      results.AddSkip(page, 'Test cannot run')
386a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      pages.remove(page)
387a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
388a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  if not pages:
389a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    return results
390a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
3917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  state = _RunState()
3927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  # TODO(dtu): Move results creation and results_for_current_run into RunState.
3937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  try:
395a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    test.WillRunTest(finder_options)
3963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    state.repeat_state = page_runner_repeat.PageRunnerRepeatState(
397a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        finder_options)
398a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
3993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    state.repeat_state.WillRunPageSet()
4001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    while state.repeat_state.ShouldRepeatPageSet() and not test.IsExiting():
401a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      for page in pages:
4023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        state.repeat_state.WillRunPage()
4031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        test.WillRunPageRepeats(page)
4043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        while state.repeat_state.ShouldRepeatPage():
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          results = _PrepareAndRunPage(
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              test, page_set, expectations, finder_options, browser_options,
4075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              page, credentials_path, possible_browser, results, state)
4083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          state.repeat_state.DidRunPage()
4091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        test.DidRunPageRepeats(page)
410a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if (not test.max_failures is None and
411a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            len(results.failures) > test.max_failures):
412a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          logging.error('Too many failures. Aborting.')
413a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          test.RequestExit()
414a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if (not test.max_errors is None and
415a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            len(results.errors) > test.max_errors):
416a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          logging.error('Too many errors. Aborting.')
417a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          test.RequestExit()
4181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        if test.IsExiting():
4191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          break
4203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      state.repeat_state.DidRunPageSet()
421a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
4221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    test.DidRunTest(state.browser, results)
4237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  finally:
4247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    state.StopBrowser()
4257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return results
4277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
429424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)def _ShuffleAndFilterPageSet(page_set, finder_options):
430424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if finder_options.pageset_shuffle_order_file:
431424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return page_set.ReorderPageSet(finder_options.pageset_shuffle_order_file)
4327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  pages = [page for page in page_set.pages[:]
434a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)           if not page.disabled and page_filter.PageFilter.IsSelected(page)]
4357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
436424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if finder_options.pageset_shuffle:
4377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    random.Random().shuffle(pages)
438a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
439a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  return pages
4407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)def _CheckArchives(page_set, pages, results):
4437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  """Returns a subset of pages that are local or have WPR archives.
4447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  Logs warnings if any are missing."""
4467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  page_set_has_live_sites = False
4477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  for page in pages:
448ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    if not page.is_local:
4497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      page_set_has_live_sites = True
4507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break
4517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  # Potential problems with the entire page set.
4537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if page_set_has_live_sites:
4547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if not page_set.archive_data_file:
4557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      logging.warning('The page set is missing an "archive_data_file" '
4567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                      'property. Skipping any live sites. To include them, '
4570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                      'pass the flag --use-live-sites.')
4587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if not page_set.wpr_archive_info:
4597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      logging.warning('The archive info file is missing. '
4607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                      'To fix this, either add svn-internal to your '
4617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                      '.gclient using http://goto/read-src-internal, '
4627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                      'or create a new archive using record_wpr.')
4637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  # Potential problems with individual pages.
4657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  pages_missing_archive_path = []
4667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  pages_missing_archive_data = []
4677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  for page in pages:
469ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    if page.is_local:
4707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      continue
4717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if not page.archive_path:
4737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      pages_missing_archive_path.append(page)
474eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    elif not os.path.isfile(page.archive_path):
4757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      pages_missing_archive_data.append(page)
4767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if pages_missing_archive_path:
4787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    logging.warning('The page set archives for some pages do not exist. '
4797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                    'Skipping those pages. To fix this, record those pages '
4807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                    'using record_wpr. To ignore this warning and run '
4810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                    'against live sites, pass the flag --use-live-sites.')
4827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if pages_missing_archive_data:
4837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    logging.warning('The page set archives for some pages are missing. '
4847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                    'Someone forgot to check them in, or they were deleted. '
4857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                    'Skipping those pages. To fix this, record those pages '
4867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                    'using record_wpr. To ignore this warning and run '
4870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                    'against live sites, pass the flag --use-live-sites.')
4887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  for page in pages_missing_archive_path + pages_missing_archive_data:
4907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    results.StartTest(page)
4917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    results.AddErrorMessage(page, 'Page set archive doesn\'t exist.')
4927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    results.StopTest(page)
4937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return [page for page in pages if page not in
4957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          pages_missing_archive_path + pages_missing_archive_data]
4967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
498424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)def _RunPage(test, page, state, expectation, results, finder_options):
499424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if expectation == 'skip':
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    logging.debug('Skipping test: Skip expectation for %s', page.url)
5015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    results.AddSkip(page, 'Skipped by test expectations')
502424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return
503424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
504cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  logging.info('Running %s', page.url)
5057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  page_state = PageState(page, test.TabForPage(page, state.browser))
5073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
5083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  def ProcessError():
5093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if expectation == 'fail':
510cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      msg = 'Expected exception while running %s' % page.url
5113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      results.AddSuccess(page)
5123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    else:
513cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      msg = 'Exception while running %s' % page.url
5143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      results.AddError(page, sys.exc_info())
515cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    exception_formatter.PrintFormattedException(msg=msg)
5167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  try:
5181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    page_state.PreparePage(test)
519424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if state.repeat_state.ShouldNavigate(
520424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        finder_options.skip_navigate_on_repeat):
5211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      page_state.ImplicitPageNavigation(test)
52223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    test.RunPage(page, page_state.tab, results)
5231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    util.CloseConnections(page_state.tab)
5244ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch  except page_test.TestNotSupportedOnPlatformFailure:
5254ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch    raise
5267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  except page_test.Failure:
527ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if expectation == 'fail':
528cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      exception_formatter.PrintFormattedException(
529cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          msg='Expected failure while running %s' % page.url)
530ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      results.AddSuccess(page)
531ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    else:
532cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      exception_formatter.PrintFormattedException(
533cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          msg='Failure while running %s' % page.url)
534ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      results.AddFailure(page, sys.exc_info())
535558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  except (util.TimeoutException, exceptions.LoginException,
536558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch          exceptions.ProfilingException):
5373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    ProcessError()
5387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  except (exceptions.TabCrashException, exceptions.BrowserGoneException):
5393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    ProcessError()
5407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    # Run() catches these exceptions to relaunch the tab/browser, so re-raise.
5417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    raise
542a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  except page_action.PageActionNotSupported as e:
543a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    results.AddSkip(page, 'Unsupported page action: %s' % e)
5447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  except Exception:
545cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    exception_formatter.PrintFormattedException(
546cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        msg='Unhandled exception while running %s' % page.url)
547a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    results.AddFailure(page, sys.exc_info())
5487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  else:
549ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if expectation == 'fail':
550ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      logging.warning('%s was expected to fail, but passed.\n', page.url)
5517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    results.AddSuccess(page)
5527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  finally:
553a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    page_state.CleanUpPage(test)
5547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)def _WaitForThermalThrottlingIfNeeded(platform):
5577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if not platform.CanMonitorThermalThrottling():
5587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return
5597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  thermal_throttling_retry = 0
5607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  while (platform.IsThermallyThrottled() and
5617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)         thermal_throttling_retry < 3):
5627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    logging.warning('Thermally throttled, waiting (%d)...',
5637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                    thermal_throttling_retry)
5647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    thermal_throttling_retry += 1
5657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    time.sleep(thermal_throttling_retry * 2)
5667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if thermal_throttling_retry and platform.IsThermallyThrottled():
568cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    logging.warning('Device is thermally throttled before running '
569cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                    'performance tests, results will vary.')
5707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)def _CheckThermalThrottling(platform):
5737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if not platform.CanMonitorThermalThrottling():
5747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return
5757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if platform.HasBeenThermallyThrottled():
576cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    logging.warning('Device has been thermally throttled during '
577cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                    'performance tests, results will vary.')
578