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