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 740 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
751 | 751 |
752 Returns: | 752 Returns: |
753 a dictionary of items from private_tests_info.txt | 753 a dictionary of items from private_tests_info.txt |
754 """ | 754 """ |
755 private_file = os.path.join( | 755 private_file = os.path.join( |
756 PyUITest.DataDir(), 'pyauto_private', 'private_tests_info.txt') | 756 PyUITest.DataDir(), 'pyauto_private', 'private_tests_info.txt') |
757 assert os.path.exists(private_file), '%s missing' % private_file | 757 assert os.path.exists(private_file), '%s missing' % private_file |
758 return PyUITest.EvalDataFrom(private_file) | 758 return PyUITest.EvalDataFrom(private_file) |
759 | 759 |
760 def WaitUntil(self, function, timeout=-1, retry_sleep=0.25, args=[], | 760 def WaitUntil(self, function, timeout=-1, retry_sleep=0.25, args=[], |
761 expect_retval=None, debug=True): | 761 expect_retval=None, keep_retval=False, debug=True): |
762 """Poll on a condition until timeout. | 762 """Poll on a condition until timeout. |
763 | 763 |
764 Waits until the |function| evalues to |expect_retval| or until |timeout| | 764 Waits until the |function| evalues to |expect_retval| or until |timeout| |
765 secs, whichever occurs earlier. | 765 secs, whichever occurs earlier. |
766 | 766 |
767 This is better than using a sleep, since it waits (almost) only as much | 767 This is better than using a sleep, since it waits (almost) only as much |
768 as needed. | 768 as needed. |
769 | 769 |
770 WARNING: This method call should be avoided as far as possible in favor | 770 WARNING: This method call should be avoided as far as possible in favor |
771 of a real wait from chromium (like wait-until-page-loaded). | 771 of a real wait from chromium (like wait-until-page-loaded). |
(...skipping 12 matching lines...) Expand all Loading... | |
784 action is to wait for kWaitForActionMaxMsec, as set in | 784 action is to wait for kWaitForActionMaxMsec, as set in |
785 ui_test.cc | 785 ui_test.cc |
786 Use None to wait indefinitely. | 786 Use None to wait indefinitely. |
787 retry_sleep: the sleep interval (in secs) before retrying |function|. | 787 retry_sleep: the sleep interval (in secs) before retrying |function|. |
788 Defaults to 0.25 secs. | 788 Defaults to 0.25 secs. |
789 args: the args to pass to |function| | 789 args: the args to pass to |function| |
790 expect_retval: the expected return value for |function|. This forms the | 790 expect_retval: the expected return value for |function|. This forms the |
791 exit criteria. In case this is None (the default), | 791 exit criteria. In case this is None (the default), |
792 |function|'s return value is checked for truth, | 792 |function|'s return value is checked for truth, |
793 so 'non-empty-string' should match with True | 793 so 'non-empty-string' should match with True |
794 keep_retval: Boolean indicating whether to return the retval of the last | |
795 iteration of |function| that succeeded without a timeout, or | |
796 True. | |
794 debug: if True, displays debug info at each retry. | 797 debug: if True, displays debug info at each retry. |
795 | 798 |
796 Returns: | 799 Returns: |
797 True, if returning when |function| evaluated to True | 800 The return value of the calling function when |function| evaluates to True |
801 and the keep_retval is True, or True when keep_retval is false. | |
798 False, when returning due to timeout | 802 False, when returning due to timeout |
799 """ | 803 """ |
800 if timeout == -1: # Default | 804 if timeout == -1: # Default |
801 timeout = self.action_max_timeout_ms() / 1000.0 | 805 timeout = self.action_max_timeout_ms() / 1000.0 |
802 assert callable(function), "function should be a callable" | 806 assert callable(function), "function should be a callable" |
803 begin = time.time() | 807 begin = time.time() |
804 debug_begin = begin | 808 debug_begin = begin |
805 while timeout is None or time.time() - begin <= timeout: | 809 while timeout is None or time.time() - begin <= timeout: |
806 retval = function(*args) | 810 retval = function(*args) |
807 if (expect_retval is None and retval) or expect_retval == retval: | 811 if (expect_retval is None and retval) or \ |
808 return True | 812 (expect_retval is not None and expect_retval == retval): |
813 return {True: retval, False: True}[keep_retval] | |
Nirnimesh
2012/08/08 19:51:13
I think it'll be fine if you just return retval he
stanleyw
2012/08/08 21:38:35
Done.
| |
809 if debug and time.time() - debug_begin > 5: | 814 if debug and time.time() - debug_begin > 5: |
810 debug_begin += 5 | 815 debug_begin += 5 |
811 if function.func_name == (lambda: True).func_name: | 816 if function.func_name == (lambda: True).func_name: |
812 function_info = inspect.getsource(function).strip() | 817 function_info = inspect.getsource(function).strip() |
813 else: | 818 else: |
814 function_info = '%s()' % function.func_name | 819 function_info = '%s()' % function.func_name |
815 logging.debug('WaitUntil(%s:%d %s) still waiting. ' | 820 logging.debug('WaitUntil(%s:%d %s) still waiting. ' |
816 'Expecting %s. Last returned %s.', | 821 'Expecting %s. Last returned %s.', |
817 os.path.basename(inspect.getsourcefile(function)), | 822 os.path.basename(inspect.getsourcefile(function)), |
818 inspect.getsourcelines(function)[1], | 823 inspect.getsourcelines(function)[1], |
(...skipping 4352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5171 | 5176 |
5172 Returns: | 5177 Returns: |
5173 The SSID of the connected network or None if we're not connected. | 5178 The SSID of the connected network or None if we're not connected. |
5174 """ | 5179 """ |
5175 service_list = self.GetNetworkInfo() | 5180 service_list = self.GetNetworkInfo() |
5176 connected_service_path = service_list.get('connected_wifi') | 5181 connected_service_path = service_list.get('connected_wifi') |
5177 if 'wifi_networks' in service_list and \ | 5182 if 'wifi_networks' in service_list and \ |
5178 connected_service_path in service_list['wifi_networks']: | 5183 connected_service_path in service_list['wifi_networks']: |
5179 return service_list['wifi_networks'][connected_service_path]['name'] | 5184 return service_list['wifi_networks'][connected_service_path]['name'] |
5180 | 5185 |
5181 def GetServicePath(self, ssid): | 5186 def GetServicePath(self, ssid, encryption=None, timeout=30): |
5182 """Returns the service path associated with an SSID. | 5187 """Waits until the SSID is observed and returns its service path. |
5183 | 5188 |
5184 Args: | 5189 Args: |
5185 ssid: String defining the SSID we are searching for. | 5190 ssid: String defining the SSID we are searching for. |
5191 encryption: Encryption type of the network; either None to return the | |
5192 first instance of network that matches the ssid, '' for | |
5193 an empty network, 'PSK', 'WEP' or '8021X'. | |
5194 timeout: Duration to wait for ssid to appear. | |
5186 | 5195 |
5187 Returns: | 5196 Returns: |
5188 The service path or None if SSID does not exist. | 5197 The service path or None if SSID does not exist after timeout period. |
5189 """ | 5198 """ |
5190 service_list = self.GetNetworkInfo() | 5199 def _get_service_path(): |
5191 service_list = service_list.get('wifi_networks', []) | 5200 service_list = self.GetNetworkInfo().get('wifi_networks', []) |
5192 for service_path, service_obj in service_list.iteritems(): | 5201 for service_path, service_obj in service_list.iteritems(): |
5193 if service_obj['name'] == ssid: | 5202 service_encr = 'PSK' if service_obj['encryption'] in ['WPA', 'RSN'] \ |
5194 return service_path | 5203 else service_obj['encryption'] |
5195 return None | 5204 |
5205 if service_obj['name'] == ssid and \ | |
5206 (encryption == None or service_encr == encryption): | |
5207 return service_path | |
5208 self.NetworkScan() | |
5209 return None | |
5210 | |
5211 service_path = self.WaitUntil(_get_service_path, keep_retval=True, | |
5212 timeout=timeout, retry_sleep=1) | |
5213 return service_path or None | |
5196 | 5214 |
5197 def NetworkScan(self): | 5215 def NetworkScan(self): |
5198 """Causes ChromeOS to scan for available wifi networks. | 5216 """Causes ChromeOS to scan for available wifi networks. |
5199 | 5217 |
5200 Blocks until scanning is complete. | 5218 Blocks until scanning is complete. |
5201 | 5219 |
5202 Returns: | 5220 Returns: |
5203 The new list of networks obtained from GetNetworkInfo(). | 5221 The new list of networks obtained from GetNetworkInfo(). |
5204 | 5222 |
5205 Raises: | 5223 Raises: |
(...skipping 1453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6659 successful = result.wasSuccessful() | 6677 successful = result.wasSuccessful() |
6660 if not successful: | 6678 if not successful: |
6661 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) | 6679 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) |
6662 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ | 6680 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ |
6663 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) | 6681 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) |
6664 sys.exit(not successful) | 6682 sys.exit(not successful) |
6665 | 6683 |
6666 | 6684 |
6667 if __name__ == '__main__': | 6685 if __name__ == '__main__': |
6668 Main() | 6686 Main() |
OLD | NEW |