1 # Copyright 2020 The Chromium Authors
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 This script runs Chrome and automatically navigates through the given list of
6 URLs the specified number of times.
8 Usage: vpython3 auto-nav.py <chrome dir> <number of navigations> <url> <url> ...
11 * --interval <seconds>, -i <seconds>: specify a number of seconds to wait
12 between navigations, e.g., -i=5
13 * --start_prompt, -s: start Chrome, then wait for the user to press Enter before
14 starting auto-navigation
15 * --exit-prompt, -e: after auto-navigation, wait for the user to press Enter
16 before shutting down chrome.exe
17 * --idlewakeups_dir: Windows only; specify the directory containing
18 idlewakeups.exe to print measurements taken by IdleWakeups,
19 e.g., --idlewakeups_dir=tools/win/IdleWakeups/x64/Debug
21 Optional flags to chrome.exe, example:
22 -- --user-data-dir=temp --disable-features=SomeFeature
23 Note: must be at end of command, following options terminator "--". The options
24 terminator stops command-line options from being interpreted as options for this
25 script, which would cause an unrecognized-argument error.
29 # python_version: "3.8"
31 # name: "infra/python/wheels/selenium-py2_py3"
32 # version: "version:3.14.0"
35 # name: "infra/python/wheels/urllib3-py2_py3"
36 # version: "version:1.24.3"
39 # name: "infra/python/wheels/psutil/${vpython_platform}"
40 # version: "version:5.7.2"
53 from selenium import webdriver
55 print('Error importing required modules. Run with vpython3 instead of '
62 # Splits list |positional_args| into two lists: |urls| and |chrome_args|, where
63 # arguments starting with '-' are treated as chrome args, and the rest as URLs.
64 def ParsePositionalArgs(positional_args):
65 urls, chrome_args = [], []
66 for arg in positional_args:
67 if arg.startswith('-'):
68 chrome_args.append(arg)
71 return [urls, chrome_args]
74 # Returns an object containing the arguments parsed from this script's command
77 # Customize usage and help to include options to be passed to chrome.exe.
78 usage_text = '''%(prog)s [-h] [--interval INTERVAL] [--start_prompt]
79 [--exit_prompt] [--idlewakeups_dir IDLEWAKEUPS_DIR]
80 chrome_dir num_navigations url [url ...]
81 [-- --chrome_option ...]'''
82 additional_help_text = '''optional arguments to chrome.exe, example:
83 -- --enable-features=MyFeature --browser-startup-dialog
84 Must be at end of command, following the options
86 parser = argparse.ArgumentParser(
87 epilog=additional_help_text,
89 formatter_class=argparse.RawDescriptionHelpFormatter)
91 'chrome_dir', help='Directory containing chrome.exe and chromedriver.exe')
92 parser.add_argument('num_navigations',
94 help='Number of times to navigate through list of URLs')
95 parser.add_argument('--interval',
98 help='Seconds to wait between navigations; default is 1')
99 parser.add_argument('--start_prompt',
102 help='Wait for confirmation before starting navigation')
103 parser.add_argument('--exit_prompt',
106 help='Wait for confirmation before exiting chrome.exe')
109 help='Windows only; directory containing idlewakeups.exe, if using')
113 help='URL(s) to navigate, separated by spaces; must include scheme, '
115 args = parser.parse_args()
116 args.url, chrome_args = ParsePositionalArgs(args.url)
119 print(os.path.basename(__file__) + ': error: missing URL argument')
120 exit(EXIT_CODE_ERROR)
122 if not urllib.parse.urlparse(url).scheme:
123 print(os.path.basename(__file__) +
124 ': error: URL is missing required scheme (e.g., "https://"): ' + url)
125 exit(EXIT_CODE_ERROR)
126 return [args, chrome_args]
129 # If |path| does not exist, prints a generic error plus optional |error_message|
131 def ExitIfNotFound(path, error_message=None):
132 if not os.path.exists(path):
133 print('File not found: {}.'.format(path))
136 exit(EXIT_CODE_ERROR)
140 # Parse arguments and check that file paths received are valid.
141 args, chrome_args = ParseArgs()
142 ExitIfNotFound(os.path.join(args.chrome_dir, 'chrome.exe'),
143 'Build target "chrome" to generate it first.')
144 chromedriver_exe = os.path.join(args.chrome_dir, 'chromedriver.exe')
145 ExitIfNotFound(chromedriver_exe,
146 'Build target "chromedriver" to generate it first.')
147 if args.idlewakeups_dir:
148 idlewakeups_exe = os.path.join(args.idlewakeups_dir, 'idlewakeups.exe')
149 ExitIfNotFound(idlewakeups_exe)
151 # Start chrome.exe. Disable chrome.exe's extensive logging to make reading
152 # this script's output easier.
153 chrome_options = webdriver.ChromeOptions()
154 chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
155 for arg in chrome_args:
156 chrome_options.add_argument(arg)
157 driver = webdriver.Chrome(os.path.abspath(chromedriver_exe),
158 options=chrome_options)
160 if args.start_prompt:
161 driver.get(args.url[0])
162 input('Press Enter to begin navigation...')
164 # Start IdleWakeups, if using, passing the browser process's ID as its target.
165 # IdleWakeups will monitor the browser process and its children. Other running
166 # chrome.exe processes (i.e., those not launched by this script) are excluded.
167 if args.idlewakeups_dir:
168 launched_processes = psutil.Process(
169 driver.service.process.pid).children(recursive=False)
170 if not launched_processes:
171 print('Error getting browser process ID for IdleWakeups.')
173 # Assume the first child process created by |driver| is the browser process.
174 idlewakeups = subprocess.Popen([
176 str(launched_processes[0].pid), '--stop-on-exit', '--tabbed'
178 stdout=subprocess.PIPE)
180 # Navigate through |args.url| list |args.num_navigations| times, then close
182 interval = args.interval if args.interval else DEFAULT_INTERVAL
183 for _ in range(args.num_navigations):
189 input('Press Enter to exit...')
192 # Print IdleWakeups' output, if using.
193 if args.idlewakeups_dir:
194 print(idlewakeups.communicate()[0])
197 if __name__ == '__main__':