| 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 # Marks the navigation as complete and unblocks the | |
| 24 # PerformActionAndWaitForNavigate call. | |
| 25 self._navigation_pending = False | |
| 26 | |
| 27 def _OnClose(self): | |
| 28 pass | |
| 29 | |
| 30 def PerformActionAndWaitForNavigate(self, action_function, timeout=60): | |
| 31 """Executes action_function, and waits for the navigation to complete. | |
| 32 | |
| 33 action_function is expect to result in a navigation. This function returns | |
| 34 when the navigation is complete or when the timeout has been exceeded. | |
| 35 """ | |
| 36 | |
| 37 # Turn on notifications. We need them to get the Page.frameNavigated event. | |
| 38 request = { | |
| 39 'method': 'Page.enable' | |
| 40 } | |
| 41 res = self._inspector_backend.SyncRequest(request, timeout) | |
| 42 assert len(res['result'].keys()) == 0 | |
| 43 | |
| 44 def DisablePageNotifications(): | |
| 45 request = { | |
| 46 'method': 'Page.disable' | |
| 47 } | |
| 48 res = self._inspector_backend.SyncRequest(request, timeout) | |
| 49 assert len(res['result'].keys()) == 0 | |
| 50 | |
| 51 self._navigation_pending = True | |
| 52 try: | |
| 53 action_function() | |
| 54 except: | |
| 55 DisablePageNotifications() | |
| 56 raise | |
| 57 | |
| 58 def IsNavigationDone(time_left): | |
| 59 self._inspector_backend.DispatchNotifications(time_left) | |
| 60 return not self._navigation_pending | |
| 61 util.WaitFor(IsNavigationDone, timeout, pass_time_left_to_func=True) | |
| 62 | |
| 63 DisablePageNotifications() | |
| 64 | |
| 65 def Navigate(self, url, timeout=60): | |
| 66 """Navigates to url""" | |
| 67 | |
| 68 def DoNavigate(): | |
| 69 # Navigate the page. However, there seems to be a bug in chrome devtools | |
| 70 # protocol where the request id for this event gets held on the browser | |
| 71 # side pretty much indefinitely. | |
| 72 # | |
| 73 # So, instead of waiting for the event to actually complete, wait for the | |
| 74 # Page.frameNavigated event. | |
| 75 request = { | |
| 76 'method': 'Page.navigate', | |
| 77 'params': { | |
| 78 'url': url, | |
| 79 } | |
| 80 } | |
| 81 self._inspector_backend.SendAndIgnoreResponse(request) | |
| 82 | |
| 83 self.PerformActionAndWaitForNavigate(DoNavigate, timeout) | |
| 84 | |
| 85 def GetCookieByName(self, name, timeout=60): | |
| 86 """Returns the value of the cookie by the given |name|.""" | |
| 87 request = { | |
| 88 'method': 'Page.getCookies' | |
| 89 } | |
| 90 res = self._inspector_backend.SyncRequest(request, timeout) | |
| 91 cookies = res['result']['cookies'] | |
| 92 for cookie in cookies: | |
| 93 if cookie['name'] == name: | |
| 94 return cookie['value'] | |
| 95 return None | |
| OLD | NEW |