tests.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from django.contrib.staticfiles.testing import StaticLiveServerTestCase
  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(StaticLiveServerTestCase):
  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)
  72. def test_layout_and_styling(self):
  73. self.browser.get(self.live_server_url)
  74. self.browser.set_window_size(1024, 768)
  75. inputbox = self.browser.find_element_by_id('id_new_item')
  76. self.assertAlmostEqual(
  77. inputbox.location['x'] + inputbox.size['width'] / 2,
  78. 512,
  79. delta=10
  80. )
  81. inputbox.send_keys('testing')
  82. inputbox.send_keys(Keys.ENTER)
  83. self.wait_for_row_in_list_table('1: testing')
  84. inputbox = self.browser.find_element_by_id('id_new_item')
  85. self.assertAlmostEqual(
  86. inputbox.location['x'] + inputbox.size['width'] / 2,
  87. 512,
  88. delta=10
  89. )