OLD | NEW |
---|---|
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 from telemetry.page.actions import page_action | |
5 from telemetry.page.actions.javascript import JavaScriptAction | 6 from telemetry.page.actions.javascript import JavaScriptAction |
6 from telemetry.page.actions.navigate import NavigateAction | 7 from telemetry.page.actions.navigate import NavigateAction |
8 from telemetry.page.actions.wait import WaitAction | |
7 from telemetry.web_perf import timeline_interaction_record as tir_module | 9 from telemetry.web_perf import timeline_interaction_record as tir_module |
8 | 10 |
9 | 11 |
10 class ActionRunner(object): | 12 class ActionRunner(object): |
13 | |
11 def __init__(self, tab): | 14 def __init__(self, tab): |
12 self._tab = tab | 15 self._tab = tab |
13 | 16 |
14 # TODO(nednguyen): remove this (or make private) when | 17 # TODO(nednguyen): remove this (or make private) when |
15 # crbug.com/361809 is marked fixed | 18 # crbug.com/361809 is marked fixed |
16 def RunAction(self, action): | 19 def RunAction(self, action): |
17 if not action.WillWaitAfterRun(): | 20 if not action.WillWaitAfterRun(): |
18 action.WillRunAction(self._tab) | 21 action.WillRunAction(self._tab) |
19 action.RunActionAndMaybeWait(self._tab) | 22 action.RunActionAndMaybeWait(self._tab) |
20 | 23 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 def ExecuteJavaScript(self, js_expression): | 67 def ExecuteJavaScript(self, js_expression): |
65 """Executes a given JavaScript expression. | 68 """Executes a given JavaScript expression. |
66 | 69 |
67 Example: runner.ExecuteJavaScript('var foo = 1;'); | 70 Example: runner.ExecuteJavaScript('var foo = 1;'); |
68 | 71 |
69 Args: | 72 Args: |
70 js_expression: The expression to execute (provided as string). | 73 js_expression: The expression to execute (provided as string). |
71 """ | 74 """ |
72 self.RunAction(JavaScriptAction({'expression': js_expression})) | 75 self.RunAction(JavaScriptAction({'expression': js_expression})) |
73 | 76 |
77 def Wait(self, seconds): | |
78 """Wait for the number of seconds specified. | |
79 | |
80 Args: | |
81 seconds: The number of seconds to wait. | |
82 """ | |
83 self.RunAction(WaitAction({'seconds': seconds})) | |
84 | |
85 def WaitForJavaScriptCondition(self, condition, timeout=60): | |
86 """Wait for a JavaScript condition to become true. | |
87 | |
88 Example: runner.WaitForJavaScriptCondition('window.foo == 10'); | |
89 | |
90 Args: | |
91 condition: The JavaScript condition (as string). | |
92 timeout: The timeout in seconds (default to 60). | |
93 """ | |
94 self.RunAction(WaitAction({'javascript': condition, 'timeout': timeout})) | |
95 | |
96 def WaitForElement(self, selector=None, contains_text=None, | |
97 element_function=None, timeout=60): | |
98 """Wait for an element to appear in the document. | |
99 | |
100 Args: | |
101 selector: A CSS selector describing the element. | |
102 contains_text: A text that should be appear on the page. | |
103 DO NOT SUBMIT: Should this be split out to WaitForText? | |
104 element_function: A JavaScript function (as string) that is used | |
105 to retrieve the element. For example: | |
106 'function() { return foo.element; }'. | |
107 timeout: The timeout in seconds (default to 60). | |
108 """ | |
109 attr = {'condition': 'element', 'timeout': timeout} | |
110 _FillElementSelector( | |
111 attr, selector, contains_text, element_function) | |
112 self.RunAction(WaitAction(attr)) | |
113 | |
114 | |
115 def _FillElementSelector(attr, selector=None, contains_text=None, | |
nednguyen
2014/06/06 21:37:51
contains_text -> text & update documentation to ma
chrishenry
2014/06/06 22:50:47
Done.
| |
116 element_function=None): | |
117 count = 0 | |
118 if selector is not None: | |
119 count = count + 1 | |
120 attr['selector'] = selector | |
121 if contains_text is not None: | |
122 count = count + 1 | |
123 attr['text'] = contains_text | |
124 if element_function is not None: | |
125 count = count + 1 | |
126 attr['element_function'] = element_function | |
127 | |
128 if count != 1: | |
129 raise page_action.PageActionFailed( | |
130 'Must specify 1 way to retrieve function, but %s was specified: %s' % | |
131 (len(attr), attr.keys())) | |
132 | |
74 | 133 |
75 class Interaction(object): | 134 class Interaction(object): |
76 def __init__(self, action_runner, label, flags): | 135 def __init__(self, action_runner, label, flags): |
77 assert action_runner | 136 assert action_runner |
78 assert label | 137 assert label |
79 assert isinstance(flags, list) | 138 assert isinstance(flags, list) |
80 | 139 |
81 self._action_runner = action_runner | 140 self._action_runner = action_runner |
82 self._label = label | 141 self._label = label |
83 self._flags = flags | 142 self._flags = flags |
84 self._started = False | 143 self._started = False |
85 | 144 |
86 def Begin(self): | 145 def Begin(self): |
87 assert not self._started | 146 assert not self._started |
88 self._started = True | 147 self._started = True |
89 self._action_runner.ExecuteJavaScript('console.time("%s");' % | 148 self._action_runner.ExecuteJavaScript('console.time("%s");' % |
90 tir_module.TimelineInteractionRecord.GetJavaScriptMarker( | 149 tir_module.TimelineInteractionRecord.GetJavaScriptMarker( |
91 self._label, self._flags)) | 150 self._label, self._flags)) |
92 | 151 |
93 def End(self): | 152 def End(self): |
94 assert self._started | 153 assert self._started |
95 self._started = False | 154 self._started = False |
96 self._action_runner.ExecuteJavaScript('console.timeEnd("%s");' % | 155 self._action_runner.ExecuteJavaScript('console.timeEnd("%s");' % |
97 tir_module.TimelineInteractionRecord.GetJavaScriptMarker( | 156 tir_module.TimelineInteractionRecord.GetJavaScriptMarker( |
98 self._label, self._flags)) | 157 self._label, self._flags)) |
OLD | NEW |