tests.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. MAX_WAIT = 10
  7. class NewVisitorTest(LiveServerTestCase):
  8. def setUp(self):
  9. #self.browser = webdriver.Firefox()
  10. self.browser = webdriver.Chrome()
  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('id_list_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.1)
  25. def test_can_start_a_list_for_one_user(self):
  26. self.browser.get(self.live_server_url)
  27. self.assertIn('To-Do', self.browser.title)
  28. header_text = self.browser.find_element_by_tag_name('h1').text
  29. self.assertIn('To-Do', header_text)
  30. inputbox = self.browser.find_element_by_id('id_new_item')
  31. self.assertEqual(
  32. inputbox.get_attribute('placeholder'),
  33. 'Enter a to-do item'
  34. )
  35. # ITEM 1
  36. inputbox.send_keys('Buy rubber ducks')
  37. inputbox.send_keys(Keys.ENTER)
  38. self.wait_for_row_in_list_table('1: Buy rubber ducks')
  39. # ITEM 2
  40. inputbox = self.browser.find_element_by_id('id_new_item')
  41. inputbox.send_keys('Buy squirrel suits')
  42. inputbox.send_keys(Keys.ENTER)
  43. self.wait_for_row_in_list_table('1: Buy rubber ducks')
  44. self.wait_for_row_in_list_table('2: Buy squirrel suits')
  45. # Done?
  46. #self.fail('Finish the test!')
  47. def test_multi_users_can_start_list_diff_urls(self):
  48. self.browser.get(self.live_server_url)
  49. ib = self.browser.find_element_by_id('id_new_item')
  50. ib.send_keys('Buy super bugz outfit')
  51. ib.send_keys(Keys.ENTER)
  52. self.wait_for_row_in_list_table('1: Buy super bugz outfit')
  53. list_url = self.browser.current_url
  54. self.assertRegex(list_url, '/lists/.+')
  55. # NEW USER
  56. self.browser.quit()
  57. self.browser = webdriver.Chrome()
  58. self.browser.get(self.live_server_url)
  59. page_text = self.browser.find_element_by_tag_name('body').text
  60. self.assertNotIn('Buy super bugz outfit', page_text)
  61. self.assertNotIn('make a fly', page_text)
  62. inputbox = self.browser.find_element_by_id('id_new_item')
  63. inputbox.send_keys('Buy milk')
  64. inputbox.send_keys(Keys.ENTER)
  65. self.wait_for_row_in_list_table('1: Buy milk')
  66. francis_list_url = self.browser.current_url
  67. self.assertRegex(francis_list_url, '/lists/.+')
  68. self.assertNotEqual(francis_list_url, list_url)
  69. page_text = self.browser.find_element_by_tag_name('body').text
  70. self.assertNotIn('Buy peacock feathers', page_text)
  71. self.assertIn('Buy milk', page_text)