| 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 25 matching lines...) Expand all Loading... |
| 36 from selenium.webdriver.support.ui import WebDriverWait | 36 from selenium.webdriver.support.ui import WebDriverWait |
| 37 import shutil | 37 import shutil |
| 38 import signal | 38 import signal |
| 39 import socket | 39 import socket |
| 40 import sys | 40 import sys |
| 41 import time | 41 import time |
| 42 import urllib2 | 42 import urllib2 |
| 43 import threading | 43 import threading |
| 44 | 44 |
| 45 TIMEOUT_ERROR_MSG = 'FAIL (timeout)' | 45 TIMEOUT_ERROR_MSG = 'FAIL (timeout)' |
| 46 CRASH_ERROR_MSG = 'CRASH' |
| 46 | 47 |
| 47 def correctness_test_done(source): | 48 def correctness_test_done(source): |
| 48 """Checks if test has completed.""" | 49 """Checks if test has completed.""" |
| 49 return ('PASS' in source) or ('FAIL' in source) | 50 return ('PASS' in source) or ('FAIL' in source) |
| 50 | 51 |
| 51 def perf_test_done(source): | 52 def perf_test_done(source): |
| 52 """Tests to see if our performance test is done by printing a score.""" | 53 """Tests to see if our performance test is done by printing a score.""" |
| 53 #This code is written this way to work around a current instability in the | 54 #This code is written this way to work around a current instability in the |
| 54 # python webdriver bindings if you call driver.get_element_by_id. | 55 # python webdriver bindings if you call driver.get_element_by_id. |
| 55 #TODO(efortuna): Access these elements in a nicer way using DOM parser. | 56 #TODO(efortuna): Access these elements in a nicer way using DOM parser. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 77 supports Firefox, Chrome, IE, Opera (and some mobile browsers).""" | 78 supports Firefox, Chrome, IE, Opera (and some mobile browsers).""" |
| 78 | 79 |
| 79 if isinstance(browser, selenium.selenium): | 80 if isinstance(browser, selenium.selenium): |
| 80 return run_test_in_browser_selenium_rc(browser, html_out, timeout, mode, | 81 return run_test_in_browser_selenium_rc(browser, html_out, timeout, mode, |
| 81 refresh) | 82 refresh) |
| 82 | 83 |
| 83 browser.get(html_out) | 84 browser.get(html_out) |
| 84 if refresh: | 85 if refresh: |
| 85 browser.refresh() | 86 browser.refresh() |
| 86 try: | 87 try: |
| 88 def pythonTimeout(): |
| 89 close_browser(browser) |
| 90 # If the browser is crashing selenium may not time out. |
| 91 # Explicitly catch this case with a python timer. |
| 92 t = threading.Timer(timeout, pythonTimeout) |
| 93 t.start() |
| 87 test_done = CONFIGURATIONS[mode] | 94 test_done = CONFIGURATIONS[mode] |
| 88 element = WebDriverWait(browser, float(timeout)).until( | 95 element = WebDriverWait(browser, float(timeout)).until( |
| 89 lambda driver: test_done(driver.page_source)) | 96 lambda driver: test_done(driver.page_source)) |
| 97 t.cancel() |
| 90 return browser.page_source | 98 return browser.page_source |
| 91 except selenium.common.exceptions.TimeoutException: | 99 except selenium.common.exceptions.TimeoutException: |
| 92 return TIMEOUT_ERROR_MSG | 100 return TIMEOUT_ERROR_MSG |
| 101 except: |
| 102 return CRASH_ERROR_MSG |
| 93 | 103 |
| 94 def run_test_in_browser_selenium_rc(sel, html_out, timeout, mode, refresh): | 104 def run_test_in_browser_selenium_rc(sel, html_out, timeout, mode, refresh): |
| 95 """ Run the desired test in the browser using Selenium 1.0 syntax, and wait | 105 """ Run the desired test in the browser using Selenium 1.0 syntax, and wait |
| 96 for the test to complete. This is used for Safari, since it is not currently | 106 for the test to complete. This is used for Safari, since it is not currently |
| 97 supported on Selenium 2.0.""" | 107 supported on Selenium 2.0.""" |
| 98 sel.open(html_out) | 108 sel.open(html_out) |
| 99 if refresh: | 109 if refresh: |
| 100 sel.refresh() | 110 sel.refresh() |
| 101 source = sel.get_html_source() | 111 source = sel.get_html_source() |
| 102 end_condition = CONFIGURATIONS[mode] | 112 end_condition = CONFIGURATIONS[mode] |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 if browser is None: | 229 if browser is None: |
| 220 return | 230 return |
| 221 if isinstance(browser, selenium.selenium): | 231 if isinstance(browser, selenium.selenium): |
| 222 browser.stop() | 232 browser.stop() |
| 223 return | 233 return |
| 224 | 234 |
| 225 # A timeout exception is thrown if nothing happens within the time limit. | 235 # A timeout exception is thrown if nothing happens within the time limit. |
| 226 if (type(browser) is not selenium.webdriver.chrome.webdriver.WebDriver and | 236 if (type(browser) is not selenium.webdriver.chrome.webdriver.WebDriver and |
| 227 type(browser) is not selenium.webdriver.ie.webdriver.WebDriver): | 237 type(browser) is not selenium.webdriver.ie.webdriver.WebDriver): |
| 228 browser.close() | 238 browser.close() |
| 229 browser.quit() | 239 |
| 240 # The builtin quit call will call close on the RemoteDriver which |
| 241 # may hang. Explicitly call browser.service.stop() |
| 242 if (type(browser) is selenium.webdriver.chrome.webdriver.WebDriver): |
| 243 # We may have called stop before if chrome was hanging. |
| 244 try: |
| 245 browser.service.stop() |
| 246 except: |
| 247 print("Trying to close browser that has already been closed") |
| 248 pass |
| 249 else: |
| 250 browser.quit() |
| 230 | 251 |
| 231 def report_results(mode, source, browser): | 252 def report_results(mode, source, browser): |
| 232 # TODO(vsm): Add a failure check for Dromaeo. | 253 # TODO(vsm): Add a failure check for Dromaeo. |
| 233 if mode != 'correctness': | 254 if mode != 'correctness': |
| 234 # We're running a performance test. | 255 # We're running a performance test. |
| 235 print source.encode('utf8') | 256 print source.encode('utf8') |
| 236 sys.stdout.flush() | 257 sys.stdout.flush() |
| 237 if 'NaN' in source: | 258 if 'NaN' in source: |
| 238 return 1 | 259 return 1 |
| 239 else: | 260 else: |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 sys.stderr.write('>>> EOF STDERR\n') | 335 sys.stderr.write('>>> EOF STDERR\n') |
| 315 sys.stderr.flush() | 336 sys.stderr.flush() |
| 316 | 337 |
| 317 # print one of: | 338 # print one of: |
| 318 # >>> TEST {PASS, FAIL, OK, CRASH, FAIL, TIMEOUT} | 339 # >>> TEST {PASS, FAIL, OK, CRASH, FAIL, TIMEOUT} |
| 319 status = report_results(mode, source, browser) | 340 status = report_results(mode, source, browser) |
| 320 if status == 0: | 341 if status == 0: |
| 321 print '>>> TEST PASS' | 342 print '>>> TEST PASS' |
| 322 elif source == TIMEOUT_ERROR_MSG: | 343 elif source == TIMEOUT_ERROR_MSG: |
| 323 print '>>> TEST TIMEOUT' | 344 print '>>> TEST TIMEOUT' |
| 345 elif source == CRASH_ERROR_MSG: |
| 346 print '>>> TEST CRASH' |
| 347 # The browser crashed, set the browser to None so that we will |
| 348 # create a new instance on next iteration. |
| 349 browser = None |
| 324 else: | 350 else: |
| 325 print '>>> TEST FAIL' | 351 print '>>> TEST FAIL' |
| 326 sys.stdout.flush() | 352 sys.stdout.flush() |
| 327 except: | 353 except: |
| 328 type, value, traceback = sys.exc_info() | 354 type, value, traceback = sys.exc_info() |
| 329 print "run_selenium.py: Unexpected exception occured: " | 355 print "run_selenium.py: Unexpected exception occured: " |
| 330 print " type: ", type | 356 print " type: ", type |
| 331 print " value: ", value | 357 print " value: ", value |
| 332 print " traceback: ", traceback | 358 print " traceback: ", traceback |
| 333 raise | 359 raise |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 browser = start_browser(browser_name, executable_path, html_out) | 391 browser = start_browser(browser_name, executable_path, html_out) |
| 366 | 392 |
| 367 try: | 393 try: |
| 368 output = run_test_in_browser(browser, html_out, timeout, mode, refresh) | 394 output = run_test_in_browser(browser, html_out, timeout, mode, refresh) |
| 369 return report_results(mode, output, browser) | 395 return report_results(mode, output, browser) |
| 370 finally: | 396 finally: |
| 371 close_browser(browser) | 397 close_browser(browser) |
| 372 | 398 |
| 373 if __name__ == "__main__": | 399 if __name__ == "__main__": |
| 374 sys.exit(main(sys.argv)) | 400 sys.exit(main(sys.argv)) |
| OLD | NEW |