| 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 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1369 | 1439 |
| 1370 Returns: | 1440 Returns: |
| 1371 The tab URL as a GURL object. | 1441 The tab URL as a GURL object. |
| 1372 | 1442 |
| 1373 Raises: | 1443 Raises: |
| 1374 pyauto_errors.JSONInterfaceError if the automation call returns an error. | 1444 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1375 """ | 1445 """ |
| 1376 return GURL(str(self.GetTabInfo(self.GetActiveTabIndex(windex), | 1446 return GURL(str(self.GetTabInfo(self.GetActiveTabIndex(windex), |
| 1377 windex)['url'])) | 1447 windex)['url'])) |
| 1378 | 1448 |
| 1449 def ActionOnSSLBlockingPage(self, tab_index=0, windex=0, proceed=True): |
| 1450 """Take action on an interstitial page. |
| 1451 |
| 1452 Calling this when an interstitial page is not showing is an error. |
| 1453 |
| 1454 Args: |
| 1455 tab_index: Integer index of the tab to activate; defaults to 0. |
| 1456 windex: Integer index of the browser window to use; defaults to the first |
| 1457 window. |
| 1458 proceed: Whether to proceed to the URL or not. |
| 1459 |
| 1460 Raises: |
| 1461 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1462 """ |
| 1463 cmd_dict = { |
| 1464 'command': 'ActionOnSSLBlockingPage', |
| 1465 'tab_index': tab_index, |
| 1466 'windex': windex, |
| 1467 'proceed': proceed, |
| 1468 } |
| 1469 return self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1470 |
| 1379 def GetBookmarkModel(self, windex=0): | 1471 def GetBookmarkModel(self, windex=0): |
| 1380 """Return the bookmark model as a BookmarkModel object. | 1472 """Return the bookmark model as a BookmarkModel object. |
| 1381 | 1473 |
| 1382 This is a snapshot of the bookmark model; it is not a proxy and | 1474 This is a snapshot of the bookmark model; it is not a proxy and |
| 1383 does not get updated as the bookmark model changes. | 1475 does not get updated as the bookmark model changes. |
| 1384 """ | 1476 """ |
| 1385 bookmarks_as_json = self._GetBookmarksAsJSON(windex) | 1477 bookmarks_as_json = self._GetBookmarksAsJSON(windex) |
| 1386 if not bookmarks_as_json: | 1478 if not bookmarks_as_json: |
| 1387 raise JSONInterfaceError('Could not resolve browser proxy.') | 1479 raise JSONInterfaceError('Could not resolve browser proxy.') |
| 1388 return bookmark_model.BookmarkModel(bookmarks_as_json) | 1480 return bookmark_model.BookmarkModel(bookmarks_as_json) |
| (...skipping 906 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2295 NORMAL_PAGE | 2387 NORMAL_PAGE |
| 2296 ERROR_PAGE | 2388 ERROR_PAGE |
| 2297 INTERSTITIAL_PAGE | 2389 INTERSTITIAL_PAGE |
| 2298 """ | 2390 """ |
| 2299 cmd_dict = { # Prepare command for the json interface | 2391 cmd_dict = { # Prepare command for the json interface |
| 2300 'command': 'GetNavigationInfo', | 2392 'command': 'GetNavigationInfo', |
| 2301 'tab_index': tab_index, | 2393 'tab_index': tab_index, |
| 2302 } | 2394 } |
| 2303 return self._GetResultFromJSONRequest(cmd_dict, windex=windex) | 2395 return self._GetResultFromJSONRequest(cmd_dict, windex=windex) |
| 2304 | 2396 |
| 2397 def GetSecurityState(self, tab_index=0, windex=0): |
| 2398 """Get security details for a given tab. |
| 2399 |
| 2400 Args: |
| 2401 tab_index: The tab index, default is 0. |
| 2402 window_index: The window index, default is 0. |
| 2403 |
| 2404 Returns: |
| 2405 a dictionary. |
| 2406 Sample: |
| 2407 { "security_style": SECURITY_STYLE_AUTHENTICATED, |
| 2408 "ssl_cert_status": 3, // bitmask of status flags |
| 2409 "insecure_content_status": 1, // bitmask of status flags |
| 2410 } |
| 2411 """ |
| 2412 cmd_dict = { # Prepare command for the json interface |
| 2413 'command': 'GetSecurityState', |
| 2414 'tab_index': tab_index, |
| 2415 'windex': windex, |
| 2416 } |
| 2417 return self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 2418 |
| 2305 def GetHistoryInfo(self, search_text=''): | 2419 def GetHistoryInfo(self, search_text=''): |
| 2306 """Return info about browsing history. | 2420 """Return info about browsing history. |
| 2307 | 2421 |
| 2308 Args: | 2422 Args: |
| 2309 search_text: the string to search in history. Defaults to empty string | 2423 search_text: the string to search in history. Defaults to empty string |
| 2310 which means that all history would be returned. This is | 2424 which means that all history would be returned. This is |
| 2311 functionally equivalent to searching for a text in the | 2425 functionally equivalent to searching for a text in the |
| 2312 chrome://history UI. So partial matches work too. | 2426 chrome://history UI. So partial matches work too. |
| 2313 When non-empty, the history items returned will contain a | 2427 When non-empty, the history items returned will contain a |
| 2314 "snippet" field corresponding to the snippet visible in | 2428 "snippet" field corresponding to the snippet visible in |
| (...skipping 4143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6458 successful = result.wasSuccessful() | 6572 successful = result.wasSuccessful() |
| 6459 if not successful: | 6573 if not successful: |
| 6460 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) | 6574 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) |
| 6461 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ | 6575 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ |
| 6462 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) | 6576 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) |
| 6463 sys.exit(not successful) | 6577 sys.exit(not successful) |
| 6464 | 6578 |
| 6465 | 6579 |
| 6466 if __name__ == '__main__': | 6580 if __name__ == '__main__': |
| 6467 Main() | 6581 Main() |
| OLD | NEW |