OLD | NEW |
| (Empty) |
1 # Copyright (c) 2012 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 import logging | |
5 import os | |
6 import sys | |
7 import tempfile | |
8 import time | |
9 | |
10 from telemetry.core import browser_finder | |
11 from telemetry.core import browser_options | |
12 from telemetry.core import wpr_modes | |
13 from telemetry.page import all_page_actions # pylint: disable=W0611 | |
14 from telemetry.page import multi_page_benchmark | |
15 from telemetry.page import page_runner | |
16 from telemetry.page import page_set | |
17 from telemetry.page import page_test | |
18 from telemetry.test import discover | |
19 | |
20 class RecordPage(page_test.PageTest): | |
21 def __init__(self, benchmarks): | |
22 # This class overwrites PageTest.Run, so that the test method name is not | |
23 # really used (except for throwing an exception if it doesn't exist). | |
24 super(RecordPage, self).__init__('Run') | |
25 self._action_names = set( | |
26 [benchmark().action_name_to_run | |
27 for benchmark in benchmarks.values() | |
28 if benchmark().action_name_to_run]) | |
29 | |
30 def CanRunForPage(self, page): | |
31 return not not self._ActionsForPage(page) | |
32 | |
33 def CustomizeBrowserOptionsForPage(self, page, options): | |
34 for action in self._ActionsForPage(page): | |
35 action.CustomizeBrowserOptions(options) | |
36 | |
37 def Run(self, options, page, tab, results): | |
38 # When recording, sleep to catch any resources that load post-onload. | |
39 time.sleep(3) | |
40 | |
41 # Run the actions for all benchmarks. Reload the page between | |
42 # actions. | |
43 should_reload = False | |
44 for action in self._ActionsForPage(page): | |
45 if should_reload: | |
46 tab.Navigate(page.url) | |
47 tab.WaitForDocumentReadyStateToBeComplete() | |
48 action.WillRunAction(page, tab) | |
49 action.RunAction(page, tab, None) | |
50 should_reload = True | |
51 | |
52 def _ActionsForPage(self, page): | |
53 actions = [] | |
54 for action_name in self._action_names: | |
55 if not hasattr(page, action_name): | |
56 continue | |
57 action_data = getattr(page, action_name) | |
58 actions.append(all_page_actions.FindClassWithName( | |
59 action_data['action'])(action_data)) | |
60 return actions | |
61 | |
62 | |
63 def Main(benchmark_dir): | |
64 benchmarks = discover.Discover(benchmark_dir, | |
65 os.path.join(benchmark_dir, '..'), | |
66 '', | |
67 multi_page_benchmark.MultiPageBenchmark) | |
68 options = browser_options.BrowserOptions() | |
69 parser = options.CreateParser('%prog <page_set>') | |
70 page_runner.PageRunner.AddCommandLineOptions(parser) | |
71 | |
72 recorder = RecordPage(benchmarks) | |
73 recorder.AddCommandLineOptions(parser) | |
74 | |
75 _, args = parser.parse_args() | |
76 | |
77 if len(args) != 1: | |
78 parser.print_usage() | |
79 sys.exit(1) | |
80 | |
81 ps = page_set.PageSet.FromFile(args[0]) | |
82 | |
83 # Set the archive path to something temporary. | |
84 temp_target_wpr_file_path = tempfile.mkstemp()[1] | |
85 ps.wpr_archive_info.AddNewTemporaryRecording(temp_target_wpr_file_path) | |
86 | |
87 # Do the actual recording. | |
88 options.wpr_mode = wpr_modes.WPR_RECORD | |
89 recorder.CustomizeBrowserOptions(options) | |
90 possible_browser = browser_finder.FindBrowser(options) | |
91 if not possible_browser: | |
92 print >> sys.stderr, """No browser found.\n | |
93 Use --browser=list to figure out which are available.\n""" | |
94 sys.exit(1) | |
95 results = page_test.PageTestResults() | |
96 with page_runner.PageRunner(ps) as runner: | |
97 runner.Run(options, possible_browser, recorder, results) | |
98 | |
99 if results.page_failures: | |
100 logging.warning('Some pages failed. The recording has not been updated for ' | |
101 'these pages.') | |
102 logging.warning('Failed pages: %s', '\n'.join( | |
103 [failure['page'].url for failure in results.page_failures])) | |
104 | |
105 if results.skipped_pages: | |
106 logging.warning('Some pages were skipped. The recording has not been ' | |
107 'updated for these pages.') | |
108 logging.warning('Skipped pages: %s', '\n'.join( | |
109 [skipped['page'].url for skipped in results.skipped_pages])) | |
110 | |
111 if results.page_successes: | |
112 # Update the metadata for the pages which were recorded. | |
113 ps.wpr_archive_info.AddRecordedPages( | |
114 [page['page'] for page in results.page_successes]) | |
115 else: | |
116 os.remove(temp_target_wpr_file_path) | |
117 | |
118 return min(255, len(results.page_failures)) | |
OLD | NEW |