tests.py 2.4 KB

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