forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_chrome_with_chromedriver.py
executable file
·185 lines (167 loc) · 6.73 KB
/
test_chrome_with_chromedriver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python3
# Copyright 2018 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Test Chrome using chromedriver.
If the webdriver API or the chromedriver.exe binary can't be found this
becomes a no-op. This is to allow running locally while the waterfalls get
setup. Once all locations have been provisioned and are expected to contain
these items the checks should be removed to ensure this is run on each test
and fails if anything is incorrect.
"""
import argparse
import contextlib
import logging
import os
import shutil
import sys
import tempfile
import time
from selenium import webdriver
from selenium.webdriver import ChromeOptions
import chrome_helper
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
TEST_HTML_FILE = 'file://' + os.path.join(THIS_DIR, 'test_page.html')
@contextlib.contextmanager
def CreateChromedriver(args):
"""Create a webdriver object and close it after."""
def DeleteWithRetry(path, func):
# There seems to be a race condition on the bots that causes the paths
# to not delete because they are being used. This allows up to 4 seconds
# to delete
for _ in range(8):
try:
return func(path)
except WindowsError:
time.sleep(0.5)
raise
def CollectCrashReports(user_data_dir, output_dir):
"""Searches for Chrome crash reports, collecting them for analysis.
Args:
user_data_dir: The full path of the User Data dir.
output_dir: If not None, a path to which collected crash reports are
to be moved.
Returns:
The number of crash reports found.
"""
report_dir = os.path.join(user_data_dir, 'Crashpad', 'reports')
dumps = []
try:
dumps = os.listdir(report_dir)
except OSError:
# Assume this is file not found, meaning no crash reports.
return 0
for dump in dumps:
dump_path = os.path.join(report_dir, dump)
if (output_dir):
target_path = os.path.join(output_dir, dump)
try:
shutil.copyfile(dump_path, target_path)
logging.error('Saved Chrome crash dump to %s', target_path)
except OSError:
logging.exception(
'Failed to copy Chrome crash dump from %s to %s',
dump_path, target_path)
else:
logging.error('Found Chrome crash dump at %s', dump_path)
return len(dumps)
driver = None
user_data_dir = tempfile.mkdtemp()
fd, log_file = tempfile.mkstemp()
os.close(fd)
chrome_options = ChromeOptions()
chrome_options.binary_location = args.chrome_path
chrome_options.add_argument('user-data-dir=' + user_data_dir)
chrome_options.add_argument('log-file=' + log_file)
chrome_options.add_argument('enable-logging')
chrome_options.add_argument('v=1')
emit_log = False
try:
driver = webdriver.Chrome(args.chromedriver_path,
chrome_options=chrome_options)
yield driver
except:
emit_log = True
raise
finally:
if driver:
driver.quit()
chrome_helper.WaitForChromeExit(args.chrome_path)
report_count = CollectCrashReports(user_data_dir, args.output_dir)
if report_count:
emit_log = True
try:
DeleteWithRetry(user_data_dir, shutil.rmtree)
except:
emit_log = True
raise
finally:
if emit_log:
with open(log_file) as fh:
logging.error(fh.read())
if args.output_dir:
target = os.path.join(args.output_dir,
os.path.basename(log_file))
shutil.copyfile(log_file, target)
logging.error('Saved Chrome log to %s', target)
try:
DeleteWithRetry(log_file, os.remove)
except WindowsError:
# Don't fail the test if the log file couldn't be deleted.
logging.exception('Failed to delete log file %s' % log_file)
if report_count:
raise Exception('Failing test due to %s crash reports found' %
report_count)
def main():
"""Main entry point."""
parser = parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-q',
'--quiet',
action='store_true',
default=False,
help='Reduce test runner output')
parser.add_argument('--chromedriver-path',
default='chromedriver.exe',
metavar='FILENAME',
help='Path to chromedriver')
parser.add_argument(
'--output-dir',
metavar='DIR',
help='Directory into which crash dumps and other output '
' files are to be written')
parser.add_argument('chrome_path',
metavar='FILENAME',
help='Path to chrome installer')
args = parser.parse_args()
# This test is run from src, but this script is called with a cwd of
# chrome/test/mini_installer, so relative paths need to be compensated for.
if not os.path.exists(args.chromedriver_path):
args.chromedriver_path = os.path.join('..', '..', '..',
args.chromedriver_path)
if not os.path.exists(args.chrome_path):
args.chrome_path = os.path.join('..', '..', '..', args.chrome_path)
logging.basicConfig(
format='[%(asctime)s:%(filename)s(%(lineno)d)] %(message)s',
datefmt='%m%d/%H%M%S',
level=logging.ERROR if args.quiet else logging.INFO)
if not args.chrome_path:
logging.error('The path to the chrome binary is required.')
return -1
# Check that chromedriver is correct.
if not os.path.exists(args.chromedriver_path):
# If we can't find chromedriver exit as a no-op.
logging.info('Cant find %s. Exiting test_chrome_with_chromedriver',
args.chromedriver_path)
return 0
with CreateChromedriver(args) as driver:
driver.get(TEST_HTML_FILE)
assert driver.title == 'Chromedriver Test Page', (
'The page title was not correct.')
element = driver.find_element_by_tag_name('body')
assert element.text == 'This is the test page', (
'The page body was not correct')
return 0
if __name__ == '__main__':
sys.exit(main())