tests.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 = 3
  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.2)
  25. def test_can_start_a_list_for_one_user(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. header_text = self.browser.find_element_by_tag_name('h2').text
  34. self.assertIn('Not logged-in', header_text)
  35. # She is invited to enter a to-do item straight away
  36. inputbox = self.browser.find_element_by_id('input-new-movie')
  37. self.assertEqual(
  38. inputbox.get_attribute('placeholder'),
  39. 'Enter a movie title'
  40. )
  41. # She types "Inception" into a text box
  42. inputbox.send_keys('Inception')
  43. # When she hits enter, the page updates, and now the page lists
  44. # "1: Inception" as an item in a to-do list table
  45. inputbox.send_keys(Keys.ENTER)
  46. self.wait_for_row_in_list_table('1: Inception')
  47. inputbox = self.browser.find_element_by_id('input-new-movie')
  48. inputbox.send_keys('The Sixth Sense')
  49. inputbox.send_keys(Keys.ENTER)
  50. self.wait_for_row_in_list_table('1: Inception')
  51. self.wait_for_row_in_list_table('2: The Sixth Sense')
  52. # There is still a text box inviting her to add another item. She
  53. # enters "Armageddon"
  54. # self.fail('Finish the test!')
  55. # She is invited to enter a to-do item straight away
  56. def test_multiple_users_can_start_lists_at_different_urls(self):
  57. # Edith starts a new to-do list
  58. self.browser.get(self.live_server_url)
  59. inputbox = self.browser.find_element_by_id('input-new-movie')
  60. inputbox.send_keys('Avatar')
  61. inputbox.send_keys(Keys.ENTER)
  62. self.wait_for_row_in_list_table('1: Avatar')
  63. # She notices that her list has a unique URL
  64. edith_list_url = self.browser.current_url
  65. self.assertRegex(edith_list_url, '/libraries/.+')
  66. ## We use a new browser session to make sure that no information
  67. ## of Edith's is coming through from cookies etc
  68. self.browser.quit()
  69. self.browser = webdriver.Firefox()
  70. # Francis visits the home page. There is no sign of Edith's
  71. # list
  72. self.browser.get(self.live_server_url)
  73. page_text = self.browser.find_element_by_tag_name('body').text
  74. self.assertNotIn('Avatar', page_text)
  75. self.assertNotIn('make a fly', page_text)
  76. # Francis starts a new list by entering a new item. He
  77. # is less interesting than Edith...
  78. inputbox = self.browser.find_element_by_id('input-new-movie')
  79. inputbox.send_keys('X-Men')
  80. inputbox.send_keys(Keys.ENTER)
  81. self.wait_for_row_in_list_table('1: X-Men')
  82. # Francis gets his own unique URL
  83. francis_list_url = self.browser.current_url
  84. self.assertRegex(francis_list_url, '/libraries/.+')
  85. self.assertNotEqual(francis_list_url, edith_list_url)
  86. # Again, there is no trace of Edith's list
  87. page_text = self.browser.find_element_by_tag_name('body').text
  88. self.assertNotIn('Avatar', page_text)
  89. self.assertIn('X-Men', page_text)
  90. # Satisfied, they both go back to sleep