Chromium Code Reviews| 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: | 10 Expects: |
| 11 sys.argv[1] = html output file | 11 sys.argv[1] = html output file |
| 12 sys.argv[2] = browser type (default = chrome) | 12 sys.argv[2] = browser type (default = chrome) |
| 13 sys.argv[3] = time to wait before timeout | |
| 14 sys.argv[4] = is_perf_test -- a boolean indicating if this test is a | |
|
Siggi Cherem (dart-lang)
2011/11/23 20:00:29
you might want to use the [optparse] package and m
Emily Fortuna
2011/11/30 00:37:13
Done.
| |
| 15 performance test. Default (no argument provided) is False. | |
| 13 """ | 16 """ |
| 14 | 17 |
| 15 import platform | 18 import platform |
| 16 import selenium | 19 import selenium |
| 17 from selenium.webdriver.support.ui import WebDriverWait | 20 from selenium.webdriver.support.ui import WebDriverWait |
| 18 import sys | 21 import sys |
| 19 | 22 |
| 23 def perf_test_done(driver): | |
| 24 """Tests if the performance test has completed.""" | |
|
Siggi Cherem (dart-lang)
2011/11/23 20:00:29
Tests -> Checks :)
Emily Fortuna
2011/11/30 00:37:13
Done.
| |
| 25 #This code is written this way to work around a current instability in the | |
| 26 # python webdriver bindings if you call driver.get_element_by_id. | |
|
Siggi Cherem (dart-lang)
2011/11/23 20:00:29
strange - remind me to chat about this in person :
| |
| 27 source = driver.page_source | |
| 28 string = '<div id="status">' | |
| 29 index = source.find(string) | |
| 30 end_index = source.find('</div>', index+1) | |
| 31 source = source[index + len(string):end_index] | |
| 32 return 'Score:' in source | |
| 20 | 33 |
| 21 def runTestInBrowser(browser): | 34 def run_test_in_browser(browser): |
| 22 """Run the desired test in the browser, and wait for the test to complete.""" | 35 """Run the desired test in the browser, and wait for the test to complete.""" |
| 23 browser.get("file://" + sys.argv[1]) | 36 browser.get("file://" + sys.argv[1]) |
| 24 source = '' | 37 source = '' |
| 25 try: | 38 try: |
| 26 element = WebDriverWait(browser, 10).until( \ | 39 if len(sys.argv) > 4: |
| 27 lambda driver : ('PASS' in driver.page_source) or \ | 40 # We're running a performance test. |
| 28 ('FAIL' in driver.page_source)) | 41 element = WebDriverWait(browser, float(sys.argv[3])).until(perf_test_done) |
| 42 else: | |
| 43 element = WebDriverWait(browser, float(sys.argv[3])).until( | |
| 44 lambda driver : ('PASS' in driver.page_source) or | |
| 45 ('FAIL' in driver.page_source)) | |
| 29 source = browser.page_source | 46 source = browser.page_source |
| 30 finally: | 47 finally: |
| 31 # A timeout exception is thrown if nothing happens within the time limit. | 48 # A timeout exception is thrown if nothing happens within the time limit. |
| 32 browser.close() | 49 browser.close() |
| 33 return source | 50 return source |
| 34 | 51 |
| 35 def Main(): | 52 def Main(): |
| 36 # Note: you need ChromeDriver *in your path* to run Chrome, in addition to | 53 # Note: you need ChromeDriver *in your path* to run Chrome, in addition to |
| 37 # installing Chrome. | 54 # installing Chrome. |
| 38 browser = None | 55 browser = None |
| 39 if sys.argv[2] == 'chrome': | 56 if sys.argv[2] == 'chrome': |
| 40 browser = selenium.webdriver.Chrome() | 57 browser = selenium.webdriver.Chrome() |
| 41 elif sys.argv[2] == 'ff': | 58 elif sys.argv[2] == 'ff': |
| 42 browser = selenium.webdriver.Firefox() | 59 browser = selenium.webdriver.Firefox() |
| 43 elif sys.argv[2] == 'ie' and platform.system() == 'Windows': | 60 elif sys.argv[2] == 'ie' and platform.system() == 'Windows': |
| 44 browser = selenium.webdriver.Ie() | 61 browser = selenium.webdriver.Ie() |
| 45 else: | 62 else: |
| 46 raise Exception('Incompatible browser and platform combination.') | 63 raise Exception('Incompatible browser and platform combination.') |
| 47 source = runTestInBrowser(browser) | 64 source = run_test_in_browser(browser) |
| 48 | 65 |
| 49 if ('PASS' in source): | 66 if len(sys.argv) > 4: |
| 50 print 'Content-Type: text/plain\nPASS' | 67 # We're running a performance test. |
| 51 return 0 | 68 print source |
| 69 if 'NaN' in source: | |
| 70 return 1 | |
| 71 else: | |
| 72 return 0 | |
| 52 else: | 73 else: |
| 53 index = source.find('<body>') | 74 # We're running a correctness test. |
| 54 index += len('<body>') | 75 if ('PASS' in source): |
| 55 end_index = source.find('<script') | 76 print 'Content-Type: text/plain\nPASS' |
| 56 print source[index : end_index] | 77 return 0 |
| 57 return 1 | 78 else: |
| 79 #The hacky way to get document.getElementById('body').innerHTML for this | |
| 80 # webpage, without the JavaScript. | |
| 81 index = source.find('<body>') | |
| 82 index += len('<body>') | |
| 83 end_index = source.find('<script') | |
| 84 print source[index : end_index] | |
| 85 return 1 | |
| 58 | 86 |
| 59 | 87 |
| 60 if __name__ == "__main__": | 88 if __name__ == "__main__": |
| 61 sys.exit(Main()) | 89 sys.exit(Main()) |
| OLD | NEW |