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 1066 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1077 additional_info)) | 1077 additional_info)) |
1078 ret_dict = json.loads(result) | 1078 ret_dict = json.loads(result) |
1079 if ret_dict.has_key('error'): | 1079 if ret_dict.has_key('error'): |
1080 raise JSONInterfaceError(ret_dict['error']) | 1080 raise JSONInterfaceError(ret_dict['error']) |
1081 return ret_dict | 1081 return ret_dict |
1082 | 1082 |
1083 def NavigateToURL(self, url, windex=0, tab_index=None, navigation_count=1): | 1083 def NavigateToURL(self, url, windex=0, tab_index=None, navigation_count=1): |
1084 """Navigate the given tab to the given URL. | 1084 """Navigate the given tab to the given URL. |
1085 | 1085 |
1086 Note that this method also activates the corresponding tab/window if it's | 1086 Note that this method also activates the corresponding tab/window if it's |
1087 not active already. Blocks until page has loaded. | 1087 not active already. Blocks until |navigation_count| navigations have |
| 1088 completed. |
1088 | 1089 |
1089 Args: | 1090 Args: |
1090 url: The URL to which to navigate, can be a string or GURL object. | 1091 url: The URL to which to navigate, can be a string or GURL object. |
1091 windex: The index of the browser window to work on. Defaults to the first | 1092 windex: The index of the browser window to work on. Defaults to the first |
1092 window. | 1093 window. |
1093 tab_index: The index of the tab to work on. Defaults to the active tab. | 1094 tab_index: The index of the tab to work on. Defaults to the active tab. |
1094 navigation_count: the number of navigations to wait for. Defaults to 1. | 1095 navigation_count: the number of navigations to wait for. Defaults to 1. |
1095 | 1096 |
1096 Raises: | 1097 Raises: |
1097 pyauto_errors.JSONInterfaceError if the automation call returns an error. | 1098 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
1098 """ | 1099 """ |
1099 if isinstance(url, GURL): | 1100 if isinstance(url, GURL): |
1100 url = url.spec() | 1101 url = url.spec() |
1101 if tab_index is None: | 1102 if tab_index is None: |
1102 tab_index = self.GetActiveTabIndex(windex) | 1103 tab_index = self.GetActiveTabIndex(windex) |
1103 cmd_dict = { | 1104 cmd_dict = { |
1104 'command': 'NavigateToURL', | 1105 'command': 'NavigateToURL', |
1105 'url': url, | 1106 'url': url, |
1106 'windex': windex, | 1107 'windex': windex, |
1107 'tab_index': tab_index, | 1108 'tab_index': tab_index, |
1108 'navigation_count': navigation_count, | 1109 'navigation_count': navigation_count, |
1109 } | 1110 } |
1110 self._GetResultFromJSONRequest(cmd_dict, windex=None) | 1111 self._GetResultFromJSONRequest(cmd_dict, windex=None) |
1111 | 1112 |
| 1113 def NavigateToURLAsync(self, url, windex=0, tab_index=None): |
| 1114 """Initiate a URL navigation. |
| 1115 |
| 1116 A wrapper for NavigateToURL with navigation_count set to 0. |
| 1117 """ |
| 1118 self.NavigateToURL(url, windex, tab_index, 0) |
| 1119 |
1112 def ApplyAccelerator(self, accelerator, windex=0): | 1120 def ApplyAccelerator(self, accelerator, windex=0): |
1113 """Apply the accelerator with the given id. | 1121 """Apply the accelerator with the given id. |
1114 | 1122 |
1115 Note that this method schedules the accelerator, but does not wait for it to | 1123 Note that this method schedules the accelerator, but does not wait for it to |
1116 actually finish doing anything. | 1124 actually finish doing anything. |
1117 | 1125 |
1118 Args: | 1126 Args: |
1119 accelerator: The accelerator id, IDC_BACK, IDC_NEWTAB, etc. The list of | 1127 accelerator: The accelerator id, IDC_BACK, IDC_NEWTAB, etc. The list of |
1120 ids can be found at chrome/app/chrome_command_ids.h. | 1128 ids can be found at chrome/app/chrome_command_ids.h. |
1121 windex: The index of the browser window to work on. Defaults to the first | 1129 windex: The index of the browser window to work on. Defaults to the first |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1169 Returns: | 1177 Returns: |
1170 True if the command is enabled for the given window. | 1178 True if the command is enabled for the given window. |
1171 """ | 1179 """ |
1172 cmd_dict = { | 1180 cmd_dict = { |
1173 'command': 'IsMenuCommandEnabled', | 1181 'command': 'IsMenuCommandEnabled', |
1174 'accelerator': accelerator, | 1182 'accelerator': accelerator, |
1175 'windex': windex, | 1183 'windex': windex, |
1176 } | 1184 } |
1177 return self._GetResultFromJSONRequest(cmd_dict, windex=None).get('enabled') | 1185 return self._GetResultFromJSONRequest(cmd_dict, windex=None).get('enabled') |
1178 | 1186 |
| 1187 def TabGoForward(self, tab_index=0, windex=0): |
| 1188 """Navigate a tab forward in history. |
| 1189 |
| 1190 Equivalent to clicking the Forward button in the UI. Activates the tab as a |
| 1191 side effect. |
| 1192 |
| 1193 Raises: |
| 1194 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1195 """ |
| 1196 self.ActivateTab(tab_index, windex) |
| 1197 self.RunCommand(IDC_FORWARD, windex) |
| 1198 |
| 1199 def TabGoBack(self, tab_index=0, windex=0): |
| 1200 """Navigate a tab backwards in history. |
| 1201 |
| 1202 Equivalent to clicking the Back button in the UI. Activates the tab as a |
| 1203 side effect. |
| 1204 |
| 1205 Raises: |
| 1206 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1207 """ |
| 1208 self.ActivateTab(tab_index, windex) |
| 1209 self.RunCommand(IDC_BACK, windex) |
| 1210 |
1179 def ReloadTab(self, tab_index=0, windex=0): | 1211 def ReloadTab(self, tab_index=0, windex=0): |
1180 """Reload the given tab. | 1212 """Reload the given tab. |
1181 | 1213 |
1182 Blocks until the page has reloaded. | 1214 Blocks until the page has reloaded. |
1183 | 1215 |
1184 Args: | 1216 Args: |
1185 tab_index: The index of the tab to reload. Defaults to 0. | 1217 tab_index: The index of the tab to reload. Defaults to 0. |
1186 windex: The index of the browser window to work on. Defaults to the first | 1218 windex: The index of the browser window to work on. Defaults to the first |
1187 window. | 1219 window. |
1188 | 1220 |
1189 Raises: | 1221 Raises: |
1190 pyauto_errors.JSONInterfaceError if the automation call returns an error. | 1222 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
1191 """ | 1223 """ |
| 1224 self.ActivateTab(tab_index, windex) |
| 1225 self.RunCommand(IDC_RELOAD, windex) |
| 1226 |
| 1227 def CloseTab(self, tab_index=0, windex=0, wait_until_closed=True): |
| 1228 """Close the given tab. |
| 1229 |
| 1230 Note: Be careful closing the last tab in a window as it may close the |
| 1231 browser. |
| 1232 |
| 1233 Args: |
| 1234 tab_index: The index of the tab to reload. Defaults to 0. |
| 1235 windex: The index of the browser window to work on. Defaults to the first |
| 1236 window. |
| 1237 wait_until_closed: Whether to block until the tab finishes closing. |
| 1238 |
| 1239 Raises: |
| 1240 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1241 """ |
1192 cmd_dict = { | 1242 cmd_dict = { |
1193 'command': 'Reload', | 1243 'command': 'CloseTab', |
| 1244 'tab_index': tab_index, |
| 1245 'windex': windex, |
| 1246 'wait_until_closed': wait_until_closed, |
| 1247 } |
| 1248 self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1249 |
| 1250 def WaitForTabToBeRestored(self, tab_index=0, windex=0, timeout=-1): |
| 1251 """Wait for the given tab to be restored. |
| 1252 |
| 1253 Args: |
| 1254 tab_index: The index of the tab to reload. Defaults to 0. |
| 1255 windex: The index of the browser window to work on. Defaults to the first |
| 1256 window. |
| 1257 timeout: Timeout in milliseconds. |
| 1258 |
| 1259 Raises: |
| 1260 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1261 """ |
| 1262 cmd_dict = { |
| 1263 'command': 'CloseTab', |
1194 'tab_index': tab_index, | 1264 'tab_index': tab_index, |
1195 'windex': windex, | 1265 'windex': windex, |
1196 } | 1266 } |
1197 self._GetResultFromJSONRequest(cmd_dict, windex=None) | 1267 self._GetResultFromJSONRequest(cmd_dict, windex=None, timeout=timeout) |
1198 | 1268 |
1199 def ReloadActiveTab(self, windex=0): | 1269 def ReloadActiveTab(self, windex=0): |
1200 """Reload an active tab. | 1270 """Reload an active tab. |
1201 | 1271 |
1202 Warning: Depending on the concept of an active tab is dangerous as it can | 1272 Warning: Depending on the concept of an active tab is dangerous as it can |
1203 change during the test. Use ReloadTab and supply a tab_index explicitly. | 1273 change during the test. Use ReloadTab and supply a tab_index explicitly. |
1204 | 1274 |
1205 Args: | 1275 Args: |
1206 windex: The index of the browser window to work on. Defaults to the first | 1276 windex: The index of the browser window to work on. Defaults to the first |
1207 window. | 1277 window. |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1414 | 1484 |
1415 Returns: | 1485 Returns: |
1416 The tab URL as a GURL object. | 1486 The tab URL as a GURL object. |
1417 | 1487 |
1418 Raises: | 1488 Raises: |
1419 pyauto_errors.JSONInterfaceError if the automation call returns an error. | 1489 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
1420 """ | 1490 """ |
1421 return GURL(str(self.GetTabInfo(self.GetActiveTabIndex(windex), | 1491 return GURL(str(self.GetTabInfo(self.GetActiveTabIndex(windex), |
1422 windex)['url'])) | 1492 windex)['url'])) |
1423 | 1493 |
| 1494 def ActionOnSSLBlockingPage(self, tab_index=0, windex=0, proceed=True): |
| 1495 """Take action on an interstitial page. |
| 1496 |
| 1497 Calling this when an interstitial page is not showing is an error. |
| 1498 |
| 1499 Args: |
| 1500 tab_index: Integer index of the tab to activate; defaults to 0. |
| 1501 windex: Integer index of the browser window to use; defaults to the first |
| 1502 window. |
| 1503 proceed: Whether to proceed to the URL or not. |
| 1504 |
| 1505 Raises: |
| 1506 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1507 """ |
| 1508 cmd_dict = { |
| 1509 'command': 'ActionOnSSLBlockingPage', |
| 1510 'tab_index': tab_index, |
| 1511 'windex': windex, |
| 1512 'proceed': proceed, |
| 1513 } |
| 1514 return self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1515 |
1424 def GetBookmarkModel(self, windex=0): | 1516 def GetBookmarkModel(self, windex=0): |
1425 """Return the bookmark model as a BookmarkModel object. | 1517 """Return the bookmark model as a BookmarkModel object. |
1426 | 1518 |
1427 This is a snapshot of the bookmark model; it is not a proxy and | 1519 This is a snapshot of the bookmark model; it is not a proxy and |
1428 does not get updated as the bookmark model changes. | 1520 does not get updated as the bookmark model changes. |
1429 """ | 1521 """ |
1430 bookmarks_as_json = self._GetBookmarksAsJSON(windex) | 1522 bookmarks_as_json = self._GetBookmarksAsJSON(windex) |
1431 if not bookmarks_as_json: | 1523 if not bookmarks_as_json: |
1432 raise JSONInterfaceError('Could not resolve browser proxy.') | 1524 raise JSONInterfaceError('Could not resolve browser proxy.') |
1433 return bookmark_model.BookmarkModel(bookmarks_as_json) | 1525 return bookmark_model.BookmarkModel(bookmarks_as_json) |
(...skipping 906 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2340 NORMAL_PAGE | 2432 NORMAL_PAGE |
2341 ERROR_PAGE | 2433 ERROR_PAGE |
2342 INTERSTITIAL_PAGE | 2434 INTERSTITIAL_PAGE |
2343 """ | 2435 """ |
2344 cmd_dict = { # Prepare command for the json interface | 2436 cmd_dict = { # Prepare command for the json interface |
2345 'command': 'GetNavigationInfo', | 2437 'command': 'GetNavigationInfo', |
2346 'tab_index': tab_index, | 2438 'tab_index': tab_index, |
2347 } | 2439 } |
2348 return self._GetResultFromJSONRequest(cmd_dict, windex=windex) | 2440 return self._GetResultFromJSONRequest(cmd_dict, windex=windex) |
2349 | 2441 |
| 2442 def GetSecurityState(self, tab_index=0, windex=0): |
| 2443 """Get security details for a given tab. |
| 2444 |
| 2445 Args: |
| 2446 tab_index: The tab index, default is 0. |
| 2447 window_index: The window index, default is 0. |
| 2448 |
| 2449 Returns: |
| 2450 a dictionary. |
| 2451 Sample: |
| 2452 { "security_style": SECURITY_STYLE_AUTHENTICATED, |
| 2453 "ssl_cert_status": 3, // bitmask of status flags |
| 2454 "insecure_content_status": 1, // bitmask of status flags |
| 2455 } |
| 2456 """ |
| 2457 cmd_dict = { # Prepare command for the json interface |
| 2458 'command': 'GetSecurityState', |
| 2459 'tab_index': tab_index, |
| 2460 'windex': windex, |
| 2461 } |
| 2462 return self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 2463 |
2350 def GetHistoryInfo(self, search_text=''): | 2464 def GetHistoryInfo(self, search_text=''): |
2351 """Return info about browsing history. | 2465 """Return info about browsing history. |
2352 | 2466 |
2353 Args: | 2467 Args: |
2354 search_text: the string to search in history. Defaults to empty string | 2468 search_text: the string to search in history. Defaults to empty string |
2355 which means that all history would be returned. This is | 2469 which means that all history would be returned. This is |
2356 functionally equivalent to searching for a text in the | 2470 functionally equivalent to searching for a text in the |
2357 chrome://history UI. So partial matches work too. | 2471 chrome://history UI. So partial matches work too. |
2358 When non-empty, the history items returned will contain a | 2472 When non-empty, the history items returned will contain a |
2359 "snippet" field corresponding to the snippet visible in | 2473 "snippet" field corresponding to the snippet visible in |
(...skipping 4185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6545 successful = result.wasSuccessful() | 6659 successful = result.wasSuccessful() |
6546 if not successful: | 6660 if not successful: |
6547 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) | 6661 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) |
6548 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ | 6662 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ |
6549 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) | 6663 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) |
6550 sys.exit(not successful) | 6664 sys.exit(not successful) |
6551 | 6665 |
6552 | 6666 |
6553 if __name__ == '__main__': | 6667 if __name__ == '__main__': |
6554 Main() | 6668 Main() |
OLD | NEW |