| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 | 2 |
| 3 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 3 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 4 # for details. All rights reserved. Use of this source code is governed by a | 4 # for details. All rights reserved. Use of this source code is governed by a |
| 5 # BSD-style license that can be found in the LICENSE file. | 5 # BSD-style license that can be found in the LICENSE file. |
| 6 # | 6 # |
| 7 | 7 |
| 8 """Script to actually open a browser and perform the test, and reports back with | 8 """Script to actually open a browser and perform the test, and reports back with |
| 9 the result. It uses Selenium WebDriver when possible for running the tests. It | 9 the result. It uses Selenium WebDriver when possible for running the tests. It |
| 10 uses Selenium RC for Safari. | 10 uses Selenium RC for Safari. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 stdin: --browser=ff --timeout=60 path/to/test2.html | 24 stdin: --browser=ff --timeout=60 path/to/test2.html |
| 25 stdout: >>> TEST FAIL | 25 stdout: >>> TEST FAIL |
| 26 stdin: --terminate | 26 stdin: --terminate |
| 27 $ | 27 $ |
| 28 """ | 28 """ |
| 29 | 29 |
| 30 import os | 30 import os |
| 31 import optparse | 31 import optparse |
| 32 import platform | 32 import platform |
| 33 import selenium | 33 import selenium |
| 34 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities |
| 35 from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver |
| 34 from selenium.webdriver.support.ui import WebDriverWait | 36 from selenium.webdriver.support.ui import WebDriverWait |
| 35 import shutil | 37 import shutil |
| 36 import signal | 38 import signal |
| 37 import socket | 39 import socket |
| 38 import sys | 40 import sys |
| 39 import time | 41 import time |
| 42 import urllib2 |
| 40 | 43 |
| 41 TIMEOUT_ERROR_MSG = 'FAIL (timeout)' | 44 TIMEOUT_ERROR_MSG = 'FAIL (timeout)' |
| 42 | 45 |
| 43 def correctness_test_done(source): | 46 def correctness_test_done(source): |
| 44 """Checks if test has completed.""" | 47 """Checks if test has completed.""" |
| 45 return ('PASS' in source) or ('FAIL' in source) | 48 return ('PASS' in source) or ('FAIL' in source) |
| 46 | 49 |
| 47 def perf_test_done(source): | 50 def perf_test_done(source): |
| 48 """Tests to see if our performance test is done by printing a score.""" | 51 """Tests to see if our performance test is done by printing a score.""" |
| 49 #This code is written this way to work around a current instability in the | 52 #This code is written this way to work around a current instability in the |
| (...skipping 19 matching lines...) Expand all Loading... |
| 69 | 72 |
| 70 def run_test_in_browser(browser, html_out, timeout, mode): | 73 def run_test_in_browser(browser, html_out, timeout, mode): |
| 71 """Run the desired test in the browser using Selenium 2.0 WebDriver syntax, | 74 """Run the desired test in the browser using Selenium 2.0 WebDriver syntax, |
| 72 and wait for the test to complete. This is the newer syntax, that currently | 75 and wait for the test to complete. This is the newer syntax, that currently |
| 73 supports Firefox, Chrome, IE, Opera (and some mobile browsers).""" | 76 supports Firefox, Chrome, IE, Opera (and some mobile browsers).""" |
| 74 | 77 |
| 75 if isinstance(browser, selenium.selenium): | 78 if isinstance(browser, selenium.selenium): |
| 76 return run_test_in_browser_selenium_rc(browser, html_out, timeout, mode) | 79 return run_test_in_browser_selenium_rc(browser, html_out, timeout, mode) |
| 77 | 80 |
| 78 browser.get("file://" + html_out) | 81 browser.get("file://" + html_out) |
| 79 source = '' | |
| 80 try: | 82 try: |
| 81 test_done = CONFIGURATIONS[mode] | 83 test_done = CONFIGURATIONS[mode] |
| 82 element = WebDriverWait(browser, float(timeout)).until( | 84 element = WebDriverWait(browser, float(timeout)).until( |
| 83 lambda driver: test_done(driver.page_source)) | 85 lambda driver: test_done(driver.page_source)) |
| 84 source = browser.page_source | 86 return browser.page_source |
| 85 except selenium.common.exceptions.TimeoutException: | 87 except selenium.common.exceptions.TimeoutException: |
| 86 source = TIMEOUT_ERROR_MSG | 88 return TIMEOUT_ERROR_MSG |
| 87 return source | |
| 88 | 89 |
| 89 def run_test_in_browser_selenium_rc(sel, html_out, timeout, mode): | 90 def run_test_in_browser_selenium_rc(sel, html_out, timeout, mode): |
| 90 """ Run the desired test in the browser using Selenium 1.0 syntax, and wait | 91 """ Run the desired test in the browser using Selenium 1.0 syntax, and wait |
| 91 for the test to complete. This is used for Safari, since it is not currently | 92 for the test to complete. This is used for Safari, since it is not currently |
| 92 supported on Selenium 2.0.""" | 93 supported on Selenium 2.0.""" |
| 93 sel.open('file://' + html_out) | 94 sel.open('file://' + html_out) |
| 94 source = sel.get_html_source() | 95 source = sel.get_html_source() |
| 95 end_condition = CONFIGURATIONS[mode] | 96 end_condition = CONFIGURATIONS[mode] |
| 96 | 97 |
| 97 elapsed = 0 | 98 elapsed = 0 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 122 parser.add_option('--mode', dest = 'mode', | 123 parser.add_option('--mode', dest = 'mode', |
| 123 help = 'The type of test we are running', | 124 help = 'The type of test we are running', |
| 124 action = 'store', default='correctness') | 125 action = 'store', default='correctness') |
| 125 args, _ = parser.parse_args(args=args) | 126 args, _ = parser.parse_args(args=args) |
| 126 args.out = args.out.strip('"') | 127 args.out = args.out.strip('"') |
| 127 if args.executable and args.browser != 'dartium': | 128 if args.executable and args.browser != 'dartium': |
| 128 print 'Executable path only supported when browser=dartium.' | 129 print 'Executable path only supported when browser=dartium.' |
| 129 sys.exit(1) | 130 sys.exit(1) |
| 130 return args.out, args.browser, args.executable, args.timeout, args.mode | 131 return args.out, args.browser, args.executable, args.timeout, args.mode |
| 131 | 132 |
| 133 def print_server_error(): |
| 134 """Provide the user an informative error message if we attempt to connect to |
| 135 the Selenium remote control server, but cannot access it. Then exit the |
| 136 program.""" |
| 137 print ('ERROR: Could not connect to Selenium RC server. Are you running' |
| 138 ' java -jar selenium-server-standalone-*.jar? If not, start ' |
| 139 'it before running this test.') |
| 140 sys.exit(1) |
| 141 |
| 132 def start_browser(browser, executable_path, html_out): | 142 def start_browser(browser, executable_path, html_out): |
| 133 if browser == 'chrome': | 143 if browser == 'chrome': |
| 134 # Note: you need ChromeDriver *in your path* to run Chrome, in addition to | 144 # Note: you need ChromeDriver *in your path* to run Chrome, in addition to |
| 135 # installing Chrome. Also note that the build bot runs have a different path | 145 # installing Chrome. Also note that the build bot runs have a different path |
| 136 # from a normal user -- check the build logs. | 146 # from a normal user -- check the build logs. |
| 137 return selenium.webdriver.Chrome() | 147 return selenium.webdriver.Chrome() |
| 138 elif browser == 'dartium': | 148 elif browser == 'dartium': |
| 139 script_dir = os.path.dirname(os.path.abspath(__file__)) | 149 script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 140 dartium_dir = os.path.join(script_dir, '..', '..', 'client', 'tests', | 150 dartium_dir = os.path.join(script_dir, '..', '..', 'client', 'tests', |
| 141 'dartium') | 151 'dartium') |
| (...skipping 24 matching lines...) Expand all Loading... |
| 166 # timeouts). Come up with a less hacky way to do this. | 176 # timeouts). Come up with a less hacky way to do this. |
| 167 backup_safari_prefs = os.path.dirname(__file__) + '/com.apple.Safari.plist' | 177 backup_safari_prefs = os.path.dirname(__file__) + '/com.apple.Safari.plist' |
| 168 if os.path.exists(backup_safari_prefs): | 178 if os.path.exists(backup_safari_prefs): |
| 169 shutil.copy(backup_safari_prefs, | 179 shutil.copy(backup_safari_prefs, |
| 170 '/Library/Preferences/com.apple.Safari.plist') | 180 '/Library/Preferences/com.apple.Safari.plist') |
| 171 sel = selenium.selenium('localhost', 4444, "*safari", 'file://' + html_out) | 181 sel = selenium.selenium('localhost', 4444, "*safari", 'file://' + html_out) |
| 172 try: | 182 try: |
| 173 sel.start() | 183 sel.start() |
| 174 return sel | 184 return sel |
| 175 except socket.error: | 185 except socket.error: |
| 176 print 'ERROR: Could not connect to Selenium RC server. Are you running' +\ | 186 print_server_error() |
| 177 ' java -jar selenium-server-standalone-*.jar? If not, start ' + \ | 187 elif browser == 'opera': |
| 178 'it before running this test.' | 188 try: |
| 179 sys.exit(1) | 189 driver = RemoteWebDriver(desired_capabilities=DesiredCapabilities.OPERA) |
| 190 # By default, Opera sets their script timeout (the amount of time they |
| 191 # expect to hear back from the JavaScript file) to be 10 seconds. We just |
| 192 # make it an impossibly large number so that it doesn't time out for this |
| 193 # reason, so it behaves like all of the other browser drivers. |
| 194 driver.set_script_timeout(9000) |
| 195 # If the webpage contains document.onreadystatechanged = function() {...} |
| 196 # page load event does not correctly get fired and caught (OperaDriver |
| 197 # bug). This is a band-aid. |
| 198 driver.set_page_load_timeout(1) |
| 199 return driver |
| 200 except urllib2.URLError: |
| 201 print_server_error() |
| 180 else: | 202 else: |
| 181 raise Exception('Incompatible browser and platform combination.') | 203 raise Exception('Incompatible browser and platform combination.') |
| 182 | 204 |
| 183 def close_browser(browser): | 205 def close_browser(browser): |
| 184 if browser is None: | 206 if browser is None: |
| 185 return | 207 return |
| 186 if isinstance(browser, selenium.selenium): | 208 if isinstance(browser, selenium.selenium): |
| 187 browser.stop() | 209 browser.stop() |
| 188 return | 210 return |
| 189 | 211 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 browser = start_browser(browser_name, executable_path, html_out) | 327 browser = start_browser(browser_name, executable_path, html_out) |
| 306 | 328 |
| 307 try: | 329 try: |
| 308 output = run_test_in_browser(browser, html_out, timeout, mode) | 330 output = run_test_in_browser(browser, html_out, timeout, mode) |
| 309 return report_results(mode, output) | 331 return report_results(mode, output) |
| 310 finally: | 332 finally: |
| 311 close_browser(browser) | 333 close_browser(browser) |
| 312 | 334 |
| 313 if __name__ == "__main__": | 335 if __name__ == "__main__": |
| 314 sys.exit(main(sys.argv)) | 336 sys.exit(main(sys.argv)) |
| OLD | NEW |