| 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. | 9 the result. |
| 10 Expects: | |
| 11 sys.argv[1] = html output file | |
| 12 sys.argv[2] = browser type (default = chrome) | |
| 13 """ | 10 """ |
| 14 | 11 |
| 12 import optparse |
| 15 import platform | 13 import platform |
| 16 import selenium | 14 import selenium |
| 17 from selenium.webdriver.support.ui import WebDriverWait | 15 from selenium.webdriver.support.ui import WebDriverWait |
| 18 import sys | 16 import sys |
| 19 | 17 |
| 18 def perf_test_done(driver): |
| 19 """Checks if the performance test has completed.""" |
| 20 #This code is written this way to work around a current instability in the |
| 21 # python webdriver bindings if you call driver.get_element_by_id. |
| 22 source = driver.page_source |
| 23 string = '<div id="status">' |
| 24 index = source.find(string) |
| 25 end_index = source.find('</div>', index+1) |
| 26 source = source[index + len(string):end_index] |
| 27 return 'Score:' in source |
| 20 | 28 |
| 21 def runTestInBrowser(browser): | 29 def run_test_in_browser(browser, html_out, timeout, is_perf): |
| 22 """Run the desired test in the browser, and wait for the test to complete.""" | 30 """Run the desired test in the browser, and wait for the test to complete.""" |
| 23 browser.get("file://" + sys.argv[1]) | 31 browser.get("file://" + html_out) |
| 24 source = '' | 32 source = '' |
| 25 try: | 33 try: |
| 26 element = WebDriverWait(browser, 10).until( \ | 34 if is_perf: |
| 27 lambda driver : ('PASS' in driver.page_source) or \ | 35 # We're running a performance test. |
| 28 ('FAIL' in driver.page_source)) | 36 element = WebDriverWait(browser, float(timeout)).until(perf_test_done) |
| 37 else: |
| 38 element = WebDriverWait(browser, float(timeout)).until( |
| 39 lambda driver : ('PASS' in driver.page_source) or |
| 40 ('FAIL' in driver.page_source)) |
| 29 source = browser.page_source | 41 source = browser.page_source |
| 30 finally: | 42 finally: |
| 31 # A timeout exception is thrown if nothing happens within the time limit. | 43 # A timeout exception is thrown if nothing happens within the time limit. |
| 32 browser.close() | 44 browser.close() |
| 33 return source | 45 return source |
| 34 | 46 |
| 47 def parse_args(): |
| 48 parser = optparse.OptionParser() |
| 49 parser.add_option('--out', dest='out', |
| 50 help = 'The path for html output file that we will be writing to', |
| 51 action = 'store', default = '') |
| 52 parser.add_option('--browser', dest='browser', |
| 53 help = 'The browser type (default = chrome)', |
| 54 action = 'store', default = 'chrome') |
| 55 parser.add_option('--timeout', dest = 'timeout', |
| 56 help = 'Amount of time (seconds) to wait before timeout', type = 'int', |
| 57 action = 'store', default=10) |
| 58 parser.add_option('--perf', dest = 'is_perf', |
| 59 help = 'Add this flag if we are running a browser performance test', |
| 60 action = 'store_true', default=False) |
| 61 args, ignored = parser.parse_args() |
| 62 return args.out, args.browser, args.timeout, args.is_perf |
| 63 |
| 35 def Main(): | 64 def Main(): |
| 36 # Note: you need ChromeDriver *in your path* to run Chrome, in addition to | 65 # Note: you need ChromeDriver *in your path* to run Chrome, in addition to |
| 37 # installing Chrome. | 66 # installing Chrome. |
| 38 browser = None | 67 browser = None |
| 39 if sys.argv[2] == 'chrome': | 68 html_out, browser, timeout, is_perf = parse_args() |
| 40 browser = selenium.webdriver.Chrome() | 69 |
| 41 elif sys.argv[2] == 'ff': | 70 if browser == 'chrome': |
| 71 browser = selenium.webdriver.Chrome() |
| 72 elif browser == 'ff': |
| 42 browser = selenium.webdriver.Firefox() | 73 browser = selenium.webdriver.Firefox() |
| 43 elif sys.argv[2] == 'ie' and platform.system() == 'Windows': | 74 elif browser == 'ie' and platform.system() == 'Windows': |
| 44 browser = selenium.webdriver.Ie() | 75 browser = selenium.webdriver.Ie() |
| 45 else: | 76 else: |
| 46 raise Exception('Incompatible browser and platform combination.') | 77 raise Exception('Incompatible browser and platform combination.') |
| 47 source = runTestInBrowser(browser) | 78 source = run_test_in_browser(browser, html_out, timeout, is_perf) |
| 48 | 79 |
| 49 if ('PASS' in source): | 80 if is_perf: |
| 50 print 'Content-Type: text/plain\nPASS' | 81 # We're running a performance test. |
| 51 return 0 | 82 print source |
| 83 if 'NaN' in source: |
| 84 return 1 |
| 85 else: |
| 86 return 0 |
| 52 else: | 87 else: |
| 53 index = source.find('<body>') | 88 # We're running a correctness test. |
| 54 index += len('<body>') | 89 if ('PASS' in source): |
| 55 end_index = source.find('<script') | 90 print 'Content-Type: text/plain\nPASS' |
| 56 print source[index : end_index] | 91 return 0 |
| 57 return 1 | 92 else: |
| 93 #The hacky way to get document.getElementById('body').innerHTML for this |
| 94 # webpage, without the JavaScript. |
| 95 index = source.find('<body>') |
| 96 index += len('<body>') |
| 97 end_index = source.find('<script') |
| 98 print source[index : end_index] |
| 99 return 1 |
| 58 | 100 |
| 59 | 101 |
| 60 if __name__ == "__main__": | 102 if __name__ == "__main__": |
| 61 sys.exit(Main()) | 103 sys.exit(Main()) |
| OLD | NEW |