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