1 #!/usr/bin/env python 2 # Copyright 2013 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 """Runs all ChromeDriver end to end tests.""" 7 8 import optparse 9 import os 10 import platform 11 import sys 12 13 _THIS_DIR = os.path.abspath(os.path.dirname(__file__)) 14 sys.path.insert(0, os.path.join(_THIS_DIR, os.pardir)) 15 16 import archive 17 import chrome_paths 18 import util 19 20 21 def _AppendEnvironmentPath(env_name, path): 22 if env_name in os.environ: 23 lib_path = os.environ[env_name] 24 if path not in lib_path: 25 os.environ[env_name] += os.pathsep + path 26 else: 27 os.environ[env_name] = path 28 29 30 def _AddToolsToSystemPathForWindows(): 31 path_cfg_file = 'C:\\tools\\bots_path.cfg' 32 if not os.path.exists(path_cfg_file): 33 print 'Failed to find file', path_cfg_file 34 with open(path_cfg_file, 'r') as cfg: 35 paths = cfg.read().split('\n') 36 os.environ['PATH'] = os.pathsep.join(paths) + os.pathsep + os.environ['PATH'] 37 38 39 def _GenerateTestCommand(script, 40 chromedriver, 41 ref_chromedriver=None, 42 chrome=None, 43 chrome_version=None, 44 android_package=None): 45 cmd = [ 46 sys.executable, 47 os.path.join(_THIS_DIR, script), 48 '--chromedriver=' + chromedriver, 49 ] 50 if ref_chromedriver: 51 cmd.append('--reference-chromedriver=' + ref_chromedriver) 52 if chrome: 53 cmd.append('--chrome=' + chrome) 54 if chrome_version: 55 cmd.append('--chrome-version=' + chrome_version) 56 57 if android_package: 58 cmd.insert(0, 'xvfb-run') 59 cmd.append('--android-package=' + android_package) 60 return cmd 61 62 63 def RunPythonTests(chromedriver, ref_chromedriver, 64 chrome=None, chrome_version=None, 65 chrome_version_name=None, android_package=None): 66 version_info = '' 67 if chrome_version_name: 68 version_info = '(v%s)' % chrome_version_name 69 util.MarkBuildStepStart('python_tests%s' % version_info) 70 code = util.RunCommand( 71 _GenerateTestCommand('run_py_tests.py', 72 chromedriver, 73 ref_chromedriver=ref_chromedriver, 74 chrome=chrome, 75 chrome_version=chrome_version, 76 android_package=android_package)) 77 if code: 78 util.MarkBuildStepError() 79 return code 80 81 82 def RunJavaTests(chromedriver, chrome=None, chrome_version=None, 83 chrome_version_name=None, android_package=None): 84 version_info = '' 85 if chrome_version_name: 86 version_info = '(v%s)' % chrome_version_name 87 util.MarkBuildStepStart('java_tests%s' % version_info) 88 code = util.RunCommand( 89 _GenerateTestCommand('run_java_tests.py', 90 chromedriver, 91 ref_chromedriver=None, 92 chrome=chrome, 93 chrome_version=chrome_version, 94 android_package=android_package)) 95 if code: 96 util.MarkBuildStepError() 97 return code 98 99 100 def RunCppTests(cpp_tests): 101 util.MarkBuildStepStart('chromedriver2_tests') 102 code = util.RunCommand([cpp_tests]) 103 if code: 104 util.MarkBuildStepError() 105 return code 106 107 108 def DownloadChrome(version_name, revision, download_site): 109 util.MarkBuildStepStart('download %s' % version_name) 110 return archive.DownloadChrome(revision, util.MakeTempDir(), download_site) 111 112 113 def main(): 114 parser = optparse.OptionParser() 115 parser.add_option( 116 '', '--android-package', 117 help='Application package name, if running tests on Android.') 118 # Option 'chrome-version' is for desktop only. 119 parser.add_option( 120 '', '--chrome-version', 121 help='Version of chrome, e.g., \'HEAD\', \'27\', or \'26\'.' 122 'Default is to run tests against all of these versions.' 123 'Notice: this option only applies to desktop.') 124 options, _ = parser.parse_args() 125 126 exe_postfix = '' 127 if util.IsWindows(): 128 exe_postfix = '.exe' 129 cpp_tests_name = 'chromedriver2_tests' + exe_postfix 130 server_name = 'chromedriver2_server' + exe_postfix 131 132 required_build_outputs = [server_name] 133 if not options.android_package: 134 required_build_outputs += [cpp_tests_name] 135 build_dir = chrome_paths.GetBuildDir(required_build_outputs) 136 print 'Using build outputs from', build_dir 137 138 chromedriver = os.path.join(build_dir, server_name) 139 platform_name = util.GetPlatformName() 140 if util.IsLinux() and platform.architecture()[0] == '64bit': 141 platform_name += '64' 142 ref_chromedriver = os.path.join( 143 chrome_paths.GetSrc(), 144 'chrome', 'test', 'chromedriver', 'third_party', 'java_tests', 145 'reference_builds', 146 'chromedriver_%s%s' % (platform_name, exe_postfix)) 147 148 if util.IsLinux(): 149 # Set LD_LIBRARY_PATH to enable successful loading of shared object files, 150 # when chromedriver2.so is not a static build. 151 _AppendEnvironmentPath('LD_LIBRARY_PATH', os.path.join(build_dir, 'lib')) 152 elif util.IsWindows(): 153 # For Windows bots: add ant, java(jre) and the like to system path. 154 _AddToolsToSystemPathForWindows() 155 156 if options.android_package: 157 os.environ['PATH'] += os.pathsep + os.path.join( 158 _THIS_DIR, os.pardir, 'chrome') 159 code1 = RunPythonTests(chromedriver, 160 ref_chromedriver, 161 android_package=options.android_package) 162 code2 = RunJavaTests(chromedriver, 163 android_package=options.android_package) 164 return code1 or code2 165 else: 166 latest_snapshot_revision = archive.GetLatestRevision(archive.Site.SNAPSHOT) 167 versions = [ 168 ['HEAD', latest_snapshot_revision], 169 ['29', archive.CHROME_29_REVISION], 170 ['28', archive.CHROME_28_REVISION], 171 ['27', archive.CHROME_27_REVISION] 172 ] 173 code = 0 174 for version in versions: 175 if options.chrome_version and version[0] != options.chrome_version: 176 continue 177 download_site = archive.Site.CONTINUOUS 178 version_name = version[0] 179 if version_name == 'HEAD': 180 version_name = version[1] 181 download_site = archive.Site.SNAPSHOT 182 chrome_path = DownloadChrome(version_name, version[1], download_site) 183 code1 = RunPythonTests(chromedriver, 184 ref_chromedriver, 185 chrome=chrome_path, 186 chrome_version=version[0], 187 chrome_version_name=version_name) 188 code2 = RunJavaTests(chromedriver, chrome=chrome_path, 189 chrome_version=version[0], 190 chrome_version_name=version_name) 191 code = code or code1 or code2 192 cpp_tests = os.path.join(build_dir, cpp_tests_name) 193 return RunCppTests(cpp_tests) or code 194 195 196 if __name__ == '__main__': 197 sys.exit(main()) 198