Added login call to each file. (Some still need users added to data.ini).
[mediagoblin-automation:mediagoblin-selenium.git] / tests / test_AddComment.py
1 from util import login
2 from selenium import webdriver
3 from selenium.webdriver.common.by import By
4 from selenium.webdriver.support.ui import Select
5 from selenium.common.exceptions import NoSuchElementException
6 import unittest, time, re
7
8 class AddAComment(unittest.TestCase):
9     def setUp(self):
10         self.driver = webdriver.Firefox()
11         self.driver.implicitly_wait(30)
12         self.base_url = "http://127.0.0.1:6543"
13         self.verificationErrors = []
14         self.accept_next_alert = True
15     
16     def test_add_a_comment(self):
17         driver = self.driver
18         driver.get(self.base_url + "/")
19         tLogin = login.Login()
20         data = {"username" : "testcommentor", "password" : "IamaGoblinN0mN0mN0m"}
21         tLogin.log_in(self, data)
22         self.assertTrue(self.is_element_present(By.LINK_TEXT, "Gavroche Test"))
23         driver.find_element_by_link_text("Gavroche Test").click()
24         self.assertTrue(self.is_element_present(By.ID, "button_addcomment"))
25         driver.find_element_by_id("button_addcomment").click()
26         self.assertTrue(self.is_element_present(By.ID, "comment_content"))
27         driver.find_element_by_id("comment_content").clear()
28         driver.find_element_by_id("comment_content").send_keys("This is a test comment.")
29         try: self.assertEqual("Comment Preview", driver.find_element_by_css_selector("h3").text)
30         except AssertionError as e: self.verificationErrors.append(str(e))
31         self.assertTrue(self.is_element_present(By.CSS_SELECTOR, "input.button_action"))
32         driver.find_element_by_css_selector("input.button_action").click()
33         try: self.assertEqual("This is a test comment.", driver.find_element_by_css_selector("div.comment_content > p").text)
34         except AssertionError as e: self.verificationErrors.append(str(e))
35         try: self.assertEqual("testuser", driver.find_element_by_css_selector("a.comment_authorlink").text)
36         except AssertionError as e: self.verificationErrors.append(str(e))
37     
38     def is_element_present(self, how, what):
39         try: self.driver.find_element(by=how, value=what)
40         except NoSuchElementException, e: return False
41         return True
42     
43     def is_alert_present(self):
44         try: self.driver.switch_to_alert()
45         except NoAlertPresentException, e: return False
46         return True
47     
48     def close_alert_and_get_its_text(self):
49         try:
50             alert = self.driver.switch_to_alert()
51             alert_text = alert.text
52             if self.accept_next_alert:
53                 alert.accept()
54             else:
55                 alert.dismiss()
56             return alert_text
57         finally: self.accept_next_alert = True
58     
59     def tearDown(self):
60         self.driver.quit()
61         self.assertEqual([], self.verificationErrors)
62
63 if __name__ == "__main__":
64     unittest.main()