OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """PyAuto: Python Interface to Chromium's Automation Proxy. | 6 """PyAuto: Python Interface to Chromium's Automation Proxy. |
7 | 7 |
8 PyAuto uses swig to expose Automation Proxy interfaces to Python. | 8 PyAuto uses swig to expose Automation Proxy interfaces to Python. |
9 For complete documentation on the functionality available, | 9 For complete documentation on the functionality available, |
10 run pydoc on this file. | 10 run pydoc on this file. |
(...skipping 2950 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2961 cmd_dict = { | 2961 cmd_dict = { |
2962 'command': 'AddDomEventObserver', | 2962 'command': 'AddDomEventObserver', |
2963 'event_name': event_name, | 2963 'event_name': event_name, |
2964 'automation_id': automation_id, | 2964 'automation_id': automation_id, |
2965 'recurring': recurring, | 2965 'recurring': recurring, |
2966 } | 2966 } |
2967 return self._GetResultFromJSONRequest(cmd_dict, windex=None)['observer_id'] | 2967 return self._GetResultFromJSONRequest(cmd_dict, windex=None)['observer_id'] |
2968 | 2968 |
2969 def AddDomMutationObserver(self, mutation_type, xpath, | 2969 def AddDomMutationObserver(self, mutation_type, xpath, |
2970 attribute='textContent', expected_value=None, | 2970 attribute='textContent', expected_value=None, |
2971 automation_id=44444, **kwargs): | 2971 automation_id=44444, |
2972 exec_js=None, **kwargs): | |
Nirnimesh
2012/04/24 23:25:02
would supplying the default arg (PyUITest.ExecuteJ
craigdh
2012/04/25 00:57:15
Unfortunately, no, I tried that. Even moving the E
| |
2972 """Sets up an event observer watching for a specific DOM mutation. | 2973 """Sets up an event observer watching for a specific DOM mutation. |
2973 | 2974 |
2974 Creates an observer that raises an event when a mutation of the given type | 2975 Creates an observer that raises an event when a mutation of the given type |
2975 occurs on a DOM node specified by |selector|. | 2976 occurs on a DOM node specified by |selector|. |
2976 | 2977 |
2977 Args: | 2978 Args: |
2978 mutation_type: One of 'add', 'remove', 'change', or 'exists'. | 2979 mutation_type: One of 'add', 'remove', 'change', or 'exists'. |
2979 xpath: An xpath specifying the DOM node to watch. The node must already | 2980 xpath: An xpath specifying the DOM node to watch. The node must already |
2980 exist if |mutation_type| is 'change'. | 2981 exist if |mutation_type| is 'change'. |
2981 attribute: Attribute to match |expected_value| against, if given. Defaults | 2982 attribute: Attribute to match |expected_value| against, if given. Defaults |
2982 to 'textContent'. | 2983 to 'textContent'. |
2983 expected_value: Optional regular expression to match against the node's | 2984 expected_value: Optional regular expression to match against the node's |
2984 textContent attribute after the mutation. Defaults to None. | 2985 textContent attribute after the mutation. Defaults to None. |
2985 automation_id: The automation_id used to route the observer javascript | 2986 automation_id: The automation_id used to route the observer javascript |
2986 messages. Defaults to 44444. | 2987 messages. Defaults to 44444. |
2988 exec_js: A callable of the form f(self, js, **kwargs) used to inject the | |
2989 MutationObserver javascript. Defaults to None, which uses | |
2990 PyUITest.ExecuteJavascript. | |
2987 | 2991 |
2988 Any additional keyword arguments are passed on to ExecuteJavascript and | 2992 Any additional keyword arguments are passed on to ExecuteJavascript and |
2989 can be used to select the tab where the DOM MutationObserver is created. | 2993 can be used to select the tab where the DOM MutationObserver is created. |
2990 | 2994 |
2991 Returns: | 2995 Returns: |
2992 The id of the created observer, which can be used with GetNextEvent(id) | 2996 The id of the created observer, which can be used with GetNextEvent(id) |
2993 and RemoveEventObserver(id). | 2997 and RemoveEventObserver(id). |
2994 | 2998 |
2995 Raises: | 2999 Raises: |
2996 pyauto_errors.JSONInterfaceError if the automation call returns an error. | 3000 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
(...skipping 11 matching lines...) Expand all Loading... | |
3008 observer_id = ( | 3012 observer_id = ( |
3009 self._GetResultFromJSONRequest(cmd_dict, windex=None)['observer_id']) | 3013 self._GetResultFromJSONRequest(cmd_dict, windex=None)['observer_id']) |
3010 expected_string = ('null' if expected_value is None else '"%s"' % | 3014 expected_string = ('null' if expected_value is None else '"%s"' % |
3011 expected_value.replace('"', r'\"')) | 3015 expected_value.replace('"', r'\"')) |
3012 jsfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), | 3016 jsfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), |
3013 'dom_mutation_observer.js') | 3017 'dom_mutation_observer.js') |
3014 with open(jsfile, 'r') as f: | 3018 with open(jsfile, 'r') as f: |
3015 js = ('(' + f.read() + ')(%d, %d, "%s", "%s", "%s", %s);' % | 3019 js = ('(' + f.read() + ')(%d, %d, "%s", "%s", "%s", %s);' % |
3016 (automation_id, observer_id, mutation_type, | 3020 (automation_id, observer_id, mutation_type, |
3017 xpath.replace('"', r'\"'), attribute, expected_string)) | 3021 xpath.replace('"', r'\"'), attribute, expected_string)) |
3018 jsreturn = self.ExecuteJavascript(js, **kwargs) | 3022 exec_js = PyUITest.ExecuteJavascript if exec_js is None else exec_js |
Nirnimesh
2012/04/24 23:25:02
exec_js = exec_js or PyUITest.ExecuteJavascript
craigdh
2012/04/25 00:57:15
Done. Good point, a callable will never evaluate a
| |
3023 jsreturn = exec_js(self, js, **kwargs) | |
3019 if jsreturn != 'success': | 3024 if jsreturn != 'success': |
3020 self.RemoveEventObserver(observer_id) | 3025 self.RemoveEventObserver(observer_id) |
3021 raise pyauto_errors.JavascriptRuntimeError(jsreturn) | 3026 raise pyauto_errors.JavascriptRuntimeError(jsreturn) |
3022 return observer_id | 3027 return observer_id |
3023 | 3028 |
3024 def WaitForDomNode(self, xpath, attribute='textContent', | 3029 def WaitForDomNode(self, xpath, attribute='textContent', |
3025 expected_value=None, timeout=-1, **kwargs): | 3030 expected_value=None, exec_js=None, timeout=-1, **kwargs): |
3026 """Waits until a node specified by an xpath exists in the DOM. | 3031 """Waits until a node specified by an xpath exists in the DOM. |
3027 | 3032 |
3028 NOTE: This does NOT poll. It returns as soon as the node appears, or | 3033 NOTE: This does NOT poll. It returns as soon as the node appears, or |
3029 immediately if the node already exists. | 3034 immediately if the node already exists. |
3030 | 3035 |
3031 Args: | 3036 Args: |
3032 xpath: An xpath specifying the DOM node to watch. | 3037 xpath: An xpath specifying the DOM node to watch. |
3033 attribute: Attribute to match |expected_value| against, if given. Defaults | 3038 attribute: Attribute to match |expected_value| against, if given. Defaults |
3034 to 'textContent'. | 3039 to 'textContent'. |
3035 expected_value: Optional regular expression to match against the node's | 3040 expected_value: Optional regular expression to match against the node's |
3036 textContent attribute. Defaults to None. | 3041 textContent attribute. Defaults to None. |
3042 exec_js: A callable of the form f(self, js, **kwargs) used to inject the | |
3043 MutationObserver javascript. Defaults to None, which uses | |
3044 PyUITest.ExecuteJavascript. | |
3037 timeout: Time to wait for the node to exist before raising an exception, | 3045 timeout: Time to wait for the node to exist before raising an exception, |
3038 defaults to the default automation timeout. | 3046 defaults to the default automation timeout. |
3039 | 3047 |
3040 Any additional keyword arguments are passed on to ExecuteJavascript and | 3048 Any additional keyword arguments are passed on to ExecuteJavascript and |
3041 can be used to select the tab where the DOM MutationObserver is created. | 3049 can be used to select the tab where the DOM MutationObserver is created. |
3042 | 3050 |
3043 Raises: | 3051 Raises: |
3044 pyauto_errors.JSONInterfaceError if the automation call returns an error. | 3052 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
3045 pyauto_errors.JavascriptRuntimeError if the injected javascript | 3053 pyauto_errors.JavascriptRuntimeError if the injected javascript |
3046 MutationObserver returns an error. | 3054 MutationObserver returns an error. |
3047 """ | 3055 """ |
3048 observer_id = self.AddDomMutationObserver('exists', xpath, attribute, | 3056 observer_id = self.AddDomMutationObserver('exists', xpath, attribute, |
3049 expected_value, **kwargs) | 3057 expected_value, exec_js=exec_js, |
3058 **kwargs) | |
3050 self.GetNextEvent(observer_id, timeout=timeout) | 3059 self.GetNextEvent(observer_id, timeout=timeout) |
3051 | 3060 |
3052 def GetNextEvent(self, observer_id=-1, blocking=True, timeout=-1): | 3061 def GetNextEvent(self, observer_id=-1, blocking=True, timeout=-1): |
3053 """Waits for an observed event to occur. | 3062 """Waits for an observed event to occur. |
3054 | 3063 |
3055 The returned event is removed from the Event Queue. If there is already a | 3064 The returned event is removed from the Event Queue. If there is already a |
3056 matching event in the queue it is returned immediately, otherwise the call | 3065 matching event in the queue it is returned immediately, otherwise the call |
3057 blocks until a matching event occurs. If blocking is disabled and no | 3066 blocks until a matching event occurs. If blocking is disabled and no |
3058 matching event is in the queue this function will immediately return None. | 3067 matching event is in the queue this function will immediately return None. |
3059 | 3068 |
(...skipping 2339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5399 successful = result.wasSuccessful() | 5408 successful = result.wasSuccessful() |
5400 if not successful: | 5409 if not successful: |
5401 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) | 5410 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) |
5402 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ | 5411 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ |
5403 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) | 5412 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) |
5404 sys.exit(not successful) | 5413 sys.exit(not successful) |
5405 | 5414 |
5406 | 5415 |
5407 if __name__ == '__main__': | 5416 if __name__ == '__main__': |
5408 Main() | 5417 Main() |
OLD | NEW |