| 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(): |
| 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 testScreenCaptureAgainstReference(self): |
| 191 # Create a red square of 2000x2000 pixels. |
| 192 url = BasicTest.GetFileURLForPath(os.path.join(BasicTest.DataDir(), |
| 193 self.REDBOX)) |
| 194 url = url + "?2000,2000" |
| 195 self._driver.get(url) |
| 196 s = self._driver.get_screenshot_as_base64(); |
| 197 h = hashlib.md5(s).hexdigest() |
| 198 # Compare the PNG created to the reference hash. |
| 199 self.assertTrue(h, '12c0ade27e3875da3d8866f52d2fa84f') |
| 200 |
| 151 class SessionTest(unittest.TestCase): | 201 class SessionTest(unittest.TestCase): |
| 152 """Tests dealing with WebDriver sessions.""" | 202 """Tests dealing with WebDriver sessions.""" |
| 153 | 203 |
| 154 def setUp(self): | 204 def setUp(self): |
| 155 self._launcher = ChromeDriverLauncher() | 205 self._launcher = ChromeDriverLauncher() |
| 156 | 206 |
| 157 def tearDown(self): | 207 def tearDown(self): |
| 158 self._launcher.Kill() | 208 self._launcher.Kill() |
| 159 | 209 |
| 160 def testCreatingSessionShouldRedirectToCorrectURL(self): | 210 def testCreatingSessionShouldRedirectToCorrectURL(self): |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 self.assertEquals('', url_parts[0]) | 338 self.assertEquals('', url_parts[0]) |
| 289 self.assertEquals('wd', url_parts[1]) | 339 self.assertEquals('wd', url_parts[1]) |
| 290 self.assertEquals('hub', url_parts[2]) | 340 self.assertEquals('hub', url_parts[2]) |
| 291 self.assertEquals('session', url_parts[3]) | 341 self.assertEquals('session', url_parts[3]) |
| 292 self.assertEquals(data['sessionId'], url_parts[4]) | 342 self.assertEquals(data['sessionId'], url_parts[4]) |
| 293 | 343 |
| 294 | 344 |
| 295 if __name__ == '__main__': | 345 if __name__ == '__main__': |
| 296 unittest.main(module='chromedriver_tests', | 346 unittest.main(module='chromedriver_tests', |
| 297 testRunner=GTestTextTestRunner(verbosity=1)) | 347 testRunner=GTestTextTestRunner(verbosity=1)) |
| OLD | NEW |