Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(801)

Side by Side Diff: pyautolib/pyauto.py

Issue 10384104: Chrome updater test framework (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/chrome/test/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 Args: 129 Args:
129 methodName: the default method name. Internal use by unittest module 130 methodName: the default method name. Internal use by unittest module
130 131
131 (The rest of the args can be in any order. They can even be skipped in 132 (The rest of the args can be in any order. They can even be skipped in
132 which case the defaults will be used.) 133 which case the defaults will be used.)
133 134
134 clear_profile: If True, clean the profile dir before use. Defaults to True 135 clear_profile: If True, clean the profile dir before use. Defaults to True
135 homepage: the home page. Defaults to "about:blank" 136 homepage: the home page. Defaults to "about:blank"
136 """ 137 """
137 # Fetch provided keyword args, or fill in defaults. 138 # Fetch provided keyword args, or fill in defaults.
139 browser_path = kwargs.get('browser_path', None)
138 clear_profile = kwargs.get('clear_profile', True) 140 clear_profile = kwargs.get('clear_profile', True)
139 homepage = kwargs.get('homepage', 'about:blank') 141 homepage = kwargs.get('homepage', 'about:blank')
140 self._automation_timeout = _DEFAULT_AUTOMATION_TIMEOUT * 1000 142 self._automation_timeout = _DEFAULT_AUTOMATION_TIMEOUT * 1000
141 143
142 pyautolib.PyUITestBase.__init__(self, clear_profile, homepage) 144 pyautolib.PyUITestBase.__init__(self, clear_profile, homepage)
143 self.Initialize(pyautolib.FilePath(self.BrowserPath())) 145 if browser_path:
146 self.Initialize(pyautolib.FilePath(browser_path))
147 else:
148 self.Initialize(pyautolib.FilePath(PyUITest.BrowserPath()))
144 unittest.TestCase.__init__(self, methodName) 149 unittest.TestCase.__init__(self, methodName)
145 150
146 # Give all pyauto tests easy access to pprint.PrettyPrinter functions. 151 # Give all pyauto tests easy access to pprint.PrettyPrinter functions.
147 self.pprint = pprint.pprint 152 self.pprint = pprint.pprint
148 self.pformat = pprint.pformat 153 self.pformat = pprint.pformat
149 154
150 # Set up remote proxies, if they were requested. 155 # Set up remote proxies, if they were requested.
151 self.remotes = [] 156 self.remotes = []
152 self.remote = None 157 self.remote = None
153 global _REMOTE_PROXY 158 global _REMOTE_PROXY
(...skipping 6177 matching lines...) Expand 10 before | Expand all | Expand 10 after
6331 if env.get('PYTHONPATH', None): 6336 if env.get('PYTHONPATH', None):
6332 env['PYTHONPATH'] += ':' + main_path 6337 env['PYTHONPATH'] += ':' + main_path
6333 else: 6338 else:
6334 env['PYTHONPATH'] = main_path 6339 env['PYTHONPATH'] = main_path
6335 6340
6336 # Run it! 6341 # Run it!
6337 subprocess.Popen([sys.executable, os.path.join(os.path.dirname(__file__), 6342 subprocess.Popen([sys.executable, os.path.join(os.path.dirname(__file__),
6338 'remote_host.py')], env=env) 6343 'remote_host.py')], env=env)
6339 6344
6340 6345
6341 class _GTestTextTestResult(unittest._TextTestResult):
6342 """A test result class that can print formatted text results to a stream.
6343
6344 Results printed in conformance with gtest output format, like:
6345 [ RUN ] autofill.AutofillTest.testAutofillInvalid: "test desc."
6346 [ OK ] autofill.AutofillTest.testAutofillInvalid
6347 [ RUN ] autofill.AutofillTest.testFillProfile: "test desc."
6348 [ OK ] autofill.AutofillTest.testFillProfile
6349 [ RUN ] autofill.AutofillTest.testFillProfileCrazyCharacters: "Test."
6350 [ OK ] autofill.AutofillTest.testFillProfileCrazyCharacters
6351 """
6352 def __init__(self, stream, descriptions, verbosity):
6353 unittest._TextTestResult.__init__(self, stream, descriptions, verbosity)
6354
6355 def _GetTestURI(self, test):
6356 if sys.version_info[:2] <= (2, 4):
6357 return '%s.%s' % (unittest._strclass(test.__class__),
6358 test._TestCase__testMethodName)
6359 return '%s.%s.%s' % (test.__class__.__module__,
6360 test.__class__.__name__,
6361 test._testMethodName)
6362
6363 def getDescription(self, test):
6364 return '%s: "%s"' % (self._GetTestURI(test), test.shortDescription())
6365
6366 def startTest(self, test):
6367 unittest.TestResult.startTest(self, test)
6368 self.stream.writeln('[ RUN ] %s' % self.getDescription(test))
6369
6370 def addSuccess(self, test):
6371 unittest.TestResult.addSuccess(self, test)
6372 self.stream.writeln('[ OK ] %s' % self._GetTestURI(test))
6373
6374 def addError(self, test, err):
6375 unittest.TestResult.addError(self, test, err)
6376 self.stream.writeln('[ ERROR ] %s' % self._GetTestURI(test))
6377
6378 def addFailure(self, test, err):
6379 unittest.TestResult.addFailure(self, test, err)
6380 self.stream.writeln('[ FAILED ] %s' % self._GetTestURI(test))
6381
6382
6383 class PyAutoTextTestRunner(unittest.TextTestRunner):
6384 """Test Runner for PyAuto tests that displays results in textual format.
6385
6386 Results are displayed in conformance with gtest output.
6387 """
6388 def __init__(self, verbosity=1):
6389 unittest.TextTestRunner.__init__(self,
6390 stream=sys.stderr,
6391 verbosity=verbosity)
6392
6393 def _makeResult(self):
6394 return _GTestTextTestResult(self.stream, self.descriptions, self.verbosity)
6395
6396
6397 # Implementation inspired from unittest.main() 6346 # Implementation inspired from unittest.main()
6398 class Main(object): 6347 class Main(object):
6399 """Main program for running PyAuto tests.""" 6348 """Main program for running PyAuto tests."""
6400 6349
6401 _options, _args = None, None 6350 _options, _args = None, None
6402 _tests_filename = 'PYAUTO_TESTS' 6351 _tests_filename = 'PYAUTO_TESTS'
6403 _platform_map = { 6352 _platform_map = {
6404 'win32': 'win', 6353 'win32': 'win',
6405 'darwin': 'mac', 6354 'darwin': 'mac',
6406 'linux2': 'linux', 6355 'linux2': 'linux',
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
6683 if self._options.list_tests: # List tests and exit 6632 if self._options.list_tests: # List tests and exit
6684 for name in test_names: 6633 for name in test_names:
6685 print name 6634 print name
6686 sys.exit(0) 6635 sys.exit(0)
6687 pyauto_suite = PyUITestSuite(suite_args) 6636 pyauto_suite = PyUITestSuite(suite_args)
6688 loaded_tests = unittest.defaultTestLoader.loadTestsFromNames(test_names) 6637 loaded_tests = unittest.defaultTestLoader.loadTestsFromNames(test_names)
6689 pyauto_suite.addTests(loaded_tests) 6638 pyauto_suite.addTests(loaded_tests)
6690 verbosity = 1 6639 verbosity = 1
6691 if self._options.verbose: 6640 if self._options.verbose:
6692 verbosity = 2 6641 verbosity = 2
6693 result = PyAutoTextTestRunner(verbosity=verbosity).run(pyauto_suite) 6642 result = GTestTextTestRunner(verbosity=verbosity).run(pyauto_suite)
6694 del loaded_tests # Need to destroy test cases before the suite 6643 del loaded_tests # Need to destroy test cases before the suite
6695 del pyauto_suite 6644 del pyauto_suite
6696 successful = result.wasSuccessful() 6645 successful = result.wasSuccessful()
6697 if not successful: 6646 if not successful:
6698 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) 6647 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename)
6699 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ 6648 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \
6700 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) 6649 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL)
6701 sys.exit(not successful) 6650 sys.exit(not successful)
6702 6651
6703 6652
6704 if __name__ == '__main__': 6653 if __name__ == '__main__':
6705 Main() 6654 Main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698