OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """The base test class for GTalk tests. | |
7 | |
8 This module contains a set of common utilities used by unittest for querying | |
9 and manipulating the Google Talk Chrome Extension (http//go/quasar) | |
10 """ | |
11 | |
12 import logging | |
13 import re | |
14 import os | |
15 | |
16 import pyauto_gtalk # must preceed pyauto | |
17 import pyauto | |
18 import pyauto_errors | |
19 | |
20 | |
21 class GTalkBaseTest(pyauto.PyUITest): | |
22 """Base test class for testing GTalk.""" | |
23 | |
24 _injected_js = None | |
25 | |
26 def InstallGTalkExtension(self): | |
27 """Download and install the GTalk extension.""" | |
28 self.UninstallGTalkExtension() | |
29 | |
30 extension_path = os.path.abspath( | |
31 os.path.join(self.DataDir(), 'extensions', 'gtalk', 'gtalk.crx')) | |
32 self.assertTrue(os.path.exists(extension_path), | |
33 msg='Failed to find GTalk extension: ' + extension_path) | |
34 | |
35 self.InstallExtension(extension_path, False) | |
36 extension = self.GetGTalkExtensionInfo() | |
37 self.assertTrue(extension, msg='Failed to install GTalk extension') | |
38 self.assertTrue(extension['is_enabled'], msg='GTalk extension is disabled') | |
39 | |
40 def UninstallGTalkExtension(self): | |
41 """Uninstall the GTalk extension (if present)""" | |
42 extension = self.GetGTalkExtensionInfo() | |
43 if extension: | |
44 self.UninstallExtensionById(extension['id']) | |
45 | |
46 def GetGTalkExtensionInfo(self): | |
47 """Get the data object about the GTalk extension.""" | |
48 extensions = [x for x in self.GetExtensionsInfo() | |
49 if x['name'] == 'Google Talk'] | |
50 return extensions[0] if len(extensions) == 1 else None | |
51 | |
52 def RunInMole(self, js, mole_index=0): | |
53 """Execute javascript in a chat mole. | |
54 | |
55 Args: | |
56 js: The javascript to run. | |
57 mole_index: The index of the mole in which to run the JS. | |
58 | |
59 Returns: | |
60 The resulting value from executing the javascript. | |
61 """ | |
62 return self._RunInTab(self.GetMoleInfo(mole_index), js) | |
63 | |
64 def RunInRoster(self, js): | |
65 """Execute javascript in the chat roster. | |
66 | |
67 Args: | |
68 js: The javascript to run. | |
69 | |
70 Returns: | |
71 The resulting value from executing the javascript. | |
72 """ | |
73 return self._RunInTab(self.GetViewerInfo(), js, '//iframe[1]\n//iframe[1]') | |
74 | |
75 def RunInLoginPage(self, js, xpath=''): | |
76 """Execute javascript in the gaia login popup. | |
77 | |
78 Args: | |
79 js: The javascript to run. | |
80 xpath: The xpath to the frame in which to execute the javascript. | |
81 | |
82 Returns: | |
83 The resulting value from executing the javascript. | |
84 """ | |
85 return self._RunInTab(self.GetLoginPageInfo(), js, xpath) | |
86 | |
87 def RunInViewer(self, js, xpath=''): | |
88 """Execute javascript in the GTalk viewer window. | |
89 | |
90 Args: | |
91 js: The javascript to run. | |
92 xpath: The xpath to the frame in which to execute the javascript. | |
93 | |
94 Returns: | |
95 The resulting value from executing the javascript. | |
96 """ | |
97 return self._RunInTab(self.GetViewerInfo(), js, xpath) | |
98 | |
99 def RunInBackground(self, js, xpath=''): | |
100 """Execute javascript in the GTalk viewer window. | |
101 | |
102 Args: | |
103 js: The javascript to run. | |
104 xpath: The xpath to the frame in which to execute the javascript. | |
105 | |
106 Returns: | |
107 The resulting value from executing the javascript. | |
108 """ | |
109 background_view = self.GetBackgroundInfo() | |
110 value = self.ExecuteJavascriptInRenderView( | |
111 self._WrapJs(js), background_view['view']) | |
112 self._LogRun(js, value) | |
113 return value | |
114 | |
115 def GetMoleInfo(self, mole_index=0): | |
116 """Get the data object about a given chat mole. | |
117 | |
118 Args: | |
119 mole_index: The index of the mole to retrieve. | |
120 | |
121 Returns: | |
122 Data object describing mole. | |
123 """ | |
124 return self._GetTabInfo('/pmole?', mole_index) | |
125 | |
126 def GetViewerInfo(self): | |
127 """Get the data object about the GTalk viewer dialog.""" | |
128 extension = self.GetGTalkExtensionInfo() | |
129 return self._GetTabInfo( | |
130 'chrome-extension://' + extension['id'] + '/viewer.html') | |
131 | |
132 def GetLoginPageInfo(self): | |
133 """Get the data object about the gaia login popup.""" | |
134 return self._GetTabInfo('https://accounts.google.com/ServiceLogin?') | |
135 | |
136 def GetBackgroundInfo(self): | |
137 """Get the data object about the GTalk background page.""" | |
138 extension_views = self.GetBrowserInfo()['extension_views'] | |
139 for extension_view in extension_views: | |
140 if 'Google Talk' in extension_view['name'] and \ | |
141 'EXTENSION_BACKGROUND_PAGE' == extension_view['view_type']: | |
142 return extension_view | |
143 return None | |
144 | |
145 def WaitUntilResult(self, result, func, msg): | |
146 """Loop func until a condition matches is satified. | |
147 | |
148 Args: | |
149 result: Value of func() at which to stop. | |
150 func: Function to run at each iteration. | |
151 msg: Error to print upon timing out. | |
152 """ | |
153 assert callable(func) | |
154 self.assertTrue(self.WaitUntil( | |
155 lambda: func(), expect_retval=result), msg=msg) | |
156 | |
157 def WaitUntilCondition(self, func, matches, msg): | |
158 """Loop func until condition matches is satified. | |
159 | |
160 Args: | |
161 func: Function to run at each iteration. | |
162 matches: Funtion to evalute output and determine whether to stop. | |
163 msg: Error to print upon timing out. | |
164 """ | |
165 assert callable(func) | |
166 assert callable(matches) | |
167 self.assertTrue(self.WaitUntil( | |
168 lambda: matches(func())), msg=msg) | |
169 | |
170 def _WrapJs(self, statement): | |
171 """Wrap the javascript to be executed. | |
172 | |
173 Args: | |
174 statement: The piece of javascript to wrap. | |
175 | |
176 Returns: | |
177 The wrapped javascript. | |
178 """ | |
179 return """ | |
180 window.domAutomationController.send( | |
181 (function(){ | |
182 %s | |
183 try{return %s} | |
184 catch(e){return "JS_ERROR: " + e}})()) | |
185 """ % (self._GetInjectedJs(), statement) | |
186 | |
187 def _RunInTab(self, tab, js, xpath=''): | |
188 """Execute javascript in a given tab. | |
189 | |
190 Args: | |
191 tab: The data object for the Chrome window tab returned by | |
192 _GetTabInfo. | |
193 js: The javascript to run. | |
194 xpath: The xpath to the frame in which to execute the javascript. | |
195 | |
196 Returns: | |
197 The resulting value from executing the javascript. | |
198 """ | |
199 if not tab: | |
200 logging.debug('Tab not found: %s' % tab) | |
201 return False | |
202 logging.info('Run in tab: %s' % js) | |
203 | |
204 value = self.ExecuteJavascriptInTab(self._WrapJs(js), | |
205 windex = tab['windex'], | |
206 frame_xpath = xpath) | |
207 self._LogRun(js, value) | |
208 return value | |
209 | |
210 def _LogRun(self, js, value): | |
211 """Log a particular run. | |
212 | |
213 Args: | |
214 js: The javascript statement executed. | |
215 value: The return value for the execution. | |
216 """ | |
217 # works around UnicodeEncodeError: 'ascii' codec can't encode... | |
218 out = value | |
219 if not isinstance(value, basestring): | |
220 out = str(value) | |
221 out = re.sub('\s', ';', out[:300]) | |
222 logging.info(js + ' ===> ' + out.encode('utf-8')) | |
223 | |
224 def _GetTabInfo(self, url_query, index=0): | |
225 """Get the data object for a given tab. | |
226 | |
227 Args: | |
228 url_query: The substring of the URL to search for. | |
229 index: The index within the list of matches to return. | |
230 | |
231 Returns: | |
232 The data object for the tab. | |
233 """ | |
234 windows = self.GetBrowserInfo()['windows'] | |
235 i = 0 | |
236 for win in windows: | |
237 for tab in win['tabs']: | |
238 if tab['url'] and url_query in tab['url']: | |
239 # Store reference to windex used in _RunInTab. | |
240 tab['windex'] = win['index'] | |
241 if i == index: | |
242 return tab | |
243 i = i + 1 | |
244 return None | |
245 | |
246 def _GetInjectedJs(self): | |
247 """Get the javascript to inject in the execution environment.""" | |
248 if self._injected_js is None: | |
249 self._injected_js = open(os.path.dirname(__file__) + '/jsutils.js').read() | |
250 return self._injected_js | |
251 | |
252 def action_max_timeout_ms(self): | |
253 """Get the default timeout for each WaitUntil command.""" | |
254 return 30000; | |
Nirnimesh
2011/12/13 21:06:40
nit: remove ;
wud
2011/12/13 21:22:01
Done.
| |
OLD | NEW |