tests.py 5.6 KB

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