run_layout_tests.py revision 82c11b42f1564904eb4407d6e30a31ea02d81644
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  adb_cmd = "adb"
49  if options.serial:
50    adb_cmd += " -s " + options.serial
51  cmd = adb_cmd + " shell am instrument "
52  cmd += "-e class com.android.dumprendertree2.scriptsupport.Starter#startLayoutTests "
53  cmd += "-e path \"" + path + "\" "
54  cmd += "-w com.android.dumprendertree2/com.android.dumprendertree2.scriptsupport.ScriptTestRunner"
55
56  logging.info("Running the tests...")
57  (stdoutdata, stderrdata) = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
58  if re.search("^INSTRUMENTATION_STATUS_CODE: -1", stdoutdata, re.MULTILINE) != None:
59    logging.info("Failed to run the tests. Is DumpRenderTree2 installed on the device?")
60    return
61
62  logging.info("Downloading the summaries...")
63
64  # Download the txt summary to tmp folder
65  summary_txt_tmp_path = os.path.join(tmpdir, SUMMARY_TXT)
66  cmd = "rm -f " + summary_txt_tmp_path + ";"
67  cmd += adb_cmd + " pull " + RESULTS_ABSOLUTE_PATH + SUMMARY_TXT + " " + summary_txt_tmp_path
68  subprocess.Popen(cmd, shell=True).wait()
69
70  # Download the html summary to tmp folder
71  details_html_tmp_path = os.path.join(tmpdir, DETAILS_HTML)
72  cmd = "rm -f " + details_html_tmp_path + ";"
73  cmd += adb_cmd + " pull " + RESULTS_ABSOLUTE_PATH + DETAILS_HTML + " " + details_html_tmp_path
74  subprocess.Popen(cmd, shell=True).wait()
75
76  # Print summary to console
77  logging.info("All done.\n")
78  cmd = "cat " + summary_txt_tmp_path
79  os.system(cmd)
80  logging.info("")
81
82  # Open the browser with summary
83  if options.show_results_in_browser != "false":
84    webbrowser.open(details_html_tmp_path)
85
86if __name__ == "__main__":
87  option_parser = optparse.OptionParser(usage="Usage: %prog [options] test-relative-path")
88  option_parser.add_option("", "--show-results-in-browser", default="true",
89                           help="Show the results the host's default web browser, default=true")
90  option_parser.add_option("", "--tests-root-directory",
91                           help="The directory from which to take the tests, default is external/webkit/LayoutTests in this checkout of the Android tree")
92  option_parser.add_option("-s", "--serial", default=None, help="Specify the serial number of device to run test on")
93  options, args = option_parser.parse_args();
94  main(options, args);
95