Fixed util files accidentally passing themselves off as UnitTestCases.
[mediagoblin-automation:mediagoblin-selenium.git] / tests / test_AddMediaToCollection.py
1 #!/usr/bin/env python
2 '''
3    Copyright 2013 Emily O'Leary
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 '''
17 from util import login
18 from selenium import webdriver
19 from selenium.webdriver.common.by import By
20 from selenium.webdriver.support.ui import Select
21 from selenium.common.exceptions import NoSuchElementException
22 import unittest, time, re
23
24 class AddMediaToCollection(unittest.TestCase):
25     def setUp(self):
26         self.driver = webdriver.Firefox()
27         self.driver.implicitly_wait(30)
28         self.base_url = "http://127.0.0.1:6543"
29         self.verificationErrors = []
30         self.accept_next_alert = True
31     
32     def test_add_media_to_collection(self):
33         driver = self.driver
34         driver.get(self.base_url + "/")
35         tLogin = login.Login()
36         data = {"username" : "editorgolbin", "password" : "Fixyourgrammarplease"}
37         tLogin.log_in(self, data)
38         self.assertTrue(self.is_element_present(By.LINK_TEXT, "Safe Gavroche"))
39         driver.find_element_by_link_text("Safe Gavroche").click()
40         self.assertTrue(self.is_element_present(By.LINK_TEXT, "Add to a collection"))
41         driver.find_element_by_link_text("Add to a collection").click()
42         self.assertTrue(self.is_element_present(By.ID, "button_addcollection"))
43         driver.find_element_by_id("button_addcollection").click()
44         try: self.assertTrue(self.is_element_present(By.ID, "collection_title"))
45         except AssertionError as e: self.verificationErrors.append(str(e))
46         driver.find_element_by_id("collection_title").send_keys("Test Collection")
47         try: self.assertTrue(self.is_element_present(By.ID, "collection_description"))
48         except AssertionError as e: self.verificationErrors.append(str(e))
49         driver.find_element_by_id("collection_description").clear()
50         driver.find_element_by_id("collection_description").send_keys("This is a test of the collections")
51         try: self.assertTrue(self.is_element_present(By.ID, "note"))
52         except AssertionError as e: self.verificationErrors.append(str(e))
53         driver.find_element_by_id("note").clear()
54         driver.find_element_by_id("note").send_keys("This is a note")
55         self.assertTrue(self.is_element_present(By.CSS_SELECTOR, "input.button_form"))
56         driver.find_element_by_css_selector("input.button_form").click()
57         try: self.assertEqual("\"Safe Gavroche\" added to collection \"Test Collection\"", driver.find_element_by_css_selector("li.message_success").text)
58         except AssertionError as e: self.verificationErrors.append(str(e))
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()