Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 | 2 |
| 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Tests for ChromeDriver. | 7 """Tests for ChromeDriver. |
| 8 | 8 |
| 9 If your test is testing a specific part of the WebDriver API, consider adding | 9 If your test is testing a specific part of the WebDriver API, consider adding |
| 10 it to the appropriate place in the WebDriver tree instead. | 10 it to the appropriate place in the WebDriver tree instead. |
| 11 """ | 11 """ |
| 12 | 12 |
| 13 import hashlib | |
| 14 import os | |
| 13 import platform | 15 import platform |
| 14 import os | |
| 15 import sys | 16 import sys |
| 16 import unittest | 17 import unittest |
| 18 import urllib | |
| 17 import urllib2 | 19 import urllib2 |
| 18 import urlparse | 20 import urlparse |
| 19 | 21 |
| 20 from chromedriver_launcher import ChromeDriverLauncher | 22 from chromedriver_launcher import ChromeDriverLauncher |
| 21 import chromedriver_paths | 23 import chromedriver_paths |
| 22 from gtest_text_test_runner import GTestTextTestRunner | 24 from gtest_text_test_runner import GTestTextTestRunner |
| 23 | 25 |
| 24 sys.path += [chromedriver_paths.SRC_THIRD_PARTY] | 26 sys.path += [chromedriver_paths.SRC_THIRD_PARTY] |
| 25 sys.path += [chromedriver_paths.PYTHON_BINDINGS] | 27 sys.path += [chromedriver_paths.PYTHON_BINDINGS] |
| 26 | 28 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 | 78 |
| 77 class BasicTest(unittest.TestCase): | 79 class BasicTest(unittest.TestCase): |
| 78 """Basic ChromeDriver tests.""" | 80 """Basic ChromeDriver tests.""" |
| 79 | 81 |
| 80 def setUp(self): | 82 def setUp(self): |
| 81 self._launcher = ChromeDriverLauncher() | 83 self._launcher = ChromeDriverLauncher() |
| 82 | 84 |
| 83 def tearDown(self): | 85 def tearDown(self): |
| 84 self._launcher.Kill() | 86 self._launcher.Kill() |
| 85 | 87 |
| 88 @staticmethod | |
| 89 def DataDir(): | |
|
kkania
2011/03/17 18:35:11
move this out of BasicTest and put the function up
Joe
2011/03/18 00:33:32
Won't future tests all use reference web files. A
kkania
2011/03/18 19:18:22
Yes, I know this is from pyauto. However, right no
| |
| 90 """Returns the path to the data dir chrome/test/data.""" | |
| 91 return os.path.normpath( | |
| 92 os.path.join(os.path.dirname(__file__), os.pardir, "data")) | |
| 93 | |
| 94 @staticmethod | |
| 95 def GetFileURLForPath(path): | |
| 96 """Get file:// url for the given path. | |
| 97 Also quotes the url using urllib.quote(). | |
| 98 """ | |
| 99 abs_path = os.path.abspath(path) | |
| 100 if sys.platform == 'win32': | |
| 101 # Don't quote the ':' in drive letter ( say, C: ) on win. | |
| 102 # Also, replace '\' with '/' as expected in a file:/// url. | |
| 103 drive, rest = os.path.splitdrive(abs_path) | |
| 104 quoted_path = drive.upper() + urllib.quote((rest.replace('\\', '/'))) | |
| 105 return 'file:///' + quoted_path | |
| 106 else: | |
| 107 quoted_path = urllib.quote(abs_path) | |
| 108 return 'file://' + quoted_path | |
| 109 | |
| 86 def testShouldReturn404WhenSentAnUnknownCommandURL(self): | 110 def testShouldReturn404WhenSentAnUnknownCommandURL(self): |
| 87 request_url = self._launcher.GetURL() + '/foo' | 111 request_url = self._launcher.GetURL() + '/foo' |
| 88 try: | 112 try: |
| 89 SendRequest(request_url, method='GET') | 113 SendRequest(request_url, method='GET') |
| 90 self.fail('Should have raised a urllib.HTTPError for returned 404') | 114 self.fail('Should have raised a urllib.HTTPError for returned 404') |
| 91 except urllib2.HTTPError, expected: | 115 except urllib2.HTTPError, expected: |
| 92 self.assertEquals(404, expected.code) | 116 self.assertEquals(404, expected.code) |
| 93 | 117 |
| 94 def testShouldReturnHTTP405WhenSendingANonPostToTheSessionURL(self): | 118 def testShouldReturnHTTP405WhenSendingANonPostToTheSessionURL(self): |
| 95 request_url = self._launcher.GetURL() + '/session' | 119 request_url = self._launcher.GetURL() + '/session' |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 self.assertNotEqual(cookie_dict, None) | 165 self.assertNotEqual(cookie_dict, None) |
| 142 self.assertEqual(cookie_dict["value"], "this is a test"); | 166 self.assertEqual(cookie_dict["value"], "this is a test"); |
| 143 | 167 |
| 144 def testDeleteCookie(self): | 168 def testDeleteCookie(self): |
| 145 self.testAddCookie(); | 169 self.testAddCookie(); |
| 146 self._driver.delete_cookie("chromedriver_cookie_test") | 170 self._driver.delete_cookie("chromedriver_cookie_test") |
| 147 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") | 171 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") |
| 148 self.assertEqual(cookie_dict, None) | 172 self.assertEqual(cookie_dict, None) |
| 149 | 173 |
| 150 | 174 |
| 175 class ScreenshotTest(unittest.TestCase): | |
| 176 """Tests to verify screenshot retrieval""" | |
| 177 | |
| 178 SEARCH = "http://www.google.com/webhp?hl=en" | |
| 179 NEWS = "http://www.google.com/news" | |
| 180 REDBOX = "automation_proxy_snapshot/set_size.html" | |
| 181 | |
| 182 def setUp(self): | |
| 183 self._launcher = ChromeDriverLauncher() | |
| 184 self._driver = WebDriver(self._launcher.GetURL(), {}) | |
| 185 | |
| 186 def tearDown(self): | |
| 187 self._driver.quit() | |
| 188 self._launcher.Kill() | |
| 189 | |
| 190 def testScreenCapture(self): | |
| 191 self._driver.get(self.SEARCH) | |
|
kkania
2011/03/17 18:35:11
we should not use public sites on the bots unless
Joe
2011/03/18 00:33:32
Done.
kkania
2011/03/18 19:18:22
remove the SEARCH and NEW constants above too
| |
| 192 s1 = self._driver.get_screenshot_as_base64() | |
| 193 self._driver.get(self.NEWS) | |
| 194 s2 = self._driver.get_screenshot_as_base64() | |
| 195 self.assertTrue(len(s1) > 0 and len(s2) > 0) | |
| 196 self.assertTrue(len(s1) != len(s2)) | |
| 197 | |
| 198 def testScreenCaptureAgainstReference(self): | |
| 199 # Create a red square of 2000x2000 pixels. | |
| 200 url = BasicTest.GetFileURLForPath(os.path.join(BasicTest.DataDir(), | |
| 201 self.REDBOX)) | |
| 202 url = url + "?2000,2000" | |
| 203 self._driver.get(url) | |
| 204 s = self._driver.get_screenshot_as_base64(); | |
| 205 h = hashlib.md5(s).hexdigest() | |
| 206 # Compare the PNG created to the reference hash. | |
| 207 self.assertTrue(h, '12c0ade27e3875da3d8866f52d2fa84f') | |
|
kkania
2011/03/17 18:35:11
did this work on all platforms?
Joe
2011/03/18 00:33:32
Yah all platforms gave me the same PNG file with t
| |
| 208 | |
| 151 class SessionTest(unittest.TestCase): | 209 class SessionTest(unittest.TestCase): |
| 152 """Tests dealing with WebDriver sessions.""" | 210 """Tests dealing with WebDriver sessions.""" |
| 153 | 211 |
| 154 def setUp(self): | 212 def setUp(self): |
| 155 self._launcher = ChromeDriverLauncher() | 213 self._launcher = ChromeDriverLauncher() |
| 156 | 214 |
| 157 def tearDown(self): | 215 def tearDown(self): |
| 158 self._launcher.Kill() | 216 self._launcher.Kill() |
| 159 | 217 |
| 160 def testCreatingSessionShouldRedirectToCorrectURL(self): | 218 def testCreatingSessionShouldRedirectToCorrectURL(self): |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 self.assertEquals('', url_parts[0]) | 346 self.assertEquals('', url_parts[0]) |
| 289 self.assertEquals('wd', url_parts[1]) | 347 self.assertEquals('wd', url_parts[1]) |
| 290 self.assertEquals('hub', url_parts[2]) | 348 self.assertEquals('hub', url_parts[2]) |
| 291 self.assertEquals('session', url_parts[3]) | 349 self.assertEquals('session', url_parts[3]) |
| 292 self.assertEquals(data['sessionId'], url_parts[4]) | 350 self.assertEquals(data['sessionId'], url_parts[4]) |
| 293 | 351 |
| 294 | 352 |
| 295 if __name__ == '__main__': | 353 if __name__ == '__main__': |
| 296 unittest.main(module='chromedriver_tests', | 354 unittest.main(module='chromedriver_tests', |
| 297 testRunner=GTestTextTestRunner(verbosity=1)) | 355 testRunner=GTestTextTestRunner(verbosity=1)) |
| OLD | NEW |