tests.py 4.4 KB

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