import unittest import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys class NewVisitorTest(unittest.TestCase): def setUp(self): self.browser = webdriver.Firefox() def tearDown(self): self.browser.quit() def test_can_start_a_todo_list(self): # Ig has heard about a cool new online to-do app. # He goes to check out its homepage self.browser.get("http://localhost:8000") # He notices the page title and header mention to-do lists self.assertIn("To-Do", self.browser.title) header_text = self.browser.find_element(By.TAG_NAME, "h1").text self.assertIn("To-Do", header_text) # He is invited to enter a to-do item straight away inputbox = self.browser.find_element(By.ID, "id_new_item") self.assertEqual(inputbox.get_attribute("placeholder"), "Enter a to-do item") # He types "Buy peacock feathers" into a text box # (Ig's hobby is tying fly-fishing lures) inputbox.send_keys("Buy peacock feathers") # When he hits enter, the page updates, and now the page lists # "1: Buy peacock feathers" as an item in a to-do list inputbox.send_keys(Keys.ENTER) time.sleep(1) table = self.browser.find_element(By.ID, "id_list_table") rows = table.find_elements(By.TAG_NAME, "tr") self.assertIn("1: Buy peacock feathers", [row.text for row in rows]) # There is still a text box inviting him to add another item. # He enters "Use peacock feathers to make a fly" (Ig is very methodical) inputbox = self.browser.find_element(By.ID, "id_new_item") inputbox.send_keys("Use peacock feathers to make a fly") inputbox.send_keys(Keys.ENTER) time.sleep(1) # The page updates again, and now shows both items on his list table = self.browser.find_element(By.ID, "id_list_table") rows = table.find_elements(By.TAG_NAME, "tr") self.assertIn( "1: Buy peacock feathers", [row.text for row in rows], ) self.assertIn( "2: Use peacock feather sto make a fly", [row.text for row in rows], ) # Satisfied, he goes back to sleep if __name__ == "__main__": unittest.main()