OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import argparse | 5 import argparse |
6 import json | 6 import json |
7 import logging | 7 import logging |
8 import os | 8 import os |
9 import re | 9 import re |
10 import socket | 10 import socket |
11 import shlex | 11 import shlex |
12 import sys | 12 import sys |
13 import time | 13 import time |
14 import traceback | 14 import traceback |
15 import unittest | 15 import unittest |
| 16 import urlparse |
16 | 17 |
17 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, | 18 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, |
18 os.pardir, 'third_party', 'webdriver', 'pylib')) | 19 os.pardir, 'third_party', 'webdriver', 'pylib')) |
19 from selenium import webdriver | 20 from selenium import webdriver |
20 from selenium.webdriver.chrome.options import Options | 21 from selenium.webdriver.chrome.options import Options |
21 | 22 |
22 def ParseFlags(): | 23 def ParseFlags(): |
23 """Parses the given command line arguments. | 24 """Parses the given command line arguments. |
24 | 25 |
25 Returns: | 26 Returns: |
(...skipping 20 matching lines...) Expand all Loading... |
46 'error streams are buffered during the test run, and output from a ' | 47 'error streams are buffered during the test run, and output from a ' |
47 'passing test is discarded. Output will always be echoed normally on test ' | 48 'passing test is discarded. Output will always be echoed normally on test ' |
48 'fail or error and is added to the failure messages.', action='store_true') | 49 'fail or error and is added to the failure messages.', action='store_true') |
49 parser.add_argument('-c', '--catch', help='Control-C during the test run ' | 50 parser.add_argument('-c', '--catch', help='Control-C during the test run ' |
50 'waits for the current test to end and then reports all the results so ' | 51 'waits for the current test to end and then reports all the results so ' |
51 'far. A second Control-C raises the normal KeyboardInterrupt exception.', | 52 'far. A second Control-C raises the normal KeyboardInterrupt exception.', |
52 action='store_true') | 53 action='store_true') |
53 parser.add_argument('-f', '--failfast', help='Stop the test run on the first ' | 54 parser.add_argument('-f', '--failfast', help='Stop the test run on the first ' |
54 'error or failure.', action='store_true') | 55 'error or failure.', action='store_true') |
55 parser.add_argument('--logging_level', choices=['DEBUG', 'INFO', 'WARN', | 56 parser.add_argument('--logging_level', choices=['DEBUG', 'INFO', 'WARN', |
56 'ERROR', 'CRIT'], default='ERROR', help='The logging verbosity for log ' | 57 'ERROR', 'CRIT'], default='WARN', help='The logging verbosity for log ' |
57 'messages, printed to stderr. To see stderr logging output during a ' | 58 'messages, printed to stderr. To see stderr logging output during a ' |
58 'successful test run, also pass --disable_buffer. Default=ERROR') | 59 'successful test run, also pass --disable_buffer. Default=ERROR') |
59 parser.add_argument('--log_file', help='If given, write logging statements ' | 60 parser.add_argument('--log_file', help='If given, write logging statements ' |
60 'to the given file instead of stderr.') | 61 'to the given file instead of stderr.') |
61 return parser.parse_args(sys.argv[1:]) | 62 return parser.parse_args(sys.argv[1:]) |
62 | 63 |
63 def GetLogger(name='common'): | 64 def GetLogger(name='common'): |
64 """Creates a Logger instance with the given name and returns it. | 65 """Creates a Logger instance with the given name and returns it. |
65 | 66 |
66 If a logger has already been created with the same name, that instance is | 67 If a logger has already been created with the same name, that instance is |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 254 |
254 def LoadURL(self, url, timeout=30): | 255 def LoadURL(self, url, timeout=30): |
255 """Starts Chromium with any arguments previously given and navigates to the | 256 """Starts Chromium with any arguments previously given and navigates to the |
256 given URL. | 257 given URL. |
257 | 258 |
258 Args: | 259 Args: |
259 url: The URL to navigate to. | 260 url: The URL to navigate to. |
260 timeout: The time in seconds to load the page before timing out. | 261 timeout: The time in seconds to load the page before timing out. |
261 """ | 262 """ |
262 self._url = url | 263 self._url = url |
| 264 if (len(urlparse.urlparse(url).netloc) == 0 and |
| 265 len(urlparse.urlparse(url).scheme) == 0): |
| 266 self._logger.warn('Invalid URL: "%s". Did you forget to prepend ' |
| 267 '"http://"? See RFC 1808 for more information', url) |
263 if not self._driver: | 268 if not self._driver: |
264 self._StartDriver() | 269 self._StartDriver() |
265 self._driver.set_page_load_timeout(timeout) | 270 self._driver.set_page_load_timeout(timeout) |
266 self._logger.debug('Set page load timeout to %f seconds', timeout) | 271 self._logger.debug('Set page load timeout to %f seconds', timeout) |
267 self._driver.get(self._url) | 272 self._driver.get(self._url) |
268 self._logger.debug('Loaded page %s', url) | 273 self._logger.debug('Loaded page %s', url) |
269 | 274 |
270 def ExecuteJavascript(self, script, timeout=30): | 275 def ExecuteJavascript(self, script, timeout=30): |
271 """Executes the given javascript in the browser's current page in an | 276 """Executes the given javascript in the browser's current page in an |
272 anonymous function. | 277 anonymous function. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 all_messages = [] | 355 all_messages = [] |
351 for log in self._driver.execute('getLog', {'type': 'performance'})['value']: | 356 for log in self._driver.execute('getLog', {'type': 'performance'})['value']: |
352 message = json.loads(log['message'])['message'] | 357 message = json.loads(log['message'])['message'] |
353 self._logger.debug('Got Performance log: %s', log['message']) | 358 self._logger.debug('Got Performance log: %s', log['message']) |
354 if re.match(method_filter, message['method']): | 359 if re.match(method_filter, message['method']): |
355 all_messages.append(message) | 360 all_messages.append(message) |
356 self._logger.info('Got %d performance logs with filter method=%s', | 361 self._logger.info('Got %d performance logs with filter method=%s', |
357 len(all_messages), method_filter) | 362 len(all_messages), method_filter) |
358 return all_messages | 363 return all_messages |
359 | 364 |
360 def GetHTTPResponses(self, include_favicon=False): | 365 def GetHTTPResponses(self, include_favicon=False, skip_domainless_pages=True): |
361 """Parses the Performance Logs and returns a list of HTTPResponse objects. | 366 """Parses the Performance Logs and returns a list of HTTPResponse objects. |
362 | 367 |
363 Use caution when calling this function multiple times. Only responses | 368 Use caution when calling this function multiple times. Only responses |
364 since the last time this function was called are returned (or since Chrome | 369 since the last time this function was called are returned (or since Chrome |
365 started, whichever is later). | 370 started, whichever is later). |
366 | 371 |
367 Args: | 372 Args: |
368 include_favicon: A bool that if True will include responses for favicons. | 373 include_favicon: A bool that if True will include responses for favicons. |
| 374 skip_domainless_pages: If True, only responses with a net_loc as in RFC |
| 375 1808 will be included. Pages such as about:blank will be skipped. |
369 Returns: | 376 Returns: |
370 A list of HTTPResponse objects, each representing a single completed HTTP | 377 A list of HTTPResponse objects, each representing a single completed HTTP |
371 transaction by Chrome. | 378 transaction by Chrome. |
372 """ | 379 """ |
373 def MakeHTTPResponse(log_dict): | 380 def MakeHTTPResponse(log_dict): |
374 params = log_dict['params'] | 381 params = log_dict['params'] |
375 response_dict = params['response'] | 382 response_dict = params['response'] |
376 http_response_dict = { | 383 http_response_dict = { |
377 'response_headers': response_dict['headers'] if 'headers' in | 384 'response_headers': response_dict['headers'] if 'headers' in |
378 response_dict else {}, | 385 response_dict else {}, |
379 'request_headers': response_dict['requestHeaders'] if 'requestHeaders' | 386 'request_headers': response_dict['requestHeaders'] if 'requestHeaders' |
380 in response_dict else {}, | 387 in response_dict else {}, |
381 'url': response_dict['url'] if 'url' in response_dict else '', | 388 'url': response_dict['url'] if 'url' in response_dict else '', |
382 'protocol': response_dict['protocol'] if 'protocol' in response_dict | 389 'protocol': response_dict['protocol'] if 'protocol' in response_dict |
383 else '', | 390 else '', |
384 'port': response_dict['remotePort'] if 'remotePort' in response_dict | 391 'port': response_dict['remotePort'] if 'remotePort' in response_dict |
385 else -1, | 392 else -1, |
386 'status': response_dict['status'] if 'status' in response_dict else -1, | 393 'status': response_dict['status'] if 'status' in response_dict else -1, |
387 'request_type': params['type'] if 'type' in params else '' | 394 'request_type': params['type'] if 'type' in params else '' |
388 } | 395 } |
389 return HTTPResponse(**http_response_dict) | 396 return HTTPResponse(**http_response_dict) |
390 all_responses = [] | 397 all_responses = [] |
391 for message in self.GetPerformanceLogs(): | 398 for message in self.GetPerformanceLogs(): |
392 response = MakeHTTPResponse(message) | 399 response = MakeHTTPResponse(message) |
393 self._logger.debug('New HTTPResponse: %s', str(response)) | 400 self._logger.debug('New HTTPResponse: %s', str(response)) |
394 is_favicon = response.url.endswith('favicon.ico') | 401 is_favicon = response.url.endswith('favicon.ico') |
395 if not is_favicon or include_favicon: | 402 has_domain = len(urlparse.urlparse(response.url).netloc) > 0 |
| 403 if (not is_favicon or include_favicon) and (not skip_domainless_pages or |
| 404 has_domain): |
396 all_responses.append(response) | 405 all_responses.append(response) |
| 406 else: |
| 407 self._logger.info("Skipping HTTPResponse with url=%s in returned logs.", |
| 408 response.url) |
397 self._logger.info('%d new HTTPResponse objects found in the logs %s ' | 409 self._logger.info('%d new HTTPResponse objects found in the logs %s ' |
398 'favicons', len(all_responses), ('including' if include_favicon else | 410 'favicons', len(all_responses), ('including' if include_favicon else |
399 'not including')) | 411 'not including')) |
400 return all_responses | 412 return all_responses |
401 | 413 |
402 class HTTPResponse: | 414 class HTTPResponse: |
403 """This class represents a single HTTP transaction (request and response) by | 415 """This class represents a single HTTP transaction (request and response) by |
404 Chrome. | 416 Chrome. |
405 | 417 |
406 This class also includes several convenience functions for ChromeProxy | 418 This class also includes several convenience functions for ChromeProxy |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 """ | 533 """ |
522 flags = ParseFlags() | 534 flags = ParseFlags() |
523 logger = GetLogger() | 535 logger = GetLogger() |
524 logger.debug('Command line args: %s', str(sys.argv)) | 536 logger.debug('Command line args: %s', str(sys.argv)) |
525 logger.info('sys.argv parsed to %s', str(flags)) | 537 logger.info('sys.argv parsed to %s', str(flags)) |
526 # The unittest library uses sys.argv itself and is easily confused by our | 538 # The unittest library uses sys.argv itself and is easily confused by our |
527 # command line options. Pass it a simpler argv instead, while working in the | 539 # command line options. Pass it a simpler argv instead, while working in the |
528 # unittest command line args functionality. | 540 # unittest command line args functionality. |
529 unittest.main(argv=[sys.argv[0]], verbosity=2, failfast=flags.failfast, | 541 unittest.main(argv=[sys.argv[0]], verbosity=2, failfast=flags.failfast, |
530 catchbreak=flags.catch, buffer=(not flags.disable_buffer)) | 542 catchbreak=flags.catch, buffer=(not flags.disable_buffer)) |
OLD | NEW |