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

Side by Side Diff: chrome/test/webdriver/chromedriver_tests.py

Issue 7055004: File upload API in chromedriver (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Fixed the test to check the 'files' property. Created 9 years, 6 months 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
« no previous file with comments | « chrome/test/webdriver/automation.cc ('k') | chrome/test/webdriver/commands/mouse_commands.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 'autofill-edit-credit-card-apply-button').click() 615 'autofill-edit-credit-card-apply-button').click()
615 # Refresh the page to ensure the UI is up-to-date. 616 # Refresh the page to ensure the UI is up-to-date.
616 driver.refresh() 617 driver.refresh()
617 list_entry = driver.find_element_by_class_name('autofill-list-item') 618 list_entry = driver.find_element_by_class_name('autofill-list-item')
618 self.assertTrue(list_entry.is_displayed) 619 self.assertTrue(list_entry.is_displayed)
619 self.assertEqual(list_entry.text, 620 self.assertEqual(list_entry.text,
620 creditcard_data['CREDIT_CARD_NAME'], 621 creditcard_data['CREDIT_CARD_NAME'],
621 'Saved CC line item not same as what was entered.') 622 'Saved CC line item not same as what was entered.')
622 623
623 624
625 class FileUploadControlTest(unittest.TestCase):
626 """Tests dealing with file upload control."""
627
628 def setUp(self):
629 self._launcher = ChromeDriverLauncher(root_path=os.path.dirname(__file__))
630 self._driver = WebDriver(self._launcher.GetURL(),
631 DesiredCapabilities.CHROME)
632
633 def tearDown(self):
634 self._driver.quit()
635 self._launcher.Kill()
636
637 def testSetFilePathToFileUploadControl(self):
638 """Verify a file path is set to the file upload control."""
639 self._driver.get(self._launcher.GetURL() + '/upload.html')
640
641 file = tempfile.NamedTemporaryFile()
642
643 fileupload_single = self._driver.find_element_by_name('fileupload_single')
644 multiple = fileupload_single.get_attribute('multiple')
645 self.assertEqual('false', multiple)
646 fileupload_single.send_keys(file.name)
647 path = fileupload_single.value
648 self.assertTrue(path.endswith(os.path.basename(file.name)))
649
650 def testSetMultipleFilePathsToFileuploadControlWithoutMultipleWillFail(self):
651 """Verify setting file paths to the file upload control without 'multiple'
652 attribute will fail."""
653 self._driver.get(self._launcher.GetURL() + '/upload.html')
654
655 files = list()
kkania 2011/06/16 15:48:59 looks like this is never used; remove?
nodchip 2011/06/27 04:05:53 I think we need to hold file objects because the f
656 filepaths = list()
kkania 2011/06/16 15:48:59 list() -> [], everywhere used
nodchip 2011/06/27 04:05:53 Done.
657 for index in xrange(4):
658 file = tempfile.NamedTemporaryFile()
659 files.append(file)
660 filepath = file.name
661 filepaths.append(filepath)
662
663 fileupload_single = self._driver.find_element_by_name('fileupload_single')
664 multiple = fileupload_single.get_attribute('multiple')
665 self.assertEqual('false', multiple)
666 self.assertRaises(WebDriverException, fileupload_single.send_keys,
667 filepaths[0], filepaths[1], filepaths[2], filepaths[3])
668 path = fileupload_single.value
kkania 2011/06/16 15:48:59 value is supposed to be deprectated in the next re
nodchip 2011/06/27 04:05:53 Done.
669 self.assertFalse(path)
670
671 def testSetMultipleFilePathsToFileUploadControl(self):
672 """Verify multiple file paths are set to the file upload control."""
673 self._driver.get(self._launcher.GetURL() + '/upload.html')
674
675 files = list()
kkania 2011/06/16 15:48:59 looks like this is never used; remove?
nodchip 2011/06/27 04:05:53 Please see above.
676 filepaths = list()
677 filenames = set()
678 for index in xrange(4):
679 file = tempfile.NamedTemporaryFile()
680 files.append(file)
681 filepath = file.name
682 filepaths.append(filepath)
683 filenames.add(os.path.basename(filepath))
684
685 fileupload_multi = self._driver.find_element_by_name('fileupload_multi')
686 multiple = fileupload_multi.get_attribute('multiple')
687 self.assertEqual('true', multiple)
688 fileupload_multi.send_keys(filepaths[0], filepaths[1], filepaths[2],
689 filepaths[3])
690 path = fileupload_multi.value.replace('\\', '//')
kkania 2011/06/16 15:48:59 how about remove this and the next line (since it
nodchip 2011/06/27 04:05:53 Done.
691 self.assertTrue(os.path.basename(path) in filenames)
692
693 filesOnElement = self._driver.execute_script(
kkania 2011/06/16 15:48:59 files_on_element
nodchip 2011/06/27 04:05:53 Done.
694 'return document.getElementById("fileupload_multi").files;')
695 self.assertTrue(filesOnElement)
696 self.assertEqual(4, len(filesOnElement))
697 for f in filesOnElement:
698 name = f['name']
699 self.assertTrue(name in filenames)
kkania 2011/06/16 15:48:59 remove name and use f['name'] directly
nodchip 2011/06/27 04:05:53 Done.
700
701
624 if __name__ == '__main__': 702 if __name__ == '__main__':
625 unittest.main(module='chromedriver_tests', 703 unittest.main(module='chromedriver_tests',
626 testRunner=GTestTextTestRunner(verbosity=1)) 704 testRunner=GTestTextTestRunner(verbosity=1))
OLDNEW
« no previous file with comments | « chrome/test/webdriver/automation.cc ('k') | chrome/test/webdriver/commands/mouse_commands.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698