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 1414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1425 """Return the bookmark model as a BookmarkModel object. | 1425 """Return the bookmark model as a BookmarkModel object. |
1426 | 1426 |
1427 This is a snapshot of the bookmark model; it is not a proxy and | 1427 This is a snapshot of the bookmark model; it is not a proxy and |
1428 does not get updated as the bookmark model changes. | 1428 does not get updated as the bookmark model changes. |
1429 """ | 1429 """ |
1430 bookmarks_as_json = self._GetBookmarksAsJSON(windex) | 1430 bookmarks_as_json = self._GetBookmarksAsJSON(windex) |
1431 if not bookmarks_as_json: | 1431 if not bookmarks_as_json: |
1432 raise JSONInterfaceError('Could not resolve browser proxy.') | 1432 raise JSONInterfaceError('Could not resolve browser proxy.') |
1433 return bookmark_model.BookmarkModel(bookmarks_as_json) | 1433 return bookmark_model.BookmarkModel(bookmarks_as_json) |
1434 | 1434 |
| 1435 def _GetBookmarksAsJSON(self, windex=0): |
| 1436 """Get bookmarks as a JSON dictionary; used by GetBookmarkModel().""" |
| 1437 cmd_dict = { |
| 1438 'command': 'GetBookmarksAsJSON', |
| 1439 'windex': windex, |
| 1440 } |
| 1441 self.WaitForBookmarkModelToLoad(windex) |
| 1442 return self._GetResultFromJSONRequest(cmd_dict, |
| 1443 windex=None)['bookmarks_as_json'] |
| 1444 |
| 1445 def WaitForBookmarkModelToLoad(self, windex=0): |
| 1446 """Gets the status of the bookmark bar as a dictionary. |
| 1447 |
| 1448 Args: |
| 1449 windex: Integer index of the browser window to use; defaults to the first |
| 1450 window. |
| 1451 |
| 1452 Raises: |
| 1453 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1454 """ |
| 1455 cmd_dict = { |
| 1456 'command': 'WaitForBookmarkModelToLoad', |
| 1457 'windex': windex, |
| 1458 } |
| 1459 return self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1460 |
| 1461 def GetBookmarkBarStatus(self, windex=0): |
| 1462 """Gets the status of the bookmark bar as a dictionary. |
| 1463 |
| 1464 Args: |
| 1465 windex: Integer index of the browser window to use; defaults to the first |
| 1466 window. |
| 1467 |
| 1468 Returns: |
| 1469 A dictionary. |
| 1470 Example: |
| 1471 { u'visible': True, |
| 1472 u'animating': False, |
| 1473 u'detached': False, } |
| 1474 |
| 1475 Raises: |
| 1476 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1477 """ |
| 1478 cmd_dict = { |
| 1479 'command': 'GetBookmarkBarStatus', |
| 1480 'windex': windex, |
| 1481 } |
| 1482 return self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1483 |
| 1484 def GetBookmarkBarStatus(self, windex=0): |
| 1485 """Gets the status of the bookmark bar as a dictionary. |
| 1486 |
| 1487 Args: |
| 1488 windex: Integer index of the browser window to use; defaults to the first |
| 1489 window. |
| 1490 |
| 1491 Returns: |
| 1492 A dictionary. |
| 1493 Example: |
| 1494 { u'visible': True, |
| 1495 u'animating': False, |
| 1496 u'detached': False, } |
| 1497 |
| 1498 Raises: |
| 1499 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1500 """ |
| 1501 cmd_dict = { |
| 1502 'command': 'GetBookmarkBarStatus', |
| 1503 'windex': windex, |
| 1504 } |
| 1505 return self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1506 |
| 1507 def GetBookmarkBarStatus(self, windex=0): |
| 1508 """Gets the status of the bookmark bar as a dictionary. |
| 1509 |
| 1510 Args: |
| 1511 windex: Integer index of the browser window to use; defaults to the first |
| 1512 window. |
| 1513 |
| 1514 Returns: |
| 1515 A dictionary. |
| 1516 Example: |
| 1517 { u'visible': True, |
| 1518 u'animating': False, |
| 1519 u'detached': False, } |
| 1520 |
| 1521 Raises: |
| 1522 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1523 """ |
| 1524 cmd_dict = { |
| 1525 'command': 'GetBookmarkBarStatus', |
| 1526 'windex': windex, |
| 1527 } |
| 1528 return self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1529 |
| 1530 def GetBookmarkBarVisibility(self, windex=0): |
| 1531 """Returns the visibility of the bookmark bar. |
| 1532 |
| 1533 Args: |
| 1534 windex: Integer index of the browser window to use; defaults to the first |
| 1535 window. |
| 1536 |
| 1537 Returns: |
| 1538 True if the bookmark bar is visible, false otherwise. |
| 1539 |
| 1540 Raises: |
| 1541 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1542 """ |
| 1543 return self.GetBookmarkBarStatus(windex)['visible'] |
| 1544 |
| 1545 def IsBookmarkBarDetached(self, windex=0): |
| 1546 """Returns whether the bookmark bar is detached. |
| 1547 |
| 1548 Args: |
| 1549 windex: Integer index of the browser window to use; defaults to the first |
| 1550 window. |
| 1551 |
| 1552 Returns: |
| 1553 True if the bookmark bar is detached, false otherwise. |
| 1554 |
| 1555 Raises: |
| 1556 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1557 """ |
| 1558 return self.GetBookmarkBarStatus(windex)['detached'] |
| 1559 |
| 1560 def WaitForBookmarkBarVisibilityChange(self, wait_for_open, windex=0): |
| 1561 """Waits until the bookmark bar is either visible or not visible. |
| 1562 |
| 1563 Args: |
| 1564 wait_for_open: If True, wait until bookmark bar is visible; otherwise wait |
| 1565 until bookmark bar is not visible. |
| 1566 windex: Integer index of the browser window to use; defaults to the first |
| 1567 window. |
| 1568 |
| 1569 Raises: |
| 1570 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1571 """ |
| 1572 def IsChanged(wait_for_open, windex): |
| 1573 status = self.GetBookmarkBarStatus(windex) |
| 1574 return status['visible'] == wait_for_open and not status['animating'] |
| 1575 return self.WaitUntil(lambda: IsChanged(wait_for_open, windex)) |
| 1576 |
| 1577 def AddBookmarkGroup(self, parent_id, index, title, windex=0): |
| 1578 """Adds a bookmark folder. |
| 1579 |
| 1580 Args: |
| 1581 parent_id: The parent bookmark folder. |
| 1582 index: The location in the parent's list to insert this bookmark folder. |
| 1583 title: The name of the bookmark folder. |
| 1584 windex: Integer index of the browser window to use; defaults to the first |
| 1585 window. |
| 1586 |
| 1587 Returns: |
| 1588 True if the bookmark bar is detached, false otherwise. |
| 1589 |
| 1590 Raises: |
| 1591 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1592 """ |
| 1593 if isinstance(parent_id, basestring): |
| 1594 parent_id = int(parent_id) |
| 1595 cmd_dict = { |
| 1596 'command': 'AddBookmark', |
| 1597 'parent_id': parent_id, |
| 1598 'index': index, |
| 1599 'title': title, |
| 1600 'is_folder': True, |
| 1601 'windex': windex, |
| 1602 } |
| 1603 self.WaitForBookmarkModelToLoad(windex) |
| 1604 self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1605 |
| 1606 def AddBookmarkURL(self, parent_id, index, title, url, windex=0): |
| 1607 """Add a bookmark URL. |
| 1608 |
| 1609 Args: |
| 1610 parent_id: The parent bookmark folder. |
| 1611 index: The location in the parent's list to insert this bookmark. |
| 1612 title: The name of the bookmark. |
| 1613 url: The url of the bookmark. |
| 1614 windex: Integer index of the browser window to use; defaults to the first |
| 1615 window. |
| 1616 |
| 1617 Raises: |
| 1618 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1619 """ |
| 1620 if isinstance(parent_id, basestring): |
| 1621 parent_id = int(parent_id) |
| 1622 cmd_dict = { |
| 1623 'command': 'AddBookmark', |
| 1624 'parent_id': parent_id, |
| 1625 'index': index, |
| 1626 'title': title, |
| 1627 'url': url, |
| 1628 'is_folder': False, |
| 1629 'windex': windex, |
| 1630 } |
| 1631 self.WaitForBookmarkModelToLoad(windex) |
| 1632 self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1633 |
| 1634 def ReparentBookmark(self, id, new_parent_id, index, windex=0): |
| 1635 """Move a bookmark. |
| 1636 |
| 1637 Args: |
| 1638 id: The bookmark to move. |
| 1639 new_parent_id: The new parent bookmark folder. |
| 1640 index: The location in the parent's list to insert this bookmark. |
| 1641 windex: Integer index of the browser window to use; defaults to the first |
| 1642 window. |
| 1643 |
| 1644 Raises: |
| 1645 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1646 """ |
| 1647 if isinstance(id, basestring): |
| 1648 id = int(id) |
| 1649 if isinstance(new_parent_id, basestring): |
| 1650 new_parent_id = int(new_parent_id) |
| 1651 cmd_dict = { |
| 1652 'command': 'ReparentBookmark', |
| 1653 'id': id, |
| 1654 'new_parent_id': new_parent_id, |
| 1655 'index': index, |
| 1656 'windex': windex, |
| 1657 } |
| 1658 self.WaitForBookmarkModelToLoad(windex) |
| 1659 self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1660 |
| 1661 def SetBookmarkTitle(self, id, title, windex=0): |
| 1662 """Change the title of a bookmark. |
| 1663 |
| 1664 Args: |
| 1665 id: The bookmark to rename. |
| 1666 title: The new title for the bookmark. |
| 1667 windex: Integer index of the browser window to use; defaults to the first |
| 1668 window. |
| 1669 |
| 1670 Raises: |
| 1671 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1672 """ |
| 1673 if isinstance(id, basestring): |
| 1674 id = int(id) |
| 1675 cmd_dict = { |
| 1676 'command': 'SetBookmarkTitle', |
| 1677 'id': id, |
| 1678 'title': title, |
| 1679 'windex': windex, |
| 1680 } |
| 1681 self.WaitForBookmarkModelToLoad(windex) |
| 1682 self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1683 |
| 1684 def SetBookmarkURL(self, id, url, windex=0): |
| 1685 """Change the URL of a bookmark. |
| 1686 |
| 1687 Args: |
| 1688 id: The bookmark to change. |
| 1689 url: The new url for the bookmark. |
| 1690 windex: Integer index of the browser window to use; defaults to the first |
| 1691 window. |
| 1692 |
| 1693 Raises: |
| 1694 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1695 """ |
| 1696 if isinstance(id, basestring): |
| 1697 id = int(id) |
| 1698 cmd_dict = { |
| 1699 'command': 'SetBookmarkURL', |
| 1700 'id': id, |
| 1701 'url': url, |
| 1702 'windex': windex, |
| 1703 } |
| 1704 self.WaitForBookmarkModelToLoad(windex) |
| 1705 self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1706 |
| 1707 def RemoveBookmark(self, id, windex=0): |
| 1708 """Remove a bookmark. |
| 1709 |
| 1710 Args: |
| 1711 id: The bookmark to remove. |
| 1712 windex: Integer index of the browser window to use; defaults to the first |
| 1713 window. |
| 1714 |
| 1715 Raises: |
| 1716 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
| 1717 """ |
| 1718 if isinstance(id, basestring): |
| 1719 id = int(id) |
| 1720 cmd_dict = { |
| 1721 'command': 'RemoveBookmark', |
| 1722 'id': id, |
| 1723 'windex': windex, |
| 1724 } |
| 1725 self.WaitForBookmarkModelToLoad(windex) |
| 1726 self._GetResultFromJSONRequest(cmd_dict, windex=None) |
| 1727 |
1435 def GetDownloadsInfo(self, windex=0): | 1728 def GetDownloadsInfo(self, windex=0): |
1436 """Return info about downloads. | 1729 """Return info about downloads. |
1437 | 1730 |
1438 This includes all the downloads recognized by the history system. | 1731 This includes all the downloads recognized by the history system. |
1439 | 1732 |
1440 Returns: | 1733 Returns: |
1441 an instance of downloads_info.DownloadInfo | 1734 an instance of downloads_info.DownloadInfo |
1442 """ | 1735 """ |
1443 return download_info.DownloadInfo( | 1736 return download_info.DownloadInfo( |
1444 self._SendJSONRequest( | 1737 self._SendJSONRequest( |
(...skipping 5100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6545 successful = result.wasSuccessful() | 6838 successful = result.wasSuccessful() |
6546 if not successful: | 6839 if not successful: |
6547 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) | 6840 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) |
6548 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ | 6841 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ |
6549 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) | 6842 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) |
6550 sys.exit(not successful) | 6843 sys.exit(not successful) |
6551 | 6844 |
6552 | 6845 |
6553 if __name__ == '__main__': | 6846 if __name__ == '__main__': |
6554 Main() | 6847 Main() |
OLD | NEW |