OLD | NEW |
1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
2 # Copyright 2009, Google Inc. | 2 # Copyright 2009, Google Inc. |
3 # All rights reserved. | 3 # All rights reserved. |
4 # | 4 # |
5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
7 # met: | 7 # met: |
8 # | 8 # |
9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 54 |
55 | 55 |
56 def IsValidSuffix(name): | 56 def IsValidSuffix(name): |
57 """Returns True if name ends in a valid test type.""" | 57 """Returns True if name ends in a valid test type.""" |
58 name = name.lower() | 58 name = name.lower() |
59 for suffix in SUFFIXES: | 59 for suffix in SUFFIXES: |
60 if name.endswith(suffix): | 60 if name.endswith(suffix): |
61 return True | 61 return True |
62 return False | 62 return False |
63 | 63 |
| 64 def ScreenshotNameFromTestName(name): |
| 65 name = StripTestTypeSuffix(name) |
| 66 |
| 67 if name.startswith("Test"): |
| 68 # Make sure these are in order. |
| 69 prefixes = ["TestStress", "TestSample", "Test"] |
| 70 for prefix in prefixes: |
| 71 if name.startswith(prefix): |
| 72 name = name[len(prefix):] |
| 73 break |
| 74 |
| 75 # Lowercase the name only for custom test methods. |
| 76 name = name.lower() |
| 77 |
| 78 name = name.replace("_", "-") |
| 79 name = name.replace("/", "_") |
| 80 |
| 81 return name |
| 82 |
64 | 83 |
65 def StripTestTypeSuffix(name): | 84 def StripTestTypeSuffix(name): |
66 """Removes the suffix from name if it is a valid test type.""" | 85 """Removes the suffix from name if it is a valid test type.""" |
67 name_lower = name.lower() | 86 name_lower = name.lower() |
68 for suffix in SUFFIXES: | 87 for suffix in SUFFIXES: |
69 if name_lower.endswith(suffix): | 88 if name_lower.endswith(suffix): |
70 return name[:-len(suffix)] | 89 return name[:-len(suffix)] |
71 return name | 90 return name |
72 | 91 |
73 | 92 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 session.wait_for_condition("window.g_selenium_post_render", 20000) | 217 session.wait_for_condition("window.g_selenium_post_render", 20000) |
199 | 218 |
200 # Get result | 219 # Get result |
201 data_url = session.get_eval("window.g_selenium_save_screen_result") | 220 data_url = session.get_eval("window.g_selenium_save_screen_result") |
202 expected_header = "data:image/png;base64," | 221 expected_header = "data:image/png;base64," |
203 if data_url.startswith(expected_header): | 222 if data_url.startswith(expected_header): |
204 png = base64.b64decode(data_url[len(expected_header):]) | 223 png = base64.b64decode(data_url[len(expected_header):]) |
205 file = open(full_path + ".png", 'wb') | 224 file = open(full_path + ".png", 'wb') |
206 file.write(png) | 225 file.write(png) |
207 file.close() | 226 file.close() |
208 print "Saved screenshot %s." % full_path | |
209 return True | 227 return True |
210 | 228 |
211 return False | 229 return False |
212 | 230 |
213 | 231 |
214 class SeleniumTestCase(unittest.TestCase): | 232 class SeleniumTestCase(unittest.TestCase): |
215 """Wrapper for TestCase for selenium.""" | 233 """Wrapper for TestCase for selenium.""" |
216 | 234 |
217 def __init__(self, name, session, browser, path_to_html, test_type=None, | 235 def __init__(self, name, browser, path_to_html, test_type=None, |
218 sample_path=None, options=None): | 236 sample_path=None, options=None): |
219 """Constructor for SampleTests. | 237 """Constructor for SampleTests. |
220 | 238 |
221 Args: | 239 Args: |
222 name: Name of unit test. | 240 name: Name of unit test. |
223 session: Selenium session. | 241 session: Selenium session. |
224 browser: Name of browser. | 242 browser: Name of browser. |
225 path_to_html: path to html from server root | 243 path_to_html: path to html from server root |
226 test_type: Type of test ("small", "medium", "large") | 244 test_type: Type of test ("small", "medium", "large") |
227 sample_path: Path to test. | 245 sample_path: Path to test. |
228 options: list of option strings. | 246 options: list of option strings. |
229 """ | 247 """ |
230 | 248 |
231 unittest.TestCase.__init__(self, name) | 249 unittest.TestCase.__init__(self, name) |
232 self.session = session | 250 self.name = name |
| 251 self.session = None |
233 self.browser = browser | 252 self.browser = browser |
234 self.test_type = test_type | 253 self.test_type = test_type |
235 self.sample_path = sample_path | 254 self.sample_path = sample_path |
236 self.options = options | |
237 self.path_to_html = path_to_html | 255 self.path_to_html = path_to_html |
| 256 self.screenshots = [] |
| 257 self.timeout = 10000 |
| 258 self.client = "g_client" |
| 259 # parse options |
| 260 for option in options: |
| 261 if option.startswith("screenshot"): |
| 262 clock = GetArgument(option) |
| 263 if clock is None: |
| 264 clock = "27.5" |
| 265 self.screenshots.append(clock) |
| 266 elif option.startswith("timeout"): |
| 267 self.timeout = int(GetArgument(option)) |
| 268 elif option.startswith("client"): |
| 269 self.client = GetArgument(option) |
| 270 |
| 271 |
| 272 def SetSession(self, session): |
| 273 self.session = session |
| 274 |
| 275 def GetTestTimeout(self): |
| 276 return self.timeout |
238 | 277 |
239 def GetURL(self, url): | 278 def GetURL(self, url): |
240 """Gets a URL for the test.""" | 279 """Gets a URL for the test.""" |
241 return self.session.browserURL + self.path_to_html + url | 280 return self.session.browserURL + self.path_to_html + url |
242 | 281 |
243 def shortDescription(self): | 282 def shortDescription(self): |
244 """override unittest.TestCase shortDescription for our own descriptions.""" | 283 """override unittest.TestCase shortDescription for our own descriptions.""" |
245 if self.sample_path: | 284 if self.sample_path: |
246 return "Testing: " + self.sample_path + ".html" | 285 return "Testing: " + self.sample_path + ".html" |
247 else: | 286 else: |
(...skipping 12 matching lines...) Expand all Loading... |
260 | 299 |
261 If the sample is animated, it is expected to have a global variable | 300 If the sample is animated, it is expected to have a global variable |
262 called g_timeMult that can be set to 0 to stop the animation. All of its | 301 called g_timeMult that can be set to 0 to stop the animation. All of its |
263 animation must be based on a global variable called g_clock, such that | 302 animation must be based on a global variable called g_clock, such that |
264 setting g_clock to the same value will always produce the same image. | 303 setting g_clock to the same value will always produce the same image. |
265 | 304 |
266 Finally, each sample is expected to have a global variable called | 305 Finally, each sample is expected to have a global variable called |
267 g_client which is the o3d client object for that sample. This is | 306 g_client which is the o3d client object for that sample. This is |
268 used to take a screenshot. | 307 used to take a screenshot. |
269 """ | 308 """ |
270 screenshots = [] | 309 self.assertTrue(not self.timeout is None) |
271 timeout = 10000 | 310 self.assertTrue(not self.client is None) |
272 client = "g_client" | |
273 | |
274 self.assertTrue(self.test_type in ["small", "medium", "large"]) | 311 self.assertTrue(self.test_type in ["small", "medium", "large"]) |
275 | 312 |
276 # parse options | |
277 for option in self.options: | |
278 if option.startswith("screenshot"): | |
279 clock = GetArgument(option) | |
280 if clock is None: | |
281 clock = "27.5" | |
282 screenshots.append(clock) | |
283 elif option.startswith("timeout"): | |
284 timeout = GetArgument(option) | |
285 self.assertTrue(not timeout is None) | |
286 elif option.startswith("client"): | |
287 client = GetArgument(option) | |
288 self.assertTrue(not client is None) | |
289 | |
290 url = self.GetURL(base_path + self.sample_path + ".html") | 313 url = self.GetURL(base_path + self.sample_path + ".html") |
291 | 314 |
292 # load the sample. | 315 # load the sample. |
293 self.session.open(url) | 316 self.session.open(url) |
294 | 317 |
295 # wait for it to initialize. | 318 # wait for it to initialize. |
296 self.session.wait_for_condition(ready_condition, timeout) | 319 self.session.wait_for_condition(ready_condition, self.timeout) |
297 | 320 |
298 self.session.run_script( | 321 self.session.run_script( |
299 "if (window.o3d_prepForSelenium) { window.o3d_prepForSelenium(); }") | 322 "if (window.o3d_prepForSelenium) { window.o3d_prepForSelenium(); }") |
300 | 323 |
301 if assertion: | 324 if assertion: |
302 self.assertEqual("true", self.session.get_eval(assertion)) | 325 self.assertEqual("true", self.session.get_eval(assertion)) |
303 | 326 |
304 # take a screenshot. | 327 # take a screenshot. |
305 screenshot_id = 1 | 328 screenshot_id = 1 |
306 for clock in screenshots: | 329 for clock in self.screenshots: |
307 # if they are animated we need to stop the animation and set the clock | 330 # if they are animated we need to stop the animation and set the clock |
308 # to some time so we get a known state. | 331 # to some time so we get a known state. |
309 self.session.run_script("g_timeMult = 0") | 332 self.session.run_script("g_timeMult = 0") |
310 self.session.run_script("g_clock = " + clock) | 333 self.session.run_script("g_clock = " + clock) |
311 | 334 |
312 # take a screenshot. | 335 # take a screenshot. |
313 screenshot = self.sample_path.replace("/", "_") + str(screenshot_id) | 336 screenshot = self.sample_path.replace("_", "-").replace("/", "_") |
| 337 screenshot += str(screenshot_id) |
314 self.assertTrue(TakeScreenShot(self.session, self.browser, | 338 self.assertTrue(TakeScreenShot(self.session, self.browser, |
315 client, screenshot)) | 339 self.client, screenshot)) |
316 screenshot_id += 1 | 340 screenshot_id += 1 |
OLD | NEW |