| 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 |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 Behavior is analogous to 'function(){ return <script> }();' | 320 Behavior is analogous to 'function(){ return <script> }();' |
| 321 | 321 |
| 322 Args: | 322 Args: |
| 323 script: A string of Javascript code. | 323 script: A string of Javascript code. |
| 324 timeout: Timeout for the Javascript code to return in seconds. | 324 timeout: Timeout for the Javascript code to return in seconds. |
| 325 Returns: | 325 Returns: |
| 326 A string of the verbatim output from the Javascript execution. | 326 A string of the verbatim output from the Javascript execution. |
| 327 """ | 327 """ |
| 328 return self.ExecuteJavascript("return " + script, timeout) | 328 return self.ExecuteJavascript("return " + script, timeout) |
| 329 | 329 |
| 330 def GetHistogram(self, histogram): | 330 def GetHistogram(self, histogram, timeout=30): |
| 331 """Gets a Chrome histogram as a dictionary object. |
| 332 |
| 333 Args: |
| 334 histogram: the name of the histogram to fetch |
| 335 timeout: timeout for the underlying Javascript query. |
| 336 |
| 337 Returns: |
| 338 A dictionary object containing information about the histogram. |
| 339 """ |
| 331 js_query = 'statsCollectionController.getBrowserHistogram("%s")' % histogram | 340 js_query = 'statsCollectionController.getBrowserHistogram("%s")' % histogram |
| 332 string_response = self.ExecuteJavascriptStatement(js_query) | 341 string_response = self.ExecuteJavascriptStatement(js_query, timeout) |
| 333 self._logger.debug('Got %s histogram=%s', histogram, string_response) | 342 self._logger.debug('Got %s histogram=%s', histogram, string_response) |
| 334 return json.loads(string_response) | 343 return json.loads(string_response) |
| 335 | 344 |
| 336 def WaitForJavascriptExpression(self, expression, timeout, min_poll=0.1, | 345 def WaitForJavascriptExpression(self, expression, timeout, min_poll=0.1, |
| 337 max_poll=1): | 346 max_poll=1): |
| 338 """Waits for the given Javascript expression to evaluate to True within the | 347 """Waits for the given Javascript expression to evaluate to True within the |
| 339 given timeout. This method polls the Javascript expression within the range | 348 given timeout. This method polls the Javascript expression within the range |
| 340 of |min_poll| and |max_poll|. | 349 of |min_poll| and |max_poll|. |
| 341 | 350 |
| 342 Args: | 351 Args: |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 for log in self._driver.execute('getLog', {'type': 'performance'})['value']: | 384 for log in self._driver.execute('getLog', {'type': 'performance'})['value']: |
| 376 message = json.loads(log['message'])['message'] | 385 message = json.loads(log['message'])['message'] |
| 377 self._logger.debug('Got Performance log: %s', log['message']) | 386 self._logger.debug('Got Performance log: %s', log['message']) |
| 378 if re.match(method_filter, message['method']): | 387 if re.match(method_filter, message['method']): |
| 379 all_messages.append(message) | 388 all_messages.append(message) |
| 380 self._logger.info('Got %d performance logs with filter method=%s', | 389 self._logger.info('Got %d performance logs with filter method=%s', |
| 381 len(all_messages), method_filter) | 390 len(all_messages), method_filter) |
| 382 self._has_logs = False | 391 self._has_logs = False |
| 383 return all_messages | 392 return all_messages |
| 384 | 393 |
| 394 def SleepUntilHistogramHasEntry(self, histogram_name, sleep_intervals=10): |
| 395 """Polls if a histogram exists in 1-6 second intervals for 10 intervals. |
| 396 Allows script to run with a timeout of 5 seconds, so the default behavior |
| 397 allows up to 60 seconds until timeout. |
| 398 |
| 399 Args: |
| 400 histogram_name: The name of the histogram to wait for |
| 401 sleep_intervals: The number of polling intervals, each polling cycle takes |
| 402 no more than 6 seconds. |
| 403 Returns: |
| 404 Whether the histogram exists |
| 405 """ |
| 406 histogram = {} |
| 407 while(not histogram and sleep_intervals > 0): |
| 408 histogram = self.GetHistogram(histogram_name, 5) |
| 409 if (not histogram): |
| 410 time.sleep(1) |
| 411 sleep_intervals -= 1 |
| 412 |
| 413 return bool(histogram) |
| 414 |
| 385 def GetHTTPResponses(self, include_favicon=False, skip_domainless_pages=True): | 415 def GetHTTPResponses(self, include_favicon=False, skip_domainless_pages=True): |
| 386 """Parses the Performance Logs and returns a list of HTTPResponse objects. | 416 """Parses the Performance Logs and returns a list of HTTPResponse objects. |
| 387 | 417 |
| 388 Use caution when calling this function multiple times. Only responses | 418 Use caution when calling this function multiple times. Only responses |
| 389 since the last time this function was called are returned (or since Chrome | 419 since the last time this function was called are returned (or since Chrome |
| 390 started, whichever is later). An Exception will be raised if no page was | 420 started, whichever is later). An Exception will be raised if no page was |
| 391 loaded since the last time this function was called. | 421 loaded since the last time this function was called. |
| 392 | 422 |
| 393 Args: | 423 Args: |
| 394 include_favicon: A bool that if True will include responses for favicons. | 424 include_favicon: A bool that if True will include responses for favicons. |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 args[0].skipTest('This test runs on Mac OS only.') | 672 args[0].skipTest('This test runs on Mac OS only.') |
| 643 return wrapper | 673 return wrapper |
| 644 | 674 |
| 645 def NotMac(func): | 675 def NotMac(func): |
| 646 def wrapper(*args, **kwargs): | 676 def wrapper(*args, **kwargs): |
| 647 if sys.platform == 'darwin': | 677 if sys.platform == 'darwin': |
| 648 func(*args, **kwargs) | 678 func(*args, **kwargs) |
| 649 else: | 679 else: |
| 650 args[0].skipTest('This test does not run on Mac OS.') | 680 args[0].skipTest('This test does not run on Mac OS.') |
| 651 return wrapper | 681 return wrapper |
| OLD | NEW |