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 time | |
5 | |
6 from telemetry import page_action | |
7 from telemetry import util | |
8 | |
9 class WaitAction(page_action.PageAction): | |
10 DEFAULT_TIMEOUT = 60 | |
11 | |
12 def __init__(self, attributes=None): | |
13 super(WaitAction, self).__init__(attributes) | |
14 | |
15 def RunsPreviousAction(self): | |
16 assert hasattr(self, 'condition') | |
17 return self.condition == 'navigate' or self.condition == 'href_change' | |
18 | |
19 def RunAction(self, page, tab, previous_action): | |
20 assert hasattr(self, 'condition') | |
21 | |
22 if self.condition == 'duration': | |
23 assert hasattr(self, 'seconds') | |
24 time.sleep(self.seconds) | |
25 | |
26 elif self.condition == 'navigate': | |
27 if not previous_action: | |
28 raise page_action.PageActionFailed('You need to perform an action ' | |
29 'before waiting for navigate.') | |
30 previous_action.WillRunAction() | |
31 action_to_perform = lambda: previous_action.RunAction(page, tab, None) | |
32 tab.PerformActionAndWaitForNavigate(action_to_perform) | |
33 | |
34 elif self.condition == 'href_change': | |
35 if not previous_action: | |
36 raise page_action.PageActionFailed('You need to perform an action ' | |
37 'before waiting for a href change.') | |
38 previous_action.WillRunAction() | |
39 old_url = tab.EvaluateJavaScript('document.location.href') | |
40 previous_action.RunAction(page, tab, None) | |
41 util.WaitFor(lambda: tab.EvaluateJavaScript( | |
42 'document.location.href') != old_url, self.DEFAULT_TIMEOUT) | |
43 | |
44 elif self.condition == 'element': | |
45 assert hasattr(self, 'text') or hasattr(self, 'selector') | |
46 if self.text: | |
47 callback_code = 'function(element) { return element != null; }' | |
48 util.WaitFor( | |
49 lambda: util.FindElementAndPerformAction( | |
50 tab, self.text, callback_code), self.DEFAULT_TIMEOUT) | |
51 elif self.selector: | |
52 util.WaitFor(lambda: tab.EvaluateJavaScript( | |
53 'document.querySelector("%s") != null' % self.selector), | |
54 self.DEFAULT_TIMEOUT) | |
55 | |
56 elif self.condition == 'javascript': | |
57 assert hasattr(self, 'javascript') | |
58 util.WaitFor(lambda: tab.EvaluateJavaScript(self.javascript), | |
59 self.DEFAULT_TIMEOUT) | |
OLD | NEW |