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 2897 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2908 | 2908 |
2909 def AddDomMutationObserver(self, mutation_type, selector, | 2909 def AddDomMutationObserver(self, mutation_type, selector, |
2910 expected_value=None, automation_id=44444, | 2910 expected_value=None, automation_id=44444, |
2911 **kwargs): | 2911 **kwargs): |
2912 """Sets up an event observer watching for a specific DOM mutation. | 2912 """Sets up an event observer watching for a specific DOM mutation. |
2913 | 2913 |
2914 Creates an observer that raises an event when a mutation of the given type | 2914 Creates an observer that raises an event when a mutation of the given type |
2915 occurs on a DOM node specified by |selector|. | 2915 occurs on a DOM node specified by |selector|. |
2916 | 2916 |
2917 Args: | 2917 Args: |
2918 mutation_type: One of 'add', 'remove', or 'change'. | 2918 mutation_type: One of 'add', 'remove', 'change', or 'exists'. |
2919 selector: A DOMSelector object defining the DOM node to watch. The node | 2919 selector: A DOMSelector object defining the DOM node to watch. The node |
2920 must already exist if |mutation_type| is 'change'. | 2920 must already exist if |mutation_type| is 'change'. |
2921 expected_value: Optional regular expression to match against the node's | 2921 expected_value: Optional regular expression to match against the node's |
2922 textContent attribute after the mutation. Defaults to None. | 2922 textContent attribute after the mutation. Defaults to None. |
2923 automation_id: The automation_id used to route the observer javascript | 2923 automation_id: The automation_id used to route the observer javascript |
2924 messages. Defaults to 44444. | 2924 messages. Defaults to 44444. |
2925 | 2925 |
2926 Any additional keyword arguments are passed on to ExecuteJavascript and | 2926 Any additional keyword arguments are passed on to ExecuteJavascript and |
2927 can be used to select the tab where the DOM MutationObserver is created. | 2927 can be used to select the tab where the DOM MutationObserver is created. |
2928 | 2928 |
2929 Returns: | 2929 Returns: |
2930 The id of the created observer, which can be used with GetNextEvent(id) | 2930 The id of the created observer, which can be used with GetNextEvent(id) |
2931 and RemoveEventObserver(id). | 2931 and RemoveEventObserver(id). |
2932 | 2932 |
2933 Raises: | 2933 Raises: |
2934 pyauto_errors.JSONInterfaceError if the automation call returns an error. | 2934 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
2935 RuntimeError if the injected javascript MutationObserver returns an error. | 2935 RuntimeError if the injected javascript MutationObserver returns an error. |
2936 """ | 2936 """ |
2937 assert mutation_type in ('add', 'remove', 'change'), \ | 2937 assert mutation_type in ('add', 'remove', 'change', 'exists'), \ |
2938 'Unexpected value "%s" for mutation_type.' % mutation_type | 2938 'Unexpected value "%s" for mutation_type.' % mutation_type |
2939 assert isinstance(selector, domselector.DOMSelector), \ | 2939 assert isinstance(selector, domselector.DOMSelector), \ |
2940 'Unexpected type: selector is not a instance of DOMSelector.' | 2940 'Unexpected type: selector is not a instance of DOMSelector.' |
2941 assert '"' not in selector.pattern, \ | 2941 assert '"' not in selector.pattern, \ |
2942 'Do not use character " in selector.' | 2942 'Do not use character " in selector.' |
2943 assert not expected_value or '"' not in expected_value, \ | 2943 assert not expected_value or '"' not in expected_value, \ |
2944 'Do not use character " in expected_value.' | 2944 'Do not use character " in expected_value.' |
2945 cmd_dict = { | 2945 cmd_dict = { |
2946 'command': 'AddDomEventObserver', | 2946 'command': 'AddDomEventObserver', |
2947 'event_name': '__dom_mutation_observer__:$(id)', | 2947 'event_name': '__dom_mutation_observer__:$(id)', |
2948 'automation_id': automation_id, | 2948 'automation_id': automation_id, |
2949 'recurring': False, | 2949 'recurring': False, |
2950 } | 2950 } |
2951 observer_id = ( | 2951 observer_id = ( |
2952 self._GetResultFromJSONRequest(cmd_dict, windex=None)['observer_id']) | 2952 self._GetResultFromJSONRequest(cmd_dict, windex=None)['observer_id']) |
2953 jsfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), | 2953 jsfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), |
2954 'dom_mutation_observer.js') | 2954 'dom_mutation_observer.js') |
2955 with open(jsfile, 'r') as f: | 2955 with open(jsfile, 'r') as f: |
2956 js = ('(' + f.read() + ')(%d, %d, "%s", "%s", "%s", %s);' % | 2956 js = ('(' + f.read() + ')(%d, %d, "%s", "%s", "%s", %s);' % |
2957 (automation_id, observer_id, mutation_type, | 2957 (automation_id, observer_id, mutation_type, |
2958 selector.pattern, selector.type_string, | 2958 selector.pattern, selector.type_string, |
2959 'null' if expected_value is None else '"%s"' % expected_value)) | 2959 'null' if expected_value is None else '"%s"' % expected_value)) |
2960 jsreturn = self.ExecuteJavascript(js, **kwargs) | 2960 jsreturn = self.ExecuteJavascript(js, **kwargs) |
2961 if jsreturn != 'success': | 2961 if jsreturn != 'success': |
2962 self.RemoveEventObserver(observer_id) | 2962 self.RemoveEventObserver(observer_id) |
2963 raise RuntimeError(jsreturn) | 2963 raise RuntimeError(jsreturn) |
2964 return observer_id | 2964 return observer_id |
2965 | 2965 |
2966 def WaitForDomNode(self, selector, expected_value=None, timeout=-1, **kwargs): | |
2967 """Waits until a node matching selector exists in the DOM. | |
Nirnimesh
2012/04/16 23:14:12
Add:
NOTE: This does NOT poll. It returns as soon
craigdh
2012/04/16 23:38:08
Done.
| |
2968 | |
2969 Args: | |
2970 selector: A DOMSelector object defining the DOM node to wait for. | |
2971 expected_value: Optional regular expression to match against the node's | |
2972 textContent attribute. Defaults to None. | |
2973 timeout: Time to wait for the node to exist before raising an exception, | |
2974 defaults to the default automation timeout. | |
2975 | |
2976 Any additional keyword arguments are passed on to ExecuteJavascript and | |
2977 can be used to select the tab where the DOM MutationObserver is created. | |
2978 | |
2979 Raises: | |
2980 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
2981 RuntimeError if the injected javascript MutationObserver returns an error. | |
2982 """ | |
2983 observer_id = self.AddDomMutationObserver('exists', selector, | |
2984 expected_value, **kwargs) | |
2985 self.GetNextEvent(observer_id, timeout=timeout) | |
2986 | |
2966 def GetNextEvent(self, observer_id=-1, blocking=True, timeout=-1): | 2987 def GetNextEvent(self, observer_id=-1, blocking=True, timeout=-1): |
2967 """Waits for an observed event to occur. | 2988 """Waits for an observed event to occur. |
2968 | 2989 |
2969 The returned event is removed from the Event Queue. If there is already a | 2990 The returned event is removed from the Event Queue. If there is already a |
2970 matching event in the queue it is returned immediately, otherwise the call | 2991 matching event in the queue it is returned immediately, otherwise the call |
2971 blocks until a matching event occurs. If blocking is disabled and no | 2992 blocks until a matching event occurs. If blocking is disabled and no |
2972 matching event is in the queue this function will immediately return None. | 2993 matching event is in the queue this function will immediately return None. |
2973 | 2994 |
2974 Args: | 2995 Args: |
2975 observer_id: The id of the observer to wait for, matches any event by | 2996 observer_id: The id of the observer to wait for, matches any event by |
(...skipping 2312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5288 successful = result.wasSuccessful() | 5309 successful = result.wasSuccessful() |
5289 if not successful: | 5310 if not successful: |
5290 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) | 5311 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) |
5291 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ | 5312 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ |
5292 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) | 5313 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) |
5293 sys.exit(not successful) | 5314 sys.exit(not successful) |
5294 | 5315 |
5295 | 5316 |
5296 if __name__ == '__main__': | 5317 if __name__ == '__main__': |
5297 Main() | 5318 Main() |
OLD | NEW |