| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 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 import json | |
| 5 import logging | |
| 6 | |
| 7 from telemetry.core import util | |
| 8 | |
| 9 class InspectorPage(object): | |
| 10 def __init__(self, inspector_backend): | |
| 11 self._inspector_backend = inspector_backend | |
| 12 self._inspector_backend.RegisterDomain( | |
| 13 'Page', | |
| 14 self._OnNotification, | |
| 15 self._OnClose) | |
| 16 self._navigation_pending = False | |
| 17 | |
| 18 def _OnNotification(self, msg): | |
| 19 logging.debug('Notification: %s', json.dumps(msg, indent=2)) | |
| 20 if msg['method'] == 'Page.frameNavigated' and self._navigation_pending: | |
| 21 url = msg['params']['frame']['url'] | |
| 22 if (not url == 'chrome://newtab/' and not url == 'about:blank' | |
| 23 and not 'parentId' in msg['params']['frame']): | |
| 24 # Marks the navigation as complete and unblocks the | |
| 25 # PerformActionAndWaitForNavigate call. | |
| 26 self._navigation_pending = False | |
| 27 | |
| 28 def _OnClose(self): | |
| 29 pass | |
| 30 | |
| 31 def PerformActionAndWaitForNavigate(self, action_function, timeout=60): | |
| 32 """Executes action_function, and waits for the navigation to complete. | |
| 33 | |
| 34 action_function is expect to result in a navigation. This function returns | |
| 35 when the navigation is complete or when the timeout has been exceeded. | |
| 36 """ | |
| 37 | |
| 38 # Turn on notifications. We need them to get the Page.frameNavigated event. | |
| 39 request = { | |
| 40 'method': 'Page.enable' | |
| 41 } | |
| 42 res = self._inspector_backend.SyncRequest(request, timeout) | |
| 43 assert len(res['result'].keys()) == 0 | |
| 44 | |
| 45 def DisablePageNotifications(): | |
| 46 request = { | |
| 47 'method': 'Page.disable' | |
| 48 } | |
| 49 res = self._inspector_backend.SyncRequest(request, timeout) | |
| 50 assert len(res['result'].keys()) == 0 | |
| 51 | |
| 52 self._navigation_pending = True | |
| 53 try: | |
| 54 action_function() | |
| 55 except: | |
| 56 DisablePageNotifications() | |
| 57 raise | |
| 58 | |
| 59 def IsNavigationDone(time_left): | |
| 60 self._inspector_backend.DispatchNotifications(time_left) | |
| 61 return not self._navigation_pending | |
| 62 util.WaitFor(IsNavigationDone, timeout, pass_time_left_to_func=True) | |
| 63 | |
| 64 DisablePageNotifications() | |
| 65 | |
| 66 def Navigate(self, url, script_to_evaluate_on_commit=None, timeout=60): | |
| 67 """Navigates to |url|. | |
| 68 | |
| 69 If |script_to_evaluate_on_commit| is given, the script source string will be | |
| 70 evaluated when the navigation is committed. This is after the context of | |
| 71 the page exists, but before any script on the page itself has executed. | |
| 72 """ | |
| 73 | |
| 74 def DoNavigate(): | |
| 75 if script_to_evaluate_on_commit: | |
| 76 request = { | |
| 77 'method': 'Page.addScriptToEvaluateOnLoad', | |
| 78 'params': { | |
| 79 'scriptSource': script_to_evaluate_on_commit, | |
| 80 } | |
| 81 } | |
| 82 self._inspector_backend.SyncRequest(request) | |
| 83 # Navigate the page. However, there seems to be a bug in chrome devtools | |
| 84 # protocol where the request id for this event gets held on the browser | |
| 85 # side pretty much indefinitely. | |
| 86 # | |
| 87 # So, instead of waiting for the event to actually complete, wait for the | |
| 88 # Page.frameNavigated event. | |
| 89 request = { | |
| 90 'method': 'Page.navigate', | |
| 91 'params': { | |
| 92 'url': url, | |
| 93 } | |
| 94 } | |
| 95 self._inspector_backend.SendAndIgnoreResponse(request) | |
| 96 self.PerformActionAndWaitForNavigate(DoNavigate, timeout) | |
| 97 | |
| 98 def GetCookieByName(self, name, timeout=60): | |
| 99 """Returns the value of the cookie by the given |name|.""" | |
| 100 request = { | |
| 101 'method': 'Page.getCookies' | |
| 102 } | |
| 103 res = self._inspector_backend.SyncRequest(request, timeout) | |
| 104 cookies = res['result']['cookies'] | |
| 105 for cookie in cookies: | |
| 106 if cookie['name'] == name: | |
| 107 return cookie['value'] | |
| 108 return None | |
| 109 | |
| 110 def CollectGarbage(self, timeout=60): | |
| 111 request = { | |
| 112 'method': 'HeapProfiler.CollectGarbage' | |
| 113 } | |
| 114 self._inspector_backend.SyncRequest(request, timeout) | |
| OLD | NEW |