OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2011 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 | |
5 import unittest | |
6 | |
7 import pyauto_tracing | |
8 import pyauto | |
9 import tab_tracker | |
10 | |
11 from timeline_model import TimelineModel | |
Nirnimesh
2012/07/30 21:03:51
This need not be in a separate section. Move to li
| |
12 | |
13 import os | |
14 | |
15 class Tracer: | |
16 """The tracer class allows you to start and stop chrome and system tracing, | |
17 and get an instance of a TimelineModel which represents the results from a | |
18 trace. | |
19 """ | |
20 | |
21 def __init__(self, browser, tab_tracker): | |
22 """ | |
23 Args: | |
24 browser: an instance of a PyUITest | |
25 tab_tracker: an instance of a TabTracker | |
26 """ | |
27 self._browser = browser | |
28 self._tab_tracker = tab_tracker | |
29 self._tab_uuid = tab_tracker.CreateTab('chrome://tracing') | |
30 | |
31 # I'm not happy with importing timeline_model_shim.js | |
Nirnimesh
2012/07/30 21:03:51
Add a TODO?
| |
32 # here. I'd rather pull it in from within TimelineModelProxy. | |
33 # tracing_test.js depends on timeline_model_shim.js however. | |
34 files = ["timeline_model_shim.js", "tracer.js"] | |
35 for fileName in files: | |
36 fileJs, fd = (None, None) | |
37 try: | |
38 fd = open(os.path.join(os.path.dirname(__file__), fileName), "r") | |
39 fileJs = fd.read() | |
40 finally: | |
41 if fd: | |
42 fd.close() | |
43 self._ExecuteJavascript(fileJs); | |
44 | |
45 def __del__(self): | |
46 self._tab_tracker.ReleaseTab(self._tab_uuid) | |
47 | |
48 def _ExecuteJavascript(self, js): | |
49 return self._browser.ExecuteJavascript( | |
50 js = js, | |
51 windex = self._tab_tracker.GetWindowIndex(), | |
52 tab_index = self._tab_tracker.GetTabIndex(self._tab_uuid) | |
53 ) | |
54 | |
55 def BeginTracing(self, system_tracing=True): | |
56 """Start tracing | |
57 | |
58 Args: | |
59 system_tracing: whether or not to gather system traces along with chrome | |
60 traces. | |
61 """ | |
62 self._ExecuteJavascript( | |
63 """ | |
64 window.domAutomationController.send( | |
65 window.pyautoRecordTrace(%s) | |
66 ); | |
67 """ % ( | |
68 "true" if system_tracing else "false" | |
69 ) | |
70 ) | |
71 | |
72 def EndTracing(self): | |
73 """End tracing and return a TimelineModel | |
74 | |
75 Returns: | |
76 an instance of a TimelineModel which contains the results of the trace | |
77 """ | |
78 return TimelineModel( | |
79 js_executor = self._ExecuteJavascript.__get__(self, Tracer), | |
80 shim_id = self._ExecuteJavascript( | |
81 """tracingController.endTracing();""" | |
82 ) | |
83 ) | |
84 | |
85 class TracerFactory: | |
86 """A TracerFactory is used to produce a Tracer | |
87 | |
88 It's recommended to use the same TracerFactory to produce all Tracers so that | |
89 the same TabTracker can be used throughout | |
90 | |
91 Args: | |
92 browser: an instance of a PyUITest""" | |
Nirnimesh
2012/07/30 21:03:51
move """ to next line
| |
93 def __init__(self, browser): | |
94 self._tab_tracker = tab_tracker.TabTracker(browser) | |
95 self._browser = browser | |
96 | |
97 def Produce(self): | |
98 """Produce an instance of a Tracer""" | |
99 return Tracer(self._browser, self._tab_tracker) | |
OLD | NEW |