tests.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from django.test import LiveServerTestCase
  2. from selenium import webdriver
  3. from selenium.webdriver.common.keys import Keys
  4. import time
  5. import unittest
  6. class NewVisitorTest(LiveServerTestCase):
  7. def setUp(self):
  8. self.browser = webdriver.Firefox()
  9. def tearDown(self):
  10. self.browser.quit()
  11. def check_for_row_in_list_table(self, row_text):
  12. table = self.browser.find_element_by_id('movie-table')
  13. rows = table.find_elements_by_tag_name('tr')
  14. self.assertIn(row_text, [row.text for row in rows])
  15. def test_can_start_a_list_and_retrieve_it_later(self):
  16. # Edith has heard about a cool new online to-do app. She goes
  17. # to check out its homepage
  18. self.browser.get(self.live_server_url)
  19. # She notices the page title and header mention to-do lists
  20. self.assertIn('MovieLib', self.browser.title)
  21. header_text = self.browser.find_element_by_tag_name('h1').text
  22. self.assertIn('Your Movie list', header_text)
  23. # She is invited to enter a to-do item straight away
  24. inputbox = self.browser.find_element_by_id('input-new-movie')
  25. self.assertEqual(
  26. inputbox.get_attribute('placeholder'),
  27. 'Enter a movie title'
  28. )
  29. # She types "Inception" into a text box
  30. inputbox.send_keys('Inception')
  31. # When she hits enter, the page updates, and now the page lists
  32. # "1: Inception" as an item in a to-do list table
  33. inputbox.send_keys(Keys.ENTER)
  34. time.sleep(1)
  35. self.check_for_row_in_list_table('1: Inception')
  36. inputbox = self.browser.find_element_by_id('input-new-movie')
  37. inputbox.send_keys('The Sixth Sense')
  38. inputbox.send_keys(Keys.ENTER)
  39. time.sleep(1)
  40. self.check_for_row_in_list_table('1: Inception')
  41. self.check_for_row_in_list_table('2: The Sixth Sense')
  42. # There is still a text box inviting her to add another item. She
  43. # enters "Armageddon"
  44. # self.fail('Finish the test!')
  45. # She is invited to enter a to-do item straight away