Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 from telemetry import inspector_backend | 5 from telemetry import tab_backend |
| 6 from telemetry import inspector_console | |
| 7 from telemetry import inspector_page | |
| 8 from telemetry import inspector_runtime | |
| 9 from telemetry import inspector_timeline | |
| 10 from telemetry import util | |
| 11 | 6 |
| 12 DEFAULT_TAB_TIMEOUT = 60 | 7 DEFAULT_TAB_TIMEOUT = 60 |
| 13 | 8 |
| 14 class Tab(object): | 9 class Tab(object): |
| 15 """Represents a tab in the browser | 10 """Represents a tab in the browser |
| 16 | 11 |
| 17 The important parts of the Tab object are in the runtime and page objects. | 12 The important parts of the Tab object are in the runtime and page objects. |
| 18 E.g.: | 13 E.g.: |
| 19 # Navigates the tab to a given url. | 14 # Navigates the tab to a given url. |
| 20 tab.page.Navigate('http://www.google.com/') | 15 tab.page.Navigate('http://www.google.com/') |
| 21 | 16 |
| 22 # Evaluates 1+1 in the tab's javascript context. | 17 # Evaluates 1+1 in the tab's javascript context. |
| 23 tab.runtime.Evaluate('1+1') | 18 tab.runtime.Evaluate('1+1') |
| 24 """ | 19 """ |
| 25 def __init__(self, browser, tab_controller, debugger_url): | 20 def __init__(self, browser, tab_controller, debugger_url): |
| 26 self._browser = browser | 21 self._backend = tab_backend.TabBackend( |
|
nduca
2013/01/09 01:02:30
Is there a way to pass in the tab_backend on the c
dtu
2013/01/09 01:21:27
Done.
| |
| 27 self._tab_controller = tab_controller | 22 browser, tab_controller, debugger_url) |
| 28 self._debugger_url = debugger_url | |
| 29 | |
| 30 self._inspector_backend = None | |
| 31 self._console = None | |
| 32 self._page = None | |
| 33 self._runtime = None | |
| 34 self._timeline = None | |
| 35 | 23 |
| 36 def __del__(self): | 24 def __del__(self): |
| 37 self.Disconnect() | 25 self.Close() |
| 38 | 26 |
| 39 def _Connect(self): | 27 @property |
| 40 if self._inspector_backend: | 28 def browser(self): |
| 41 return | 29 """The browser in which this tab resides.""" |
| 30 return self._backend.browser | |
| 42 | 31 |
| 43 self._inspector_backend = inspector_backend.InspectorBackend( | 32 @property |
| 44 self._tab_controller, self._debugger_url) | 33 def url(self): |
| 45 self._console = inspector_console.InspectorConsole( | 34 return self._backend.url |
| 46 self._inspector_backend, self) | |
| 47 self._page = inspector_page.InspectorPage(self._inspector_backend, self) | |
| 48 self._runtime = inspector_runtime.InspectorRuntime( | |
| 49 self._inspector_backend, self) | |
| 50 self._timeline = inspector_timeline.InspectorTimeline( | |
| 51 self._inspector_backend, self) | |
| 52 | |
| 53 def Disconnect(self): | |
| 54 """Closes the connection to this tab.""" | |
| 55 self._console = None | |
| 56 self._page = None | |
| 57 self._runtime = None | |
| 58 self._timeline = None | |
| 59 if self._inspector_backend: | |
| 60 self._inspector_backend.Close() | |
| 61 self._inspector_backend = None | |
| 62 self._browser = None | |
| 63 | |
| 64 def Close(self): | |
| 65 """Closes this tab. | |
| 66 | |
| 67 Not all browsers or browser versions support this method. | |
| 68 Be sure to check browser.supports_tab_control.""" | |
| 69 self.Disconnect() | |
| 70 self._tab_controller.CloseTab(self._debugger_url) | |
| 71 | 35 |
| 72 def Activate(self): | 36 def Activate(self): |
| 73 """Brings this tab to the foreground asynchronously. | 37 """Brings this tab to the foreground asynchronously. |
| 74 | 38 |
| 75 Please note: this is asynchronous. There is a delay between this call | 39 Please note: this is asynchronous. There is a delay between this call |
| 76 and the page's documentVisibilityState becoming 'visible', and yet more | 40 and the page's documentVisibilityState becoming 'visible', and yet more |
| 77 delay until the actual tab is visible to the user. None of these delays | 41 delay until the actual tab is visible to the user. None of these delays |
| 78 are included in this call.""" | 42 are included in this call.""" |
| 79 self._Connect() | 43 self._backend.Activate() |
| 80 self._tab_controller.ActivateTab(self._debugger_url) | |
| 81 | 44 |
| 82 @property | 45 def Close(self): |
| 83 def browser(self): | 46 """Closes this tab. |
| 84 """The browser in which this tab resides.""" | |
| 85 return self._browser | |
| 86 | 47 |
| 87 @property | 48 Not all browsers or browser versions support this method. |
| 88 def url(self): | 49 Be sure to check browser.supports_tab_control.""" |
| 89 return self._tab_controller.GetTabUrl(self._debugger_url) | 50 self._backend.Close() |
| 90 | |
| 91 @property | |
| 92 def console(self): | |
| 93 """Methods for interacting with the page's console object.""" | |
| 94 self._Connect() | |
| 95 return self._console | |
| 96 | |
| 97 @property | |
| 98 def page(self): | |
| 99 """Methods for interacting with the current page.""" | |
| 100 self._Connect() | |
| 101 return self._page | |
| 102 | |
| 103 @property | |
| 104 def runtime(self): | |
| 105 """Methods for interacting with the page's javascript runtime.""" | |
| 106 self._Connect() | |
| 107 return self._runtime | |
| 108 | |
| 109 @property | |
| 110 def timeline(self): | |
| 111 """Methods for interacting with the inspector timeline.""" | |
| 112 self._Connect() | |
| 113 return self._timeline | |
| 114 | 51 |
| 115 def WaitForDocumentReadyStateToBeComplete(self, timeout=DEFAULT_TAB_TIMEOUT): | 52 def WaitForDocumentReadyStateToBeComplete(self, timeout=DEFAULT_TAB_TIMEOUT): |
| 116 util.WaitFor( | 53 self._backend.WaitForDocumentReadyStateToBeComplete(timeout) |
| 117 lambda: self._runtime.Evaluate('document.readyState') == 'complete', | |
| 118 timeout) | |
| 119 | 54 |
| 120 def WaitForDocumentReadyStateToBeInteractiveOrBetter( | 55 def WaitForDocumentReadyStateToBeInteractiveOrBetter( |
| 121 self, timeout=DEFAULT_TAB_TIMEOUT): | 56 self, timeout=DEFAULT_TAB_TIMEOUT): |
| 122 def IsReadyStateInteractiveOrBetter(): | 57 self._backend.WaitForDocumentReadyStateToBeInteractiveOrBetter(timeout) |
| 123 rs = self._runtime.Evaluate('document.readyState') | |
| 124 return rs == 'complete' or rs == 'interactive' | |
| 125 util.WaitFor(IsReadyStateInteractiveOrBetter, timeout) | |
| 126 | 58 |
| 59 @property | |
| 60 def screenshot_supported(self): | |
| 61 """True if the browser instance is capable of capturing screenshots""" | |
| 62 return self._backend.screenshot_supported | |
| 63 | |
| 64 def Screenshot(self, timeout=DEFAULT_TAB_TIMEOUT): | |
| 65 """Capture a screenshot of the window for rendering validation""" | |
| 66 return self._backend.Screenshot(timeout) | |
| 67 | |
| 68 @property | |
| 69 def message_output_stream(self): | |
| 70 return self._backend.message_output_stream | |
| 71 | |
| 72 @message_output_stream.setter | |
| 73 def message_output_stream(self, stream): | |
| 74 self._backend.message_output_stream = stream | |
| 75 | |
| 76 def PerformActionAndWaitForNavigate( | |
| 77 self, action_function, timeout=DEFAULT_TAB_TIMEOUT): | |
| 78 """Executes action_function, and waits for the navigation to complete. | |
|
nduca
2013/01/09 01:02:30
what is the type of action_function? Lets make sur
dtu
2013/01/09 01:21:27
Done.
| |
| 79 | |
| 80 action_function is expect to result in a navigation. This function returns | |
| 81 when the navigation is complete or when the timeout has been exceeded. | |
| 82 """ | |
| 83 self._backend.PerformActionAndWaitForNavigate(action_function, timeout) | |
| 84 | |
| 85 def Navigate(self, url, timeout=DEFAULT_TAB_TIMEOUT): | |
| 86 """Navigates to url.""" | |
| 87 self._backend.Navigate(url, timeout) | |
| 88 | |
| 89 def GetCookieByName(self, name, timeout=DEFAULT_TAB_TIMEOUT): | |
| 90 """Returns the value of the cookie by the given |name|.""" | |
| 91 return self._backend.GetCookieByName(name, timeout) | |
| 92 | |
| 93 def Execute(self, expr, timeout=DEFAULT_TAB_TIMEOUT): | |
|
nduca
2013/01/09 01:02:30
ExecuteJavascript
dtu
2013/01/09 01:21:27
Done.
| |
| 94 """Executes expr in javascript. Does not return the result. | |
| 95 | |
| 96 If the expression failed to evaluate, EvaluateException will be raised. | |
| 97 """ | |
| 98 self._backend.Execute(expr, timeout) | |
| 99 | |
| 100 def Evaluate(self, expr, timeout=DEFAULT_TAB_TIMEOUT): | |
|
nduca
2013/01/09 01:02:30
EvaluateJavascript
dtu
2013/01/09 01:21:27
Done.
| |
| 101 """Evalutes expr in javascript and returns the JSONized result. | |
| 102 | |
| 103 Consider using Execute for cases where the result of the expression is not | |
| 104 needed. | |
| 105 | |
| 106 If evaluation throws in javascript, a python EvaluateException will | |
| 107 be raised. | |
| 108 | |
| 109 If the result of the evaluation cannot be JSONized, then an | |
| 110 EvaluationException will be raised. | |
| 111 """ | |
| 112 return self._backend.Evaluate(expr, timeout) | |
| 113 | |
| 114 @property | |
| 115 def timeline_events(self): | |
| 116 return self._backend.timeline_events | |
| 117 | |
| 118 def StartRecording(self): | |
|
nduca
2013/01/09 01:02:30
StartRecording what?
dtu
2013/01/09 01:21:27
StartTimelineRecording? StartEventRecording? I don
| |
| 119 self._backend.StartRecording() | |
| 120 | |
| 121 def StopRecording(self): | |
| 122 self._backend.StopRecording() | |
| OLD | NEW |