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 1195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1206 Returns: | 1206 Returns: |
1207 An integer index for the currently active tab. | 1207 An integer index for the currently active tab. |
1208 """ | 1208 """ |
1209 cmd_dict = { | 1209 cmd_dict = { |
1210 'command': 'GetActiveTabIndex', | 1210 'command': 'GetActiveTabIndex', |
1211 'windex': windex, | 1211 'windex': windex, |
1212 } | 1212 } |
1213 return self._GetResultFromJSONRequest(cmd_dict, | 1213 return self._GetResultFromJSONRequest(cmd_dict, |
1214 windex=None).get('tab_index') | 1214 windex=None).get('tab_index') |
1215 | 1215 |
1216 def ActivateTab(self, tab_index=0, windex=0): | |
1217 """Activates the given tab in the specified window. | |
1218 | |
1219 Warning: Depending on the concept of an active tab is dangerous as it can | |
1220 change during the test. Instead use functions that accept a tab_index | |
1221 explicitly. | |
1222 | |
1223 Args: | |
1224 tab_index: Integer index of the tab to activate; defaults to the first | |
Nirnimesh
2012/07/18 19:22:56
'first tab' -> 0
craigdh
2012/07/18 19:45:25
Done.
| |
1225 tab. | |
1226 windex: Integer index of the browser window to use; defaults to the first | |
1227 window. | |
1228 | |
1229 Raises: | |
1230 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
1231 """ | |
1232 cmd_dict = { | |
1233 'command': 'ActivateTab', | |
1234 'tab_index': tab_index, | |
1235 'windex': windex, | |
1236 } | |
1237 self.BringBrowserToFront(windex) | |
Nirnimesh
2012/07/18 19:22:56
shouldn't this be done on the C++ side?
craigdh
2012/07/18 19:45:25
Bringing the browser to the front is available in
| |
1238 self._GetResultFromJSONRequest(cmd_dict, windex=None) | |
1239 | |
1240 def BringBrowserToFront(self, windex=0): | |
1241 """Activate the browser's window and bring it to front. | |
1242 | |
1243 Args: | |
1244 windex: Integer index of the browser window to use; defaults to the first | |
1245 window. | |
1246 | |
1247 Raises: | |
1248 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
1249 """ | |
1250 cmd_dict = { | |
1251 'command': 'BringBrowserToFront', | |
1252 'windex': windex, | |
1253 } | |
1254 self._GetResultFromJSONRequest(cmd_dict, windex=None) | |
1255 | |
1216 def AppendTab(self, url, windex=0): | 1256 def AppendTab(self, url, windex=0): |
1217 """Append a new tab. | 1257 """Append a new tab. |
1218 | 1258 |
1219 Creates a new tab at the end of given browser window and activates | 1259 Creates a new tab at the end of given browser window and activates |
1220 it. Blocks until the specified |url| is loaded. | 1260 it. Blocks until the specified |url| is loaded. |
1221 | 1261 |
1222 Args: | 1262 Args: |
1223 url: The url to load, can be string or a GURL object. | 1263 url: The url to load, can be string or a GURL object. |
1224 windex: The index of the browser window to work on. Defaults to the first | 1264 windex: The index of the browser window to work on. Defaults to the first |
1225 window. | 1265 window. |
1226 | 1266 |
1227 Returns: | 1267 Returns: |
1228 True if the url loads successfully in the new tab. False otherwise. | 1268 True if the url loads successfully in the new tab. False otherwise. |
1229 | 1269 |
1230 Raises: | 1270 Raises: |
1231 pyauto_errors.JSONInterfaceError if the automation call returns an error. | 1271 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
1232 """ | 1272 """ |
1233 if isinstance(url, GURL): | 1273 if isinstance(url, GURL): |
1234 url = url.spec() | 1274 url = url.spec() |
1235 cmd_dict = { | 1275 cmd_dict = { |
1236 'command': 'AppendTab', | 1276 'command': 'AppendTab', |
1237 'url': url, | 1277 'url': url, |
1238 'windex': windex, | 1278 'windex': windex, |
1239 } | 1279 } |
1240 return self._GetResultFromJSONRequest(cmd_dict, windex=None).get('result') | 1280 return self._GetResultFromJSONRequest(cmd_dict, windex=None).get('result') |
1241 | 1281 |
1282 def GetTabCount(self, windex=0): | |
1283 """Gets the number of tab in the given browser window. | |
1284 | |
1285 Args: | |
1286 windex: Integer index of the browser window to use; defaults to the first | |
1287 window. | |
1288 | |
1289 Returns: | |
1290 The tab count. | |
1291 | |
1292 Raises: | |
1293 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
1294 """ | |
1295 cmd_dict = { | |
1296 'command': 'GetTabCount', | |
1297 'windex': windex, | |
1298 } | |
1299 return self._GetResultFromJSONRequest(cmd_dict, windex=None)['tab_count'] | |
1300 | |
1301 def GetTabInfo(self, tab_index=0, windex=0): | |
1302 """Gets information about the specified tab. | |
1303 | |
1304 Args: | |
1305 tab_index: Integer index of the tab to activate; defaults to the first | |
1306 tab. | |
1307 windex: Integer index of the browser window to use; defaults to the first | |
1308 window. | |
1309 | |
1310 Returns: | |
1311 A dictionary containing information about the tab. | |
1312 Example: | |
1313 { u'title': "Hello World", | |
1314 u'url': "http://foo.bar", } | |
1315 | |
1316 Raises: | |
1317 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
1318 """ | |
1319 cmd_dict = { | |
1320 'command': 'GetTabInfo', | |
1321 'tab_index': tab_index, | |
1322 'windex': windex, | |
1323 } | |
1324 return self._GetResultFromJSONRequest(cmd_dict, windex=None) | |
1325 | |
1326 def GetActiveTabTitle(self, windex=0): | |
1327 """Gets the title of the active tab. | |
1328 | |
1329 Warning: Depending on the concept of an active tab is dangerous as it can | |
1330 change during the test. Use GetTabInfo and supply a tab_index explicitly. | |
1331 | |
1332 Args: | |
1333 windex: Integer index of the browser window to use; defaults to the first | |
1334 window. | |
1335 | |
1336 Returns: | |
1337 The tab title as a string. | |
1338 | |
1339 Raises: | |
1340 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
1341 """ | |
1342 return self.GetTabInfo(self.GetActiveTabIndex(windex), windex)['title'] | |
1343 | |
1344 def GetActiveTabURL(self, windex=0): | |
1345 """Gets the URL of the active tab. | |
1346 | |
1347 Warning: Depending on the concept of an active tab is dangerous as it can | |
1348 change during the test. Use GetTabInfo and supply a tab_index explicitly. | |
1349 | |
1350 Args: | |
1351 windex: Integer index of the browser window to use; defaults to the first | |
1352 window. | |
1353 | |
1354 Returns: | |
1355 The tab URL as a GURL object. | |
1356 | |
1357 Raises: | |
1358 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
1359 """ | |
1360 return GURL(self.GetTabInfo(self.GetActiveTabIndex(windex), windex)['url']) | |
1361 | |
1242 def GetBookmarkModel(self, windex=0): | 1362 def GetBookmarkModel(self, windex=0): |
1243 """Return the bookmark model as a BookmarkModel object. | 1363 """Return the bookmark model as a BookmarkModel object. |
1244 | 1364 |
1245 This is a snapshot of the bookmark model; it is not a proxy and | 1365 This is a snapshot of the bookmark model; it is not a proxy and |
1246 does not get updated as the bookmark model changes. | 1366 does not get updated as the bookmark model changes. |
1247 """ | 1367 """ |
1248 bookmarks_as_json = self._GetBookmarksAsJSON(windex) | 1368 bookmarks_as_json = self._GetBookmarksAsJSON(windex) |
1249 if not bookmarks_as_json: | 1369 if not bookmarks_as_json: |
1250 raise JSONInterfaceError('Could not resolve browser proxy.') | 1370 raise JSONInterfaceError('Could not resolve browser proxy.') |
1251 return bookmark_model.BookmarkModel(bookmarks_as_json) | 1371 return bookmark_model.BookmarkModel(bookmarks_as_json) |
(...skipping 4944 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6196 successful = result.wasSuccessful() | 6316 successful = result.wasSuccessful() |
6197 if not successful: | 6317 if not successful: |
6198 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) | 6318 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) |
6199 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ | 6319 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ |
6200 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) | 6320 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) |
6201 sys.exit(not successful) | 6321 sys.exit(not successful) |
6202 | 6322 |
6203 | 6323 |
6204 if __name__ == '__main__': | 6324 if __name__ == '__main__': |
6205 Main() | 6325 Main() |
OLD | NEW |