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 | 13 import hashlib |
14 import os | 14 import os |
15 import platform | 15 import platform |
16 import sys | 16 import sys |
| 17 import tempfile |
17 import unittest | 18 import unittest |
18 import urllib | 19 import urllib |
19 import urllib2 | 20 import urllib2 |
20 import urlparse | 21 import urlparse |
21 | 22 |
22 from chromedriver_launcher import ChromeDriverLauncher | 23 from chromedriver_launcher import ChromeDriverLauncher |
23 import chromedriver_paths | 24 import chromedriver_paths |
24 from gtest_text_test_runner import GTestTextTestRunner | 25 from gtest_text_test_runner import GTestTextTestRunner |
25 | 26 |
26 sys.path += [chromedriver_paths.SRC_THIRD_PARTY] | 27 sys.path += [chromedriver_paths.SRC_THIRD_PARTY] |
27 sys.path += [chromedriver_paths.PYTHON_BINDINGS] | 28 sys.path += [chromedriver_paths.PYTHON_BINDINGS] |
28 | 29 |
29 try: | 30 try: |
30 import simplejson as json | 31 import simplejson as json |
31 except ImportError: | 32 except ImportError: |
32 import json | 33 import json |
33 | 34 |
| 35 from selenium.common.exceptions import WebDriverException |
34 from selenium.webdriver.remote.command import Command | 36 from selenium.webdriver.remote.command import Command |
35 from selenium.webdriver.remote.webdriver import WebDriver | 37 from selenium.webdriver.remote.webdriver import WebDriver |
36 from selenium.webdriver.common.keys import Keys | 38 from selenium.webdriver.common.keys import Keys |
37 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities | 39 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities |
38 | 40 |
39 | 41 |
40 def DataDir(): | 42 def DataDir(): |
41 """Returns the path to the data dir chrome/test/data.""" | 43 """Returns the path to the data dir chrome/test/data.""" |
42 return os.path.normpath( | 44 return os.path.normpath( |
43 os.path.join(os.path.dirname(__file__), os.pardir, "data")) | 45 os.path.join(os.path.dirname(__file__), os.pardir, "data")) |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 self.assertEquals(9520, launcher.GetPort()) | 173 self.assertEquals(9520, launcher.GetPort()) |
172 driver = WebDriver(launcher.GetURL(), DesiredCapabilities.CHROME) | 174 driver = WebDriver(launcher.GetURL(), DesiredCapabilities.CHROME) |
173 driver.quit() | 175 driver.quit() |
174 launcher.Kill() | 176 launcher.Kill() |
175 | 177 |
176 | 178 |
177 class WebserverTest(unittest.TestCase): | 179 class WebserverTest(unittest.TestCase): |
178 """Tests the built-in ChromeDriver webserver.""" | 180 """Tests the built-in ChromeDriver webserver.""" |
179 | 181 |
180 def testShouldNotServeFilesByDefault(self): | 182 def testShouldNotServeFilesByDefault(self): |
181 launcher = ChromeDriverLauncher() | |
182 try: | 183 try: |
183 SendRequest(launcher.GetURL(), method='GET') | 184 SendRequest(launcher.GetURL(), method='GET') |
184 self.fail('Should have raised a urllib.HTTPError for returned 403') | 185 self.fail('Should have raised a urllib.HTTPError for returned 403') |
185 except urllib2.HTTPError, expected: | 186 except urllib2.HTTPError, expected: |
186 self.assertEquals(403, expected.code) | 187 self.assertEquals(403, expected.code) |
187 finally: | 188 finally: |
188 launcher.Kill() | 189 launcher.Kill() |
189 | 190 |
190 def testCanServeFiles(self): | 191 def testCanServeFiles(self): |
191 launcher = ChromeDriverLauncher(root_path=os.path.dirname(__file__)) | 192 launcher = ChromeDriverLauncher(root_path=os.path.dirname(__file__)) |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 | 284 |
284 def tearDown(self): | 285 def tearDown(self): |
285 self._driver.quit() | 286 self._driver.quit() |
286 self._launcher.Kill() | 287 self._launcher.Kill() |
287 | 288 |
288 def testAddCookie(self): | 289 def testAddCookie(self): |
289 self._driver.get(self._launcher.GetURL() + '/test_page.html') | 290 self._driver.get(self._launcher.GetURL() + '/test_page.html') |
290 cookie_dict = None | 291 cookie_dict = None |
291 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") | 292 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") |
292 cookie_dict = {} | 293 cookie_dict = {} |
293 cookie_dict["name"]= "chromedriver_cookie_test" | 294 cookie_dict["name"] = "chromedriver_cookie_test" |
294 cookie_dict["value"] = "this is a test" | 295 cookie_dict["value"] = "this is a test" |
295 self._driver.add_cookie(cookie_dict) | 296 self._driver.add_cookie(cookie_dict) |
296 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") | 297 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") |
297 self.assertNotEqual(cookie_dict, None) | 298 self.assertNotEqual(cookie_dict, None) |
298 self.assertEqual(cookie_dict["value"], "this is a test") | 299 self.assertEqual(cookie_dict["value"], "this is a test") |
299 | 300 |
300 def testDeleteCookie(self): | 301 def testDeleteCookie(self): |
301 self.testAddCookie(); | 302 self.testAddCookie(); |
302 self._driver.delete_cookie("chromedriver_cookie_test") | 303 self._driver.delete_cookie("chromedriver_cookie_test") |
303 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") | 304 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 'autofill-edit-credit-card-apply-button').click() | 617 'autofill-edit-credit-card-apply-button').click() |
617 # Refresh the page to ensure the UI is up-to-date. | 618 # Refresh the page to ensure the UI is up-to-date. |
618 driver.refresh() | 619 driver.refresh() |
619 list_entry = driver.find_element_by_class_name('autofill-list-item') | 620 list_entry = driver.find_element_by_class_name('autofill-list-item') |
620 self.assertTrue(list_entry.is_displayed) | 621 self.assertTrue(list_entry.is_displayed) |
621 self.assertEqual(list_entry.text, | 622 self.assertEqual(list_entry.text, |
622 creditcard_data['CREDIT_CARD_NAME'], | 623 creditcard_data['CREDIT_CARD_NAME'], |
623 'Saved CC line item not same as what was entered.') | 624 'Saved CC line item not same as what was entered.') |
624 | 625 |
625 | 626 |
| 627 class FileUploadControlTest(unittest.TestCase): |
| 628 """Tests dealing with file upload control.""" |
| 629 |
| 630 def setUp(self): |
| 631 self._launcher = ChromeDriverLauncher(root_path=os.path.dirname(__file__)) |
| 632 self._driver = WebDriver(self._launcher.GetURL(), |
| 633 DesiredCapabilities.CHROME) |
| 634 |
| 635 def tearDown(self): |
| 636 self._driver.quit() |
| 637 self._launcher.Kill() |
| 638 |
| 639 def testSetFilePathToFileUploadControl(self): |
| 640 """Verify a file path is set to the file upload control.""" |
| 641 self._driver.get(self._launcher.GetURL() + '/upload.html') |
| 642 |
| 643 file = tempfile.NamedTemporaryFile() |
| 644 |
| 645 fileupload_single = self._driver.find_element_by_name('fileupload_single') |
| 646 multiple = fileupload_single.get_attribute('multiple') |
| 647 self.assertEqual('false', multiple) |
| 648 fileupload_single.send_keys(file.name) |
| 649 path = fileupload_single.value |
| 650 self.assertTrue(path.endswith(os.path.basename(file.name))) |
| 651 |
| 652 def testSetMultipleFilePathsToFileuploadControlWithoutMultipleWillFail(self): |
| 653 """Verify setting file paths to the file upload control without 'multiple' |
| 654 attribute will fail.""" |
| 655 self._driver.get(self._launcher.GetURL() + '/upload.html') |
| 656 |
| 657 files = [] |
| 658 filepaths = [] |
| 659 for index in xrange(4): |
| 660 file = tempfile.NamedTemporaryFile() |
| 661 # We need to hold the file objects because the files will be deleted on |
| 662 # GC. |
| 663 files.append(file) |
| 664 filepath = file.name |
| 665 filepaths.append(filepath) |
| 666 |
| 667 fileupload_single = self._driver.find_element_by_name('fileupload_single') |
| 668 multiple = fileupload_single.get_attribute('multiple') |
| 669 self.assertEqual('false', multiple) |
| 670 self.assertRaises(WebDriverException, fileupload_single.send_keys, |
| 671 filepaths[0], filepaths[1], filepaths[2], filepaths[3]) |
| 672 |
| 673 def testSetMultipleFilePathsToFileUploadControl(self): |
| 674 """Verify multiple file paths are set to the file upload control.""" |
| 675 self._driver.get(self._launcher.GetURL() + '/upload.html') |
| 676 |
| 677 files = [] |
| 678 filepaths = [] |
| 679 filenames = set() |
| 680 for index in xrange(4): |
| 681 file = tempfile.NamedTemporaryFile() |
| 682 files.append(file) |
| 683 filepath = file.name |
| 684 filepaths.append(filepath) |
| 685 filenames.add(os.path.basename(filepath)) |
| 686 |
| 687 fileupload_multi = self._driver.find_element_by_name('fileupload_multi') |
| 688 multiple = fileupload_multi.get_attribute('multiple') |
| 689 self.assertEqual('true', multiple) |
| 690 fileupload_multi.send_keys(filepaths[0], filepaths[1], filepaths[2], |
| 691 filepaths[3]) |
| 692 |
| 693 files_on_element = self._driver.execute_script( |
| 694 'return document.getElementById("fileupload_multi").files;') |
| 695 self.assertTrue(files_on_element) |
| 696 self.assertEqual(4, len(files_on_element)) |
| 697 for f in files_on_element: |
| 698 self.assertTrue(f['name'] in filenames) |
| 699 |
| 700 |
626 if __name__ == '__main__': | 701 if __name__ == '__main__': |
627 unittest.main(module='chromedriver_tests', | 702 unittest.main(module='chromedriver_tests', |
628 testRunner=GTestTextTestRunner(verbosity=1)) | 703 testRunner=GTestTextTestRunner(verbosity=1)) |
OLD | NEW |