- add sources.
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / tab.py
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 from telemetry.core import web_contents
5
6 DEFAULT_TAB_TIMEOUT = 60
7
8 class Tab(web_contents.WebContents):
9   """Represents a tab in the browser
10
11   The important parts of the Tab object are in the runtime and page objects.
12   E.g.:
13       # Navigates the tab to a given url.
14       tab.Navigate('http://www.google.com/')
15
16       # Evaluates 1+1 in the tab's JavaScript context.
17       tab.Evaluate('1+1')
18   """
19   def __init__(self, inspector_backend):
20     super(Tab, self).__init__(inspector_backend)
21
22   def __del__(self):
23     super(Tab, self).__del__()
24
25   @property
26   def browser(self):
27     """The browser in which this tab resides."""
28     return self._inspector_backend.browser
29
30   @property
31   def url(self):
32     return self._inspector_backend.url
33
34   @property
35   def dom_stats(self):
36     """A dictionary populated with measured DOM statistics.
37
38     Currently this dictionary contains:
39     {
40       'document_count': integer,
41       'node_count': integer,
42       'event_listener_count': integer
43     }
44     """
45     dom_counters = self._inspector_backend.GetDOMStats(
46         timeout=DEFAULT_TAB_TIMEOUT)
47     assert (len(dom_counters) == 3 and
48             all([x in dom_counters for x in ['document_count', 'node_count',
49                                              'event_listener_count']]))
50     return dom_counters
51
52
53   def Activate(self):
54     """Brings this tab to the foreground asynchronously.
55
56     Not all browsers or browser versions support this method.
57     Be sure to check browser.supports_tab_control.
58
59     Please note: this is asynchronous. There is a delay between this call
60     and the page's documentVisibilityState becoming 'visible', and yet more
61     delay until the actual tab is visible to the user. None of these delays
62     are included in this call."""
63     self._inspector_backend.Activate()
64
65   @property
66   def screenshot_supported(self):
67     """True if the browser instance is capable of capturing screenshots"""
68     return self._inspector_backend.screenshot_supported
69
70   def Screenshot(self, timeout=DEFAULT_TAB_TIMEOUT):
71     """Capture a screenshot of the window for rendering validation"""
72     return self._inspector_backend.Screenshot(timeout)
73
74   def PerformActionAndWaitForNavigate(
75       self, action_function, timeout=DEFAULT_TAB_TIMEOUT):
76     """Executes action_function, and waits for the navigation to complete.
77
78     action_function must be a Python function that results in a navigation.
79     This function returns when the navigation is complete or when
80     the timeout has been exceeded.
81     """
82     self._inspector_backend.PerformActionAndWaitForNavigate(
83         action_function, timeout)
84
85   def Navigate(self, url, script_to_evaluate_on_commit=None,
86                timeout=DEFAULT_TAB_TIMEOUT):
87     """Navigates to url.
88
89     If |script_to_evaluate_on_commit| is given, the script source string will be
90     evaluated when the navigation is committed. This is after the context of
91     the page exists, but before any script on the page itself has executed.
92     """
93     self._inspector_backend.Navigate(url, script_to_evaluate_on_commit, timeout)
94
95   def GetCookieByName(self, name, timeout=DEFAULT_TAB_TIMEOUT):
96     """Returns the value of the cookie by the given |name|."""
97     return self._inspector_backend.GetCookieByName(name, timeout)
98
99   def CollectGarbage(self):
100     self._inspector_backend.CollectGarbage()
101
102   def ClearCache(self):
103     """Clears the browser's HTTP disk cache and the tab's HTTP memory cache."""
104     self._inspector_backend.ClearCache()