| 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 os | 7 import os |
| 8 import re | 8 import re |
| 9 import socket | 9 import socket |
| 10 import shlex | 10 import shlex |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 """Clears the browser cache. | 192 """Clears the browser cache. |
| 193 | 193 |
| 194 Important note: ChromeDriver automatically starts | 194 Important note: ChromeDriver automatically starts |
| 195 a clean copy of Chrome on every instantiation. | 195 a clean copy of Chrome on every instantiation. |
| 196 """ | 196 """ |
| 197 self.ExecuteJavascript('if(window.chrome && chrome.benchmarking && ' | 197 self.ExecuteJavascript('if(window.chrome && chrome.benchmarking && ' |
| 198 'chrome.benchmarking.clearCache){chrome.benchmarking.clearCache(); ' | 198 'chrome.benchmarking.clearCache){chrome.benchmarking.clearCache(); ' |
| 199 'chrome.benchmarking.clearPredictorCache();chrome.benchmarking.' | 199 'chrome.benchmarking.clearPredictorCache();chrome.benchmarking.' |
| 200 'clearHostResolverCache();}') | 200 'clearHostResolverCache();}') |
| 201 | 201 |
| 202 def SetURL(self, url): | 202 def LoadURL(self, url, timeout=30): |
| 203 """Sets the URL that the browser will navigate to during the test. | |
| 204 | |
| 205 Args: | |
| 206 url: The string URL to navigate to | |
| 207 """ | |
| 208 self._url = url | |
| 209 | |
| 210 def LoadPage(self, timeout=30): | |
| 211 """Starts Chromium with any arguments previously given and navigates to the | 203 """Starts Chromium with any arguments previously given and navigates to the |
| 212 given URL. | 204 given URL. |
| 213 | 205 |
| 214 Args: | 206 Args: |
| 215 timeout: Page load timeout in seconds. | 207 url: The URL to navigate to. |
| 208 timeout: The time in seconds to load the page before timing out. |
| 216 """ | 209 """ |
| 210 self._url = url |
| 217 if not self._driver: | 211 if not self._driver: |
| 218 self._StartDriver() | 212 self._StartDriver() |
| 219 self._driver.set_page_load_timeout(timeout) | 213 self._driver.set_page_load_timeout(timeout) |
| 220 self._driver.get(self._url) | 214 self._driver.get(self._url) |
| 221 | 215 |
| 222 def ExecuteJavascript(self, script, timeout=30): | 216 def ExecuteJavascript(self, script, timeout=30): |
| 223 """Executes the given javascript in the browser's current page in an | 217 """Executes the given javascript in the browser's current page in an |
| 224 anonymous function. | 218 anonymous function. |
| 225 | 219 |
| 226 If you expect a result and don't get one, try adding a return statement or | 220 If you expect a result and don't get one, try adding a return statement or |
| (...skipping 26 matching lines...) Expand all Loading... |
| 253 Returns: | 247 Returns: |
| 254 A string of the verbatim output from the Javascript execution. | 248 A string of the verbatim output from the Javascript execution. |
| 255 """ | 249 """ |
| 256 return self.ExecuteJavascript("return " + script, timeout) | 250 return self.ExecuteJavascript("return " + script, timeout) |
| 257 | 251 |
| 258 def GetHistogram(self, histogram): | 252 def GetHistogram(self, histogram): |
| 259 js_query = 'statsCollectionController.getBrowserHistogram("%s")' % histogram | 253 js_query = 'statsCollectionController.getBrowserHistogram("%s")' % histogram |
| 260 string_response = self.ExecuteJavascriptStatement(js_query) | 254 string_response = self.ExecuteJavascriptStatement(js_query) |
| 261 return json.loads(string_response) | 255 return json.loads(string_response) |
| 262 | 256 |
| 257 def WaitForJavascriptExpression(self, expression, timeout, min_poll=0.1, |
| 258 max_poll=1): |
| 259 """Waits for the given Javascript expression to evaluate to True within the |
| 260 given timeout. This method polls the Javascript expression within the range |
| 261 of |min_poll| and |max_poll|. |
| 262 |
| 263 Args: |
| 264 expression: The Javascript expression to poll, as a string. |
| 265 min_poll: The most frequently to poll as a float. |
| 266 max_poll: The least frequently to poll as a float. |
| 267 Returns: The result of the expression. |
| 268 """ |
| 269 poll_interval = max(min(max_poll, float(timeout) / 10.0), min_poll) |
| 270 result = self.ExecuteJavascriptStatement(expression) |
| 271 total_waited_time = 0 |
| 272 while not result and total_waited_time < timeout: |
| 273 time.sleep(poll_interval) |
| 274 total_waited_time += poll_interval |
| 275 result = self.ExecuteJavascriptStatement(expression) |
| 276 if not result: |
| 277 raise Exception('%s not true after %f seconds' % (expression, timeout)) |
| 278 return result |
| 279 |
| 263 def GetPerformanceLogs(self, method_filter=r'Network\.responseReceived'): | 280 def GetPerformanceLogs(self, method_filter=r'Network\.responseReceived'): |
| 264 """Returns all logged Performance events from Chrome. | 281 """Returns all logged Performance events from Chrome. |
| 265 | 282 |
| 266 Args: | 283 Args: |
| 267 method_filter: A regex expression to match the method of logged events | 284 method_filter: A regex expression to match the method of logged events |
| 268 against. Only logs who's method matches the regex will be returned. | 285 against. Only logs who's method matches the regex will be returned. |
| 269 Returns: | 286 Returns: |
| 270 Performance logs as a list of dicts, since the last time this function was | 287 Performance logs as a list of dicts, since the last time this function was |
| 271 called. | 288 called. |
| 272 """ | 289 """ |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 @property | 378 @property |
| 362 def request_type(self): | 379 def request_type(self): |
| 363 return self._request_type | 380 return self._request_type |
| 364 | 381 |
| 365 def ResponseHasViaHeader(self): | 382 def ResponseHasViaHeader(self): |
| 366 return 'via' in self._response_headers and (self._response_headers['via'] == | 383 return 'via' in self._response_headers and (self._response_headers['via'] == |
| 367 self._flags.via_header_value) | 384 self._flags.via_header_value) |
| 368 | 385 |
| 369 def WasXHR(self): | 386 def WasXHR(self): |
| 370 return self.request_type == 'XHR' | 387 return self.request_type == 'XHR' |
| OLD | NEW |