Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Side by Side Diff: chrome/test/functional/gtalk/gtalk_base_test.py

Issue 8528012: Introduce Pyauto tests for Quasar (Google Talk Extension). (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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;
Nirnimesh 2011/11/30 23:57:36 nit: remove ;
wud 2011/12/13 19:36:24 Done.
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 # Catch any JSONInterfaceError thrown while running.
205 try:
Nirnimesh 2011/11/30 23:57:36 I really think you should fix the javascript code
wud 2011/12/13 19:36:24 Ok, I've removed the "except" and haven't been abl
206 value = self.ExecuteJavascriptInTab(self._WrapJs(js),
207 windex = tab['windex'],
208 frame_xpath = xpath)
209 except pyauto_error.JSONInterfaceError as e:
210 logging.debug('Error running JS: %s' % js)
211 value = False
212 self._LogRun(js, value)
213 return value
214
215 def _LogRun(self, js, value):
216 """Log a particular run.
217
218 Args:
219 js: The javascript statement executed.
220 value: The return value for the execution.
221 """
222 # works around UnicodeEncodeError: 'ascii' codec can't encode...
223 out = value
224 if not isinstance(value, basestring):
225 out = str(value)
226 out = re.sub('\s', ';', out[:300])
227 logging.info(js + ' ===> ' + out.encode('utf-8'))
228
229 def _GetTabInfo(self, url_query, index=0):
230 """Get the data object for a given tab.
231
232 Args:
233 url_query: The substring of the URL to search for.
234 index: The index within the list of matches to return.
235
236 Returns:
237 The data object for the tab.
238 """
239 windows = self.GetBrowserInfo()['windows']
240 i = 0
241 for win in windows:
242 for tab in win['tabs']:
243 if tab['url'] and url_query in tab['url']:
244 # Store reference to windex used in _RunInTab.
245 tab['windex'] = win['index']
246 if i == index:
247 return tab
248 i = i + 1
249 return None
250
251 def _GetInjectedJs(self):
252 """Get the javascript to inject in the execution environment."""
253 if self._injected_js is None:
254 self._injected_js = open(os.path.dirname(__file__) + '/jsutils.js').read()
255 return self._injected_js
256
257 def action_max_timeout_ms(self):
258 """Get the default timeout for each WaitUntil command."""
259 return 30000;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698