tests.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_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. # 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
  54. def test_multiple_users_can_start_lists_at_different_urls(self):
  55. # Edith starts a new to-do list
  56. self.browser.get(self.live_server_url)
  57. inputbox = self.browser.find_element_by_id('input-new-movie')
  58. inputbox.send_keys('Avatar')
  59. inputbox.send_keys(Keys.ENTER)
  60. self.wait_for_row_in_list_table('1: Avatar')
  61. # She notices that her list has a unique URL
  62. edith_list_url = self.browser.current_url
  63. self.assertRegex(edith_list_url, '/libraries/.+')
  64. ## We use a new browser session to make sure that no information
  65. ## of Edith's is coming through from cookies etc
  66. self.browser.quit()
  67. self.browser = webdriver.Firefox()
  68. # Francis visits the home page. There is no sign of Edith's
  69. # list
  70. self.browser.get(self.live_server_url)
  71. page_text = self.browser.find_element_by_tag_name('body').text
  72. self.assertNotIn('Avatar', page_text)
  73. self.assertNotIn('make a fly', page_text)
  74. # Francis starts a new list by entering a new item. He
  75. # is less interesting than Edith...
  76. inputbox = self.browser.find_element_by_id('input-new-movie')
  77. inputbox.send_keys('X-Men')
  78. inputbox.send_keys(Keys.ENTER)
  79. self.wait_for_row_in_list_table('1: X-Men')
  80. # Francis gets his own unique URL
  81. francis_list_url = self.browser.current_url
  82. self.assertRegex(francis_list_url, '/libraries/.+')
  83. self.assertNotEqual(francis_list_url, edith_list_url)
  84. # Again, there is no trace of Edith's list
  85. page_text = self.browser.find_element_by_tag_name('body').text
  86. self.assertNotIn('Avatar', page_text)
  87. self.assertIn('X-Men', page_text)
  88. # Satisfied, they both go back to sleep