Added #!/usr/bin/env python to tests.
[mediagoblin-automation:mediagoblin-selenium.git] / tests / test_UploadJPEG.py
1 '''
2    Copyright 2013 Emily O'Leary
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 '''
16 from util import login
17 from selenium import webdriver
18 from selenium.webdriver.common.by import By
19 from selenium.webdriver.support.ui import Select
20 from selenium.common.exceptions import NoSuchElementException
21 import unittest, time, re
22
23 class UploadJPEG(unittest.TestCase):
24     def setUp(self):
25         self.driver = webdriver.Firefox()
26         self.driver.implicitly_wait(30)
27         self.base_url = "http://127.0.0.1:6543"
28         self.verificationErrors = []
29         self.accept_next_alert = True
30     
31     def test_upload_j_p_e_g(self):
32         driver = self.driver
33         driver.get(self.base_url + "/")
34         tLogin = login.Login()
35         data = {"username" : "testuploader", "password" : "GreatTestingPassword!5"}
36         tLogin.log_in(self, data)
37         driver.find_element_by_css_selector("div.button_action.header_dropdown_down").click()
38         self.assertTrue(self.is_element_present(By.LINK_TEXT, "Add media"))
39         driver.find_element_by_link_text("Add media").click()
40         # Warning: verifyTextPresent may require manual changes
41         try: self.assertRegexpMatches(driver.find_element_by_css_selector("BODY").text, r"^[\s\S]*Add your media[\s\S]*$")
42         except AssertionError as e: self.verificationErrors.append(str(e))
43         self.assertTrue(self.is_element_present(By.ID, "file"))
44         driver.find_element_by_id("file").clear()
45         driver.find_element_by_id("file").send_keys("WORKING_DIRECTORY/resources/gavroche.jpg")
46         self.assertTrue(self.is_element_present(By.ID, "title"))
47         driver.find_element_by_id("title").clear()
48         driver.find_element_by_id("title").send_keys("Gavroche Test")
49         driver.find_element_by_id("description").clear()
50         driver.find_element_by_id("description").send_keys("This is automatically uploaded by the Selenium scripts")
51         driver.find_element_by_id("tags").clear()
52         driver.find_element_by_id("tags").send_keys("selenium,testing,gavroche")
53         # ERROR: Caught exception [ReferenceError: selectLocator is not defined]
54         self.assertTrue(self.is_element_present(By.ID, "license"))
55         self.assertTrue(self.is_element_present(By.ID, "tags"))
56         self.assertTrue(self.is_element_present(By.CSS_SELECTOR, "input.button_form"))
57         driver.find_element_by_css_selector("input.button_form").click()
58         self.assertTrue(self.is_element_present(By.LINK_TEXT, "Gavroche Test"))
59     
60     def is_element_present(self, how, what):
61         try: self.driver.find_element(by=how, value=what)
62         except NoSuchElementException, e: return False
63         return True
64     
65     def is_alert_present(self):
66         try: self.driver.switch_to_alert()
67         except NoAlertPresentException, e: return False
68         return True
69     
70     def close_alert_and_get_its_text(self):
71         try:
72             alert = self.driver.switch_to_alert()
73             alert_text = alert.text
74             if self.accept_next_alert:
75                 alert.accept()
76             else:
77                 alert.dismiss()
78             return alert_text
79         finally: self.accept_next_alert = True
80     
81     def tearDown(self):
82         self.driver.quit()
83         self.assertEqual([], self.verificationErrors)
84
85 if __name__ == "__main__":
86     unittest.main()