Chromium Code Reviews| Index: tools/testing/run_selenium.py |
| =================================================================== |
| --- tools/testing/run_selenium.py (revision 2354) |
| +++ tools/testing/run_selenium.py (working copy) |
| @@ -13,13 +13,19 @@ |
| import platform |
| import selenium |
| from selenium.webdriver.support.ui import WebDriverWait |
| +import socket |
| import sys |
| +import time |
| def perf_test_done(driver): |
| """Checks if the performance test has completed.""" |
| + return perf_test_done_helper(driver.page_source) |
| + |
| +def perf_test_done_helper(source): |
| + """Tests to see if our performance test is done by printing a score.""" |
| #This code is written this way to work around a current instability in the |
| # python webdriver bindings if you call driver.get_element_by_id. |
| - source = driver.page_source |
| + #TODO(efortuna): Access these elements in a nicer way using DOM parser. |
| string = '<div id="status">' |
| index = source.find(string) |
| end_index = source.find('</div>', index+1) |
| @@ -27,7 +33,9 @@ |
| return 'Score:' in source |
| def run_test_in_browser(browser, html_out, timeout, is_perf): |
| - """Run the desired test in the browser, and wait for the test to complete.""" |
| + """Run the desired test in the browser using Selenium 2.0 WebDriver syntax, |
| + and wait for the test to complete. This is the newer syntax, that currently |
| + supports Firefox, Chrome, IE, Opera (and some mobile browsers).""" |
| browser.get("file://" + html_out) |
| source = '' |
| try: |
| @@ -44,10 +52,29 @@ |
| browser.close() |
| return source |
| +def run_test_in_browser_selenium1(sel, html_out, timeout, is_perf): |
| + """ Run the desired test in the browser using Selenium 1.0 syntax, and wait |
| + for the test to complete. This is used for Safari, since it is not currently |
| + supported on Selenium 2.0.""" |
| + sel.open('file://' + html_out) |
| + source = sel.get_html_source() |
| + end_condition = lambda(source): 'PASS' not in source and 'FAIL' not in source |
|
Jennifer Messerly
2011/12/13 01:26:30
style nit: I'd make this a function:
def end_condi
|
| + if is_perf: |
| + end_condition = perf_test_done_helper |
| + |
| + elapsed = 0 |
| + while not end_condition(source) and elapsed <= timeout: |
| + sec = .25 |
| + time.sleep(sec) |
| + elapsed += sec |
| + source = sel.get_html_source() |
| + sel.stop() |
| + return source |
| + |
| def parse_args(): |
| parser = optparse.OptionParser() |
| parser.add_option('--out', dest='out', |
| - help = 'The path for html output file that we will be writing to', |
| + help = 'The path for html output file that we will running our test from', |
| action = 'store', default = '') |
| parser.add_option('--browser', dest='browser', |
| help = 'The browser type (default = chrome)', |
| @@ -69,13 +96,34 @@ |
| if browser == 'chrome': |
| browser = selenium.webdriver.Chrome() |
| - elif browser == 'ff': |
| - browser = selenium.webdriver.Firefox() |
| + elif browser == 'ff': |
| + # By default WebDriver creates a new anonymous Firefox profile each time it |
| + # launches. This can fill up 250 GB in a little over a week! Always specify |
| + # a profile if you're running a bunch (many thousands) of Firefox tests over |
| + # time. |
| + #TODO(efortuna): Don't hard-code this path. Come up with a better solution. |
|
Jennifer Messerly
2011/12/13 01:26:30
http://code.google.com/p/selenium/wiki/FirefoxDriv
|
| + profile = selenium.webdriver.firefox.firefox_profile.FirefoxProfile( |
| + profile_directory='/Users/fortuna/Library/Application Support/' + |
| + 'Firefox/Profiles/wovq7ii0.selenium') |
| + browser = selenium.webdriver.Firefox(firefox_profile=profile) |
| elif browser == 'ie' and platform.system() == 'Windows': |
| browser = selenium.webdriver.Ie() |
| + elif browser == 'safari' and platform.system() == 'Darwin': |
| + sel = selenium.selenium('localhost', 4444, "*safari", 'file://' + html_out) |
| + try: |
| + sel.start() |
| + except socket.error: |
| + print 'ERROR: Could not connect to Selenium RC server. Are you running' +\ |
| + ' java -jar selenium-server-standalone-2.15.0.jar? If not, start ' + \ |
| + 'it before running this test.' |
| + return 1 |
| else: |
| raise Exception('Incompatible browser and platform combination.') |
| - source = run_test_in_browser(browser, html_out, timeout, is_perf) |
| + source = '' |
| + if browser == 'safari': |
| + source = run_test_in_browser_selenium1(sel, html_out, timeout, is_perf) |
| + else: |
| + source = run_test_in_browser(browser, html_out, timeout, is_perf) |
| if is_perf: |
| # We're running a performance test. |
| @@ -92,6 +140,7 @@ |
| else: |
| #The hacky way to get document.getElementById('body').innerHTML for this |
| # webpage, without the JavaScript. |
| + #TODO(efortuna): Access these elements in a nicer way using DOM parser. |
| index = source.find('<body>') |
| index += len('<body>') |
| end_index = source.find('<script') |