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 sys | |
5 | |
6 class Failure(Exception): | |
7 """Exception that can be thrown from MultiPageBenchmark to indicate an | |
8 undesired but designed-for problem.""" | |
9 pass | |
10 | |
11 class PageTestResults(object): | |
12 def __init__(self): | |
13 self.page_successes = [] | |
14 self.page_failures = [] | |
15 self.skipped_pages = [] | |
16 | |
17 def AddSuccess(self, page): | |
18 self.page_successes.append({'page': page}) | |
19 | |
20 def AddFailure(self, page, message, details): | |
21 self.page_failures.append({'page': page, | |
22 'message': message, | |
23 'details': details}) | |
24 | |
25 def AddSkippedPage(self, page, message, details): | |
26 self.skipped_pages.append({'page': page, | |
27 'message': message, | |
28 'details': details}) | |
29 | |
30 class PageTest(object): | |
31 """A class styled on unittest.TestCase for creating page-specific tests.""" | |
32 | |
33 def __init__(self, | |
34 test_method_name, | |
35 action_name_to_run='', | |
36 needs_browser_restart_after_each_run=False): | |
37 self.options = None | |
38 try: | |
39 self._test_method = getattr(self, test_method_name) | |
40 except AttributeError: | |
41 raise ValueError, 'No such method %s.%s' % ( | |
42 self.__class_, test_method_name) # pylint: disable=E1101 | |
43 self._action_name_to_run = action_name_to_run | |
44 self._needs_browser_restart_after_each_run = ( | |
45 needs_browser_restart_after_each_run) | |
46 | |
47 @property | |
48 def needs_browser_restart_after_each_run(self): | |
49 return self._needs_browser_restart_after_each_run | |
50 | |
51 def AddCommandLineOptions(self, parser): | |
52 """Override to expose command-line options for this benchmark. | |
53 | |
54 The provided parser is an optparse.OptionParser instance and accepts all | |
55 normal results. The parsed options are available in Run as | |
56 self.options.""" | |
57 pass | |
58 | |
59 def CustomizeBrowserOptions(self, options): | |
60 """Override to add test-specific options to the BrowserOptions object""" | |
61 pass | |
62 | |
63 def CustomizeBrowserOptionsForPage(self, page, options): | |
64 """Add options specific to the test and the given page.""" | |
65 if not self.CanRunForPage(page): | |
66 return | |
67 action = self.GetAction(page) | |
68 if action: | |
69 action.CustomizeBrowserOptions(options) | |
70 | |
71 def SetUpBrowser(self, browser): | |
72 """Override to customize the browser right after it has launched.""" | |
73 pass | |
74 | |
75 def CanRunForPage(self, page): #pylint: disable=W0613 | |
76 """Override to customize if the test can be ran for the given page.""" | |
77 return True | |
78 | |
79 def WillNavigateToPage(self, page, tab): | |
80 """Override to do operations before the page is navigated.""" | |
81 pass | |
82 | |
83 def DidNavigateToPage(self, page, tab): | |
84 """Override to do operations right after the page is navigated, but before | |
85 any waiting for completion has occurred.""" | |
86 pass | |
87 | |
88 def WillRunAction(self, page, tab, action): | |
89 """Override to do operations before running the action on the page.""" | |
90 pass | |
91 | |
92 def DidRunAction(self, page, tab, action): | |
93 """Override to do operations after running the action on the page.""" | |
94 pass | |
95 | |
96 def Run(self, options, page, tab, results): | |
97 self.options = options | |
98 action = self.GetAction(page) | |
99 if action: | |
100 action.WillRunAction(page, tab) | |
101 self.WillRunAction(page, tab, action) | |
102 action.RunAction(page, tab, None) | |
103 self.DidRunAction(page, tab, action) | |
104 try: | |
105 self._test_method(page, tab, results) | |
106 finally: | |
107 self.options = None | |
108 | |
109 def GetAction(self, page): | |
110 if not self._action_name_to_run: | |
111 return None | |
112 action_data = getattr(page, self._action_name_to_run) | |
113 from telemetry import all_page_actions | |
114 cls = all_page_actions.FindClassWithName(action_data['action']) | |
115 if not cls: | |
116 sys.stderr.write('Could not find action named %s\n' % | |
117 action_data['action']) | |
118 sys.stderr.write('Check the pageset for a typo and check the error log' + | |
119 'for possible python loading/compilation errors\n') | |
120 raise Exception('%s not found' % action_data['action']) | |
121 assert cls | |
122 return cls(action_data) | |
123 | |
124 @property | |
125 def action_name_to_run(self): | |
126 return self._action_name_to_run | |
OLD | NEW |