OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """PyAuto: Python Interface to Chromium's Automation Proxy. | 7 """PyAuto: Python Interface to Chromium's Automation Proxy. |
8 | 8 |
9 PyAuto uses swig to expose Automation Proxy interfaces to Python. | 9 PyAuto uses swig to expose Automation Proxy interfaces to Python. |
10 For complete documentation on the functionality available, | 10 For complete documentation on the functionality available, |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 This script can be used as an executable to fire off other scripts, similar | 23 This script can be used as an executable to fire off other scripts, similar |
24 to unittest.py | 24 to unittest.py |
25 python pyauto.py test_script | 25 python pyauto.py test_script |
26 """ | 26 """ |
27 | 27 |
28 import logging | 28 import logging |
29 import optparse | 29 import optparse |
30 import os | 30 import os |
31 import re | 31 import re |
| 32 import shutil |
32 import sys | 33 import sys |
| 34 import tempfile |
33 import time | 35 import time |
34 import types | 36 import types |
35 import unittest | 37 import unittest |
36 import urllib | 38 import urllib |
37 | 39 |
38 | 40 |
39 def _LocateBinDirs(): | 41 def _LocateBinDirs(): |
40 """Setup a few dirs where we expect to find dependency libraries.""" | 42 """Setup a few dirs where we expect to find dependency libraries.""" |
41 script_dir = os.path.dirname(__file__) | 43 script_dir = os.path.dirname(__file__) |
42 chrome_src = os.path.join(script_dir, os.pardir, os.pardir, os.pardir) | 44 chrome_src = os.path.join(script_dir, os.pardir, os.pardir, os.pardir) |
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 pyauto_errors.JSONInterfaceError if the automation call returns an error. | 612 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
611 """ | 613 """ |
612 cmd_dict = { | 614 cmd_dict = { |
613 'command': 'DisablePlugin', | 615 'command': 'DisablePlugin', |
614 'path': path, | 616 'path': path, |
615 } | 617 } |
616 ret_dict = json.loads(self._SendJSONRequest(0, json.dumps(cmd_dict))) | 618 ret_dict = json.loads(self._SendJSONRequest(0, json.dumps(cmd_dict))) |
617 if ret_dict.has_key('error'): | 619 if ret_dict.has_key('error'): |
618 raise JSONInterfaceError(ret_dict['error']) | 620 raise JSONInterfaceError(ret_dict['error']) |
619 | 621 |
| 622 def GetTabContents(self, tab_index=0, window_index=0): |
| 623 """Get the html contents of a tab (a la "view source"). |
| 624 |
| 625 As an implementation detail, this saves the html in a file, reads |
| 626 the file into a buffer, then deletes it. |
| 627 |
| 628 Args: |
| 629 tab_index: tab index, defaults to 0. |
| 630 window_index: window index, defaults to 0. |
| 631 Returns: |
| 632 html content of a page as a string. |
| 633 """ |
| 634 tempdir = tempfile.mkdtemp() |
| 635 filename = os.path.join(tempdir, 'content.html') |
| 636 cmd_dict = { # Prepare command for the json interface |
| 637 'command': 'SaveTabContents', |
| 638 'tab_index': tab_index, |
| 639 'filename': filename |
| 640 } |
| 641 ret_dict = json.loads(self._SendJSONRequest(window_index, |
| 642 json.dumps(cmd_dict))) |
| 643 if ret_dict.has_key('error'): |
| 644 raise JSONInterfaceError(ret_dict['error']) |
| 645 try: |
| 646 f = open(filename) |
| 647 all_data = f.read() |
| 648 f.close() |
| 649 return all_data |
| 650 except IOError: |
| 651 raise |
| 652 finally: |
| 653 shutil.rmtree(tempdir) |
| 654 |
620 | 655 |
621 class PyUITestSuite(pyautolib.PyUITestSuiteBase, unittest.TestSuite): | 656 class PyUITestSuite(pyautolib.PyUITestSuiteBase, unittest.TestSuite): |
622 """Base TestSuite for PyAuto UI tests.""" | 657 """Base TestSuite for PyAuto UI tests.""" |
623 | 658 |
624 def __init__(self, args): | 659 def __init__(self, args): |
625 pyautolib.PyUITestSuiteBase.__init__(self, args) | 660 pyautolib.PyUITestSuiteBase.__init__(self, args) |
626 | 661 |
627 # Figure out path to chromium binaries | 662 # Figure out path to chromium binaries |
628 browser_dir = os.path.normpath(os.path.dirname(pyautolib.__file__)) | 663 browser_dir = os.path.normpath(os.path.dirname(pyautolib.__file__)) |
629 logging.debug('Loading pyauto libs from %s', browser_dir) | 664 logging.debug('Loading pyauto libs from %s', browser_dir) |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
843 if self._options.verbose: | 878 if self._options.verbose: |
844 verbosity = 2 | 879 verbosity = 2 |
845 result = unittest.TextTestRunner(verbosity=verbosity).run(pyauto_suite) | 880 result = unittest.TextTestRunner(verbosity=verbosity).run(pyauto_suite) |
846 del loaded_tests # Need to destroy test cases before the suite | 881 del loaded_tests # Need to destroy test cases before the suite |
847 del pyauto_suite | 882 del pyauto_suite |
848 sys.exit(not result.wasSuccessful()) | 883 sys.exit(not result.wasSuccessful()) |
849 | 884 |
850 | 885 |
851 if __name__ == '__main__': | 886 if __name__ == '__main__': |
852 Main() | 887 Main() |
OLD | NEW |