Upload upstream chromium 94.0.4606.31
[platform/framework/web/chromium-efl.git] / tools / auto-nav.py
1 # Copyright 2020 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4 """
5 This script runs Chrome and automatically navigates through the given list of
6 URLs the specified number of times.
7
8 Usage: vpython3 auto-nav.py <chrome dir> <number of navigations> <url> <url> ...
9
10 Optional flags:
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
20
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.
26 """
27
28 # [VPYTHON:BEGIN]
29 # python_version: "3.8"
30 # wheel: <
31 #   name: "infra/python/wheels/selenium-py2_py3"
32 #   version: "version:3.14.0"
33 # >
34 # wheel: <
35 #   name: "infra/python/wheels/urllib3-py2_py3"
36 #   version: "version:1.24.3"
37 # >
38 # wheel: <
39 #   name: "infra/python/wheels/psutil/${vpython_platform}"
40 #   version: "version:5.7.2"
41 # >
42 # [VPYTHON:END]
43
44 import argparse
45 import os
46 import subprocess
47 import sys
48 import time
49 import urllib
50
51 try:
52   import psutil
53   from selenium import webdriver
54 except ImportError:
55   print('Error importing required modules. Run with vpython3 instead of python.')
56   sys.exit(1)
57
58 DEFAULT_INTERVAL = 1
59 EXIT_CODE_ERROR = 1
60
61 # Splits list |positional_args| into two lists: |urls| and |chrome_args|, where
62 # arguments starting with '-' are treated as chrome args, and the rest as URLs.
63 def ParsePositionalArgs(positional_args):
64   urls, chrome_args = [], []
65   for arg in positional_args:
66     if arg.startswith('-'):
67       chrome_args.append(arg)
68     else:
69       urls.append(arg)
70   return [urls, chrome_args]
71
72
73 # Returns an object containing the arguments parsed from this script's command
74 # line.
75 def ParseArgs():
76   # Customize usage and help to include options to be passed to chrome.exe.
77   usage_text = '''%(prog)s [-h] [--interval INTERVAL] [--wait]
78                    [--idlewakeups_dir IDLEWAKEUPS_DIR]
79                    chrome_dir num_navigations url [url ...]
80                    [-- --chrome_option ...]'''
81   additional_help_text = '''optional arguments to chrome.exe, example:
82   -- --enable-features=MyFeature --browser-startup-dialog
83                         Must be at end of command, following the options
84                         terminator "--"'''
85   parser = argparse.ArgumentParser(
86       epilog=additional_help_text,
87       usage=usage_text,
88       formatter_class=argparse.RawDescriptionHelpFormatter)
89   parser.add_argument(
90       'chrome_dir', help='Directory containing chrome.exe and chromedriver.exe')
91   parser.add_argument('num_navigations',
92                       type=int,
93                       help='Number of times to navigate through list of URLs')
94   parser.add_argument('--interval',
95                       '-i',
96                       type=int,
97                       help='Seconds to wait between navigations; default is 1')
98   parser.add_argument('--start_prompt',
99                       '-s',
100                       action='store_true',
101                       help='Wait for confirmation before starting navigation')
102   parser.add_argument('--exit_prompt',
103                       '-e',
104                       action='store_true',
105                       help='Wait for confirmation before exiting chrome.exe')
106   parser.add_argument(
107       '--idlewakeups_dir',
108       help='Windows only; directory containing idlewakeups.exe, if using')
109   parser.add_argument(
110       'url',
111       nargs='+',
112       help='URL(s) to navigate, separated by spaces; must include scheme, '
113       'e.g., "https://"')
114   args = parser.parse_args()
115   args.url, chrome_args = ParsePositionalArgs(args.url)
116   if not args.url:
117     parser.print_usage()
118     print(os.path.basename(__file__) + ': error: missing URL argument')
119     exit(EXIT_CODE_ERROR)
120   for url in args.url:
121     if not urllib.parse.urlparse(url).scheme:
122       print(os.path.basename(__file__) +
123             ': error: URL is missing required scheme (e.g., "https://"): ' + url)
124       exit(EXIT_CODE_ERROR)
125   return [args, chrome_args]
126
127
128 # If |path| does not exist, prints a generic error plus optional |error_message|
129 # and exits.
130 def ExitIfNotFound(path, error_message=None):
131   if not os.path.exists(path):
132     print('File not found: {}.'.format(path))
133     if error_message:
134       print(error_message)
135     exit(EXIT_CODE_ERROR)
136
137
138 def main():
139   # Parse arguments and check that file paths received are valid.
140   args, chrome_args = ParseArgs()
141   ExitIfNotFound(os.path.join(args.chrome_dir, 'chrome.exe'),
142                  'Build target "chrome" to generate it first.')
143   chromedriver_exe = os.path.join(args.chrome_dir, 'chromedriver.exe')
144   ExitIfNotFound(chromedriver_exe,
145                  'Build target "chromedriver" to generate it first.')
146   if args.idlewakeups_dir:
147     idlewakeups_exe = os.path.join(args.idlewakeups_dir, 'idlewakeups.exe')
148     ExitIfNotFound(idlewakeups_exe)
149
150   # Start chrome.exe. Disable chrome.exe's extensive logging to make reading
151   # this script's output easier.
152   chrome_options = webdriver.ChromeOptions()
153   chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
154   for arg in chrome_args:
155     chrome_options.add_argument(arg)
156   driver = webdriver.Chrome(os.path.abspath(chromedriver_exe),
157                             options=chrome_options)
158
159   if args.start_prompt:
160     driver.get(args.url[0])
161     input('Press Enter to begin navigation...')
162
163   # Start IdleWakeups, if using, passing the browser process's ID as its target.
164   # IdleWakeups will monitor the browser process and its children. Other running
165   # chrome.exe processes (i.e., those not launched by this script) are excluded.
166   if args.idlewakeups_dir:
167     launched_processes = psutil.Process(
168         driver.service.process.pid).children(recursive=False)
169     if not launched_processes:
170       print('Error getting browser process ID for IdleWakeups.')
171       exit()
172     # Assume the first child process created by |driver| is the browser process.
173     idlewakeups = subprocess.Popen([
174         idlewakeups_exe,
175         str(launched_processes[0].pid), '--stop-on-exit', '--tabbed'
176     ],
177                                    stdout=subprocess.PIPE)
178
179   # Navigate through |args.url| list |args.num_navigations| times, then close
180   # chrome.exe.
181   interval = args.interval if args.interval else DEFAULT_INTERVAL
182   for _ in range(args.num_navigations):
183     for url in args.url:
184       driver.get(url)
185       time.sleep(interval)
186
187   if args.exit_prompt:
188     input('Press Enter to exit...')
189   driver.quit()
190
191   # Print IdleWakeups' output, if using.
192   if args.idlewakeups_dir:
193     print(idlewakeups.communicate()[0])
194
195
196 if __name__ == '__main__':
197   sys.exit(main())