diff --git a/.gitignore b/.gitignore index e47e37305af3485914874b7b4b2c314b943a5645..3c416e9941655eb6002c2084192c3eb3c6e10d6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ db.sqlite3 -geckodriver.log +geckodriver.* env/ __pycache__ \ No newline at end of file diff --git a/functional_tests.py b/functional_tests.py index c3f17ed7ad9dd0d529544c344aa101710079d7a9..7caeb809acdc7b411ee6ff7fc62531da1cedddb4 100644 --- a/functional_tests.py +++ b/functional_tests.py @@ -1,12 +1,25 @@ +from selenium.common.exceptions import WebDriverException +from django.test import LiveServerTestCase from selenium import webdriver from selenium.webdriver.common.keys import Keys +from selenium.webdriver.firefox.options import Options import time import unittest +import environ -class NewVisitorTest(unittest.TestCase): +MAX_WAIT = 10 + +env = environ.Env(DEBUG=(bool, False)) +environ.Env.read_env('.env') + + +class NewVisitorTest(LiveServerTestCase): def setUp(self): - self.browser = webdriver.Firefox() + options = Options() + options.headless = True + self.browser = webdriver.Firefox(options=options) + self.browser.implicitly_wait(5) def tearDown(self): self.browser.quit() @@ -19,17 +32,18 @@ class NewVisitorTest(unittest.TestCase): def test_can_start_a_list_and_retrieve_it_later(self): # Edith has heard about a cool new online to-do app. She goes # to check out its homepage - self.browser.get('http://localhost:8000') + url = env("HEROKU_APP_HOST", default='http://localhost:8000') + self.browser.get(url) self.assertIn('Homepage - Farah Alhaniy', self.browser.title) # She notices the page title and header mention to-do lists # self.assertIn('To-Do', self.browser.title) header_text = self.browser.find_element_by_tag_name('h1').text - self.assertIn('To-Do', header_text) + self.assertIn('Hi!', header_text) # She is invited to enter a to-do item straight away - inputbox = self.browser.find_element_by_id('id_new_item') + inputbox = self.browser.find_element_by_id('id_new_item') self.assertEqual( inputbox.get_attribute('placeholder'), 'Enter a to-do item' @@ -42,28 +56,44 @@ class NewVisitorTest(unittest.TestCase): # When she hits enter, the page updates, and now the page lists # "1: Buy peacock feathers" as an item in a to-do list table inputbox.send_keys(Keys.ENTER) - time.sleep(3) - self.check_for_row_in_list_table('1: Buy peacock feathers') + self.wait_for_row_in_list_table('1: Buy peacock feathers') - # There is still a text box inviting her to add another item. She - # enters "Use peacock feathers to make a fly" (Edith is very - # methodical) - inputbox = self.browser.find_element_by_id('id_new_item') - inputbox.send_keys('Use peacock feathers to make a fly') - inputbox.send_keys(Keys.ENTER) - time.sleep(3) + # There is still a text box inviting her to add another item. She + # enters "Use peacock feathers to make a fly" (Edith is very + # methodical) + inputbox = self.browser.find_element_by_id('id_new_item') + inputbox.send_keys('Use peacock feathers to make a fly') + inputbox.send_keys(Keys.ENTER) - # The page updates again, and now shows both items on her list - self.check_for_row_in_list_table('1: Buy peacock feathers') - self.check_for_row_in_list_table('2: Use peacock feathers to make a fly') + # The page updates again, and now shows both items on her list + self.wait_for_row_in_list_table('2: Use peacock feathers to make a fly') + self.wait_for_row_in_list_table('1: Buy peacock feathers') # Edith wonders whether the site will remember her list. Then she sees # that the site has generated a unique URL for her -- there is some # explanatory text to that effect. - self.fail('Finish the test!') + #self.fail('Finish the test!') # She visits that URL - her to-do list is still there. -# Satisfied, she goes back to sleep + # Satisfied, she goes back to sleep + + def test_can_start_a_list_and_retrieve_it_later(self): + # Edith has heard about a cool new online to-do app. She goes + # to check out its homepage + self.browser.get(self.live_server_url) + + def wait_for_row_in_list_table(self, row_text): + start_time = time.time() + while True: + try: + table = self.browser.find_element_by_id('id_list_table') + rows = table.find_elements_by_tag_name('tr') + self.assertIn(row_text, [row.text for row in rows]) + return + except (AssertionError, WebDriverException) as e: + if time.time() - start_time > MAX_WAIT: + raise e + time.sleep(0.5) -if __name__ == '__main__': - unittest.main(warnings='ignore') \ No newline at end of file +#if __name__ == '__main__': +# unittest.main(warnings='ignore') diff --git a/functional_tests/__init__.py b/functional_tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..8b137891791fe96927ad78e64b0aad7bded08bdc --- /dev/null +++ b/functional_tests/__init__.py @@ -0,0 +1 @@ + diff --git a/functional_tests/tests.py b/functional_tests/tests.py new file mode 100644 index 0000000000000000000000000000000000000000..5685973bb577add27afe4398b365ed6b571a8626 --- /dev/null +++ b/functional_tests/tests.py @@ -0,0 +1,79 @@ +from selenium import webdriver +from selenium.webdriver.common.keys import Keys +from selenium.webdriver.firefox.options import Options +import time +import unittest +import environ + +env = environ.Env(DEBUG=(bool, False)) +environ.Env.read_env('.env') + + +class NewVisitorTest(unittest.TestCase): + + def setUp(self): + options = Options() + options.headless = True + self.browser = webdriver.Firefox(options=options) + self.browser.implicitly_wait(5) + + def tearDown(self): + self.browser.quit() + + def check_for_row_in_list_table(self, row_text): + table = self.browser.find_element_by_id('id_list_table') + rows = table.find_elements_by_tag_name('tr') + self.assertIn(row_text, [row.text for row in rows]) + + def test_can_start_a_list_and_retrieve_it_later(self): + # Edith has heard about a cool new online to-do app. She goes + # to check out its homepage + url = env("HEROKU_APP_HOST", default='http://localhost:8000') + self.browser.get(url) + + self.assertIn('Homepage - Farah Alhaniy', self.browser.title) + + # She notices the page title and header mention to-do lists + # self.assertIn('To-Do', self.browser.title) + header_text = self.browser.find_element_by_tag_name('h1').text + self.assertIn('Hi!', header_text) + + # She is invited to enter a to-do item straight away + inputbox = self.browser.find_element_by_id('id_new_item') + self.assertEqual( + inputbox.get_attribute('placeholder'), + 'Enter a to-do item' + ) + + # She types "Buy peacock feathers" into a text box (Edith's hobby + # is tying fly-fishing lures) + inputbox.send_keys('Buy peacock feathers') + + # When she hits enter, the page updates, and now the page lists + # "1: Buy peacock feathers" as an item in a to-do list table + inputbox.send_keys(Keys.ENTER) + time.sleep(3) + self.check_for_row_in_list_table('1: Buy peacock feathers') + + # There is still a text box inviting her to add another item. She + # enters "Use peacock feathers to make a fly" (Edith is very + # methodical) + inputbox = self.browser.find_element_by_id('id_new_item') + inputbox.send_keys('Use peacock feathers to make a fly') + inputbox.send_keys(Keys.ENTER) + time.sleep(3) + + # The page updates again, and now shows both items on her list + self.check_for_row_in_list_table('1: Buy peacock feathers') + self.check_for_row_in_list_table('2: Use peacock feathers to make a fly') + + # Edith wonders whether the site will remember her list. Then she sees + # that the site has generated a unique URL for her -- there is some + # explanatory text to that effect. + #self.fail('Finish the test!') + + # She visits that URL - her to-do list is still there. +# Satisfied, she goes back to sleep + +if __name__ == '__main__': + unittest.main(warnings='ignore') diff --git a/lists/tests.py b/lists/tests.py index 732134819ca11563c0b123c5e17a2555f9e3b8ab..5c4ec10cbd27708305c8ac547f4c7869dab1f671 100644 --- a/lists/tests.py +++ b/lists/tests.py @@ -42,7 +42,7 @@ class HomePageTest(TestCase): def test_home_page_returns_correct_html(self): request = HttpRequest() - response = home_page(request) + response = self.client.get('/') html = response.content.decode('utf8') self.assertTrue(html.startswith('')) self.assertIn('