run_layout_tests.py revision 42bf092c47c4af5fbc85283bccdb97fbe4ab76a9
1#!/usr/bin/python
2
3"""Run layout tests on the device.
4
5  It runs the specified tests on the device, downloads the summaries to the temporary directory
6  and optionally shows the detailed results the host's default browser.
7
8  Usage:
9    run_layout_tests.py --show-results-in-browser test-relative-path
10"""
11
12import logging
13import optparse
14import os
15import re
16import sys
17import subprocess
18import tempfile
19import webbrowser
20
21#TODO: These should not be hardcoded
22RESULTS_ABSOLUTE_PATH = "/sdcard/layout-test-results/"
23DETAILS_HTML = "details.html"
24SUMMARY_TXT = "summary.txt"
25
26def main(options, args):
27  if args:
28    path = " ".join(args);
29  else:
30    path = "";
31
32  logging.basicConfig(level=logging.INFO, format='%(message)s')
33
34  tmpdir = tempfile.gettempdir()
35
36  if options.tests_root_directory != None:
37    # if options.tests_root_directory is absolute, os.getcwd() is discarded!
38    tests_root_directory = os.path.normpath(os.path.join(os.getcwd(), options.tests_root_directory))
39    server_options = " --tests-root-directory=" + tests_root_directory
40  else:
41    server_options = "";
42
43  # Restart the server
44  cmd = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), "run_apache2.py") + server_options + " restart"
45  os.system(cmd);
46
47  # Run the tests in path
48  cmd = "adb shell am instrument "
49  cmd += "-e class com.android.dumprendertree2.scriptsupport.Starter#startLayoutTests "
50  cmd += "-e path \"" + path + "\" "
51  cmd += "-w com.android.dumprendertree2/com.android.dumprendertree2.scriptsupport.ScriptTestRunner"
52
53  logging.info("Running the tests...")
54  (stdoutdata, stderrdata) = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
55  if re.search("^INSTRUMENTATION_STATUS_CODE: -1", stdoutdata, re.MULTILINE) != None:
56    logging.info("Failed to run the tests. Is DumpRenderTree2 installed on the device?")
57    return
58
59  logging.info("Downloading the summaries...")
60
61  # Download the txt summary to tmp folder
62  summary_txt_tmp_path = os.path.join(tmpdir, SUMMARY_TXT)
63  cmd = "rm -f " + summary_txt_tmp_path + ";"
64  cmd += "adb pull " + RESULTS_ABSOLUTE_PATH + SUMMARY_TXT + " " + summary_txt_tmp_path
65  subprocess.Popen(cmd, shell=True).wait()
66
67  # Download the html summary to tmp folder
68  details_html_tmp_path = os.path.join(tmpdir, DETAILS_HTML)
69  cmd = "rm -f " + details_html_tmp_path + ";"
70  cmd += "adb pull " + RESULTS_ABSOLUTE_PATH + DETAILS_HTML + " " + details_html_tmp_path
71  subprocess.Popen(cmd, shell=True).wait()
72
73  # Print summary to console
74  logging.info("All done.\n")
75  cmd = "cat " + summary_txt_tmp_path
76  os.system(cmd)
77  logging.info("")
78
79  # Open the browser with summary
80  if options.show_results_in_browser != "false":
81    webbrowser.open(details_html_tmp_path)
82
83if __name__ == "__main__":
84  option_parser = optparse.OptionParser(usage="Usage: %prog [options] test-relative-path")
85  option_parser.add_option("", "--show-results-in-browser", default="true",
86                           help="Show the results the host's default web browser, default=true")
87  option_parser.add_option("", "--tests-root-directory",
88                           help="The directory from which to take the tests, default is external/webkit/LayoutTests in this checkout of the Android tree")
89  options, args = option_parser.parse_args();
90  main(options, args);
91