Chromium Code Reviews| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 import string | 42 import string |
| 43 import subprocess | 43 import subprocess |
| 44 import sys | 44 import sys |
| 45 import tempfile | 45 import tempfile |
| 46 import time | 46 import time |
| 47 import types | 47 import types |
| 48 import unittest | 48 import unittest |
| 49 import urllib | 49 import urllib |
| 50 | 50 |
| 51 import pyauto_paths | 51 import pyauto_paths |
| 52 from pyauto_utils import GTestTextTestRunner | |
| 52 | 53 |
| 53 | 54 |
| 54 def _LocateBinDirs(): | 55 def _LocateBinDirs(): |
| 55 """Setup a few dirs where we expect to find dependency libraries.""" | 56 """Setup a few dirs where we expect to find dependency libraries.""" |
| 56 deps_dirs = [ | 57 deps_dirs = [ |
| 57 os.path.dirname(__file__), | 58 os.path.dirname(__file__), |
| 58 pyauto_paths.GetThirdPartyDir(), | 59 pyauto_paths.GetThirdPartyDir(), |
| 59 os.path.join(pyauto_paths.GetThirdPartyDir(), 'webdriver', 'pylib'), | 60 os.path.join(pyauto_paths.GetThirdPartyDir(), 'webdriver', 'pylib'), |
| 60 ] | 61 ] |
| 61 sys.path += map(os.path.normpath, pyauto_paths.GetBuildDirs() + deps_dirs) | 62 sys.path += map(os.path.normpath, pyauto_paths.GetBuildDirs() + deps_dirs) |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 | 111 |
| 111 Example: | 112 Example: |
| 112 | 113 |
| 113 class MyTest(PyUITest): | 114 class MyTest(PyUITest): |
| 114 | 115 |
| 115 def testNavigation(self): | 116 def testNavigation(self): |
| 116 self.NavigateToURL("http://www.google.com") | 117 self.NavigateToURL("http://www.google.com") |
| 117 self.assertEqual("Google", self.GetActiveTabTitle()) | 118 self.assertEqual("Google", self.GetActiveTabTitle()) |
| 118 """ | 119 """ |
| 119 | 120 |
| 120 def __init__(self, methodName='runTest', **kwargs): | 121 def __init__(self, methodName='runTest', **kwargs): |
|
Nirnimesh
2012/08/09 21:32:48
why not add browser_path arg here?
nkang
2012/08/16 23:46:24
I included it in kwargs at Ken's recommendation. A
| |
| 121 """Initialize PyUITest. | 122 """Initialize PyUITest. |
| 122 | 123 |
| 123 When redefining __init__ in a derived class, make sure that: | 124 When redefining __init__ in a derived class, make sure that: |
| 124 o you make a call this __init__ | 125 o you make a call this __init__ |
| 125 o __init__ takes methodName as an arg. this is mandated by unittest module | 126 o __init__ takes methodName as an arg. this is mandated by unittest module |
| 126 | 127 |
| 127 Args: | 128 Args: |
| 128 methodName: the default method name. Internal use by unittest module | 129 methodName: the default method name. Internal use by unittest module |
| 129 | 130 |
| 130 (The rest of the args can be in any order. They can even be skipped in | 131 (The rest of the args can be in any order. They can even be skipped in |
| 131 which case the defaults will be used.) | 132 which case the defaults will be used.) |
| 132 | 133 |
| 133 clear_profile: If True, clean the profile dir before use. Defaults to True | 134 clear_profile: If True, clean the profile dir before use. Defaults to True |
| 134 homepage: the home page. Defaults to "about:blank" | 135 homepage: the home page. Defaults to "about:blank" |
| 135 """ | 136 """ |
| 136 # Fetch provided keyword args, or fill in defaults. | 137 # Fetch provided keyword args, or fill in defaults. |
| 138 browser_path = kwargs.get('browser_path', None) | |
|
Nirnimesh
2012/08/09 21:32:48
So BrowserPath() method will be out of sync?
nkang
2012/08/16 23:46:24
BrowserPath returns the same path as the pyautolib
Nirnimesh
2012/08/22 07:06:34
All this is so confusing. You'll not remember it 1
| |
| 137 clear_profile = kwargs.get('clear_profile', True) | 139 clear_profile = kwargs.get('clear_profile', True) |
| 138 homepage = kwargs.get('homepage', 'about:blank') | 140 homepage = kwargs.get('homepage', 'about:blank') |
| 139 | 141 |
| 140 pyautolib.PyUITestBase.__init__(self, clear_profile, homepage) | 142 pyautolib.PyUITestBase.__init__(self, clear_profile, homepage) |
| 141 self.Initialize(pyautolib.FilePath(self.BrowserPath())) | 143 if browser_path: |
| 144 self.Initialize(pyautolib.FilePath(browser_path)) | |
| 145 else: | |
| 146 self.Initialize(pyautolib.FilePath(self.BrowserPath())) | |
| 142 unittest.TestCase.__init__(self, methodName) | 147 unittest.TestCase.__init__(self, methodName) |
| 143 | 148 |
| 144 # Give all pyauto tests easy access to pprint.PrettyPrinter functions. | 149 # Give all pyauto tests easy access to pprint.PrettyPrinter functions. |
| 145 self.pprint = pprint.pprint | 150 self.pprint = pprint.pprint |
| 146 self.pformat = pprint.pformat | 151 self.pformat = pprint.pformat |
| 147 | 152 |
| 148 # Set up remote proxies, if they were requested. | 153 # Set up remote proxies, if they were requested. |
| 149 self.remotes = [] | 154 self.remotes = [] |
| 150 self.remote = None | 155 self.remote = None |
| 151 global _REMOTE_PROXY | 156 global _REMOTE_PROXY |
| (...skipping 5605 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 5757 if env.get('PYTHONPATH', None): | 5762 if env.get('PYTHONPATH', None): |
| 5758 env['PYTHONPATH'] += ':' + main_path | 5763 env['PYTHONPATH'] += ':' + main_path |
| 5759 else: | 5764 else: |
| 5760 env['PYTHONPATH'] = main_path | 5765 env['PYTHONPATH'] = main_path |
| 5761 | 5766 |
| 5762 # Run it! | 5767 # Run it! |
| 5763 subprocess.Popen([sys.executable, os.path.join(os.path.dirname(__file__), | 5768 subprocess.Popen([sys.executable, os.path.join(os.path.dirname(__file__), |
| 5764 'remote_host.py')], env=env) | 5769 'remote_host.py')], env=env) |
| 5765 | 5770 |
| 5766 | 5771 |
| 5767 class _GTestTextTestResult(unittest._TextTestResult): | |
| 5768 """A test result class that can print formatted text results to a stream. | |
| 5769 | |
| 5770 Results printed in conformance with gtest output format, like: | |
| 5771 [ RUN ] autofill.AutofillTest.testAutofillInvalid: "test desc." | |
| 5772 [ OK ] autofill.AutofillTest.testAutofillInvalid | |
| 5773 [ RUN ] autofill.AutofillTest.testFillProfile: "test desc." | |
| 5774 [ OK ] autofill.AutofillTest.testFillProfile | |
| 5775 [ RUN ] autofill.AutofillTest.testFillProfileCrazyCharacters: "Test." | |
| 5776 [ OK ] autofill.AutofillTest.testFillProfileCrazyCharacters | |
| 5777 """ | |
| 5778 def __init__(self, stream, descriptions, verbosity): | |
| 5779 unittest._TextTestResult.__init__(self, stream, descriptions, verbosity) | |
| 5780 | |
| 5781 def _GetTestURI(self, test): | |
| 5782 if sys.version_info[:2] <= (2, 4): | |
| 5783 return '%s.%s' % (unittest._strclass(test.__class__), | |
| 5784 test._TestCase__testMethodName) | |
| 5785 return '%s.%s.%s' % (test.__class__.__module__, | |
| 5786 test.__class__.__name__, | |
| 5787 test._testMethodName) | |
| 5788 | |
| 5789 def getDescription(self, test): | |
| 5790 return '%s: "%s"' % (self._GetTestURI(test), test.shortDescription()) | |
| 5791 | |
| 5792 def startTest(self, test): | |
| 5793 unittest.TestResult.startTest(self, test) | |
| 5794 self.stream.writeln('[ RUN ] %s' % self.getDescription(test)) | |
| 5795 | |
| 5796 def addSuccess(self, test): | |
| 5797 unittest.TestResult.addSuccess(self, test) | |
| 5798 self.stream.writeln('[ OK ] %s' % self._GetTestURI(test)) | |
| 5799 | |
| 5800 def addError(self, test, err): | |
| 5801 unittest.TestResult.addError(self, test, err) | |
| 5802 self.stream.writeln('[ ERROR ] %s' % self._GetTestURI(test)) | |
| 5803 | |
| 5804 def addFailure(self, test, err): | |
| 5805 unittest.TestResult.addFailure(self, test, err) | |
| 5806 self.stream.writeln('[ FAILED ] %s' % self._GetTestURI(test)) | |
| 5807 | |
| 5808 | |
| 5809 class PyAutoTextTestRunner(unittest.TextTestRunner): | |
| 5810 """Test Runner for PyAuto tests that displays results in textual format. | |
| 5811 | |
| 5812 Results are displayed in conformance with gtest output. | |
| 5813 """ | |
| 5814 def __init__(self, verbosity=1): | |
| 5815 unittest.TextTestRunner.__init__(self, | |
| 5816 stream=sys.stderr, | |
| 5817 verbosity=verbosity) | |
| 5818 | |
| 5819 def _makeResult(self): | |
| 5820 return _GTestTextTestResult(self.stream, self.descriptions, self.verbosity) | |
| 5821 | |
| 5822 | |
| 5823 # Implementation inspired from unittest.main() | 5772 # Implementation inspired from unittest.main() |
| 5824 class Main(object): | 5773 class Main(object): |
| 5825 """Main program for running PyAuto tests.""" | 5774 """Main program for running PyAuto tests.""" |
| 5826 | 5775 |
| 5827 _options, _args = None, None | 5776 _options, _args = None, None |
| 5828 _tests_filename = 'PYAUTO_TESTS' | 5777 _tests_filename = 'PYAUTO_TESTS' |
| 5829 _platform_map = { | 5778 _platform_map = { |
| 5830 'win32': 'win', | 5779 'win32': 'win', |
| 5831 'darwin': 'mac', | 5780 'darwin': 'mac', |
| 5832 'linux2': 'linux', | 5781 'linux2': 'linux', |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 6116 if self._options.list_tests: # List tests and exit | 6065 if self._options.list_tests: # List tests and exit |
| 6117 for name in test_names: | 6066 for name in test_names: |
| 6118 print name | 6067 print name |
| 6119 sys.exit(0) | 6068 sys.exit(0) |
| 6120 pyauto_suite = PyUITestSuite(suite_args) | 6069 pyauto_suite = PyUITestSuite(suite_args) |
| 6121 loaded_tests = unittest.defaultTestLoader.loadTestsFromNames(test_names) | 6070 loaded_tests = unittest.defaultTestLoader.loadTestsFromNames(test_names) |
| 6122 pyauto_suite.addTests(loaded_tests) | 6071 pyauto_suite.addTests(loaded_tests) |
| 6123 verbosity = 1 | 6072 verbosity = 1 |
| 6124 if self._options.verbose: | 6073 if self._options.verbose: |
| 6125 verbosity = 2 | 6074 verbosity = 2 |
| 6126 result = PyAutoTextTestRunner(verbosity=verbosity).run(pyauto_suite) | 6075 result = GTestTextTestRunner(verbosity=verbosity).run(pyauto_suite) |
| 6127 del loaded_tests # Need to destroy test cases before the suite | 6076 del loaded_tests # Need to destroy test cases before the suite |
| 6128 del pyauto_suite | 6077 del pyauto_suite |
| 6129 successful = result.wasSuccessful() | 6078 successful = result.wasSuccessful() |
| 6130 if not successful: | 6079 if not successful: |
| 6131 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) | 6080 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) |
| 6132 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ | 6081 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ |
| 6133 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) | 6082 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) |
| 6134 sys.exit(not successful) | 6083 sys.exit(not successful) |
| 6135 | 6084 |
| 6136 | 6085 |
| 6137 if __name__ == '__main__': | 6086 if __name__ == '__main__': |
| 6138 Main() | 6087 Main() |
| OLD | NEW |