From 99e9b4cef91772acc4c761663be1aa08e248bf72 Mon Sep 17 00:00:00 2001 From: Farah Alhaniy Date: Thu, 3 Oct 2019 14:46:32 +0700 Subject: [PATCH 1/3] Fix branch to match master --- .gitignore | 2 +- functional_tests.py | 22 ++++++++++----- lists/tests.py | 5 ++-- requirements.txt | 65 ++++++--------------------------------------- 4 files changed, 27 insertions(+), 67 deletions(-) diff --git a/.gitignore b/.gitignore index e47e373..3c416e9 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 c3f17ed..5685973 100644 --- a/functional_tests.py +++ b/functional_tests.py @@ -1,12 +1,21 @@ 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): - 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 +28,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' @@ -60,10 +70,10 @@ class NewVisitorTest(unittest.TestCase): # 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 if __name__ == '__main__': - unittest.main(warnings='ignore') \ No newline at end of file + unittest.main(warnings='ignore') diff --git a/lists/tests.py b/lists/tests.py index 7321348..5c4ec10 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('Homepage - Farah Alhaniy', html) @@ -61,7 +61,7 @@ class HomePageTest(TestCase): def test_comment_todolist_empty(self): response = self.client.get('/') - self.assertIn('Yey, waktunya libur', response.content.decode()) + self.assertIn('Yey, waktunya berlibur', response.content.decode()) def test_comment_todolist_less_than_5(self): for i in range(3): @@ -77,7 +77,6 @@ class HomePageTest(TestCase): response = self.client.get('/') self.assertIn('Oh tidak', response.content.decode()) - class ItemModelTest(TestCase): def test_saving_and_retrieving_items(self): diff --git a/requirements.txt b/requirements.txt index 6cb2028..f845286 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,59 +1,10 @@ -astroid==1.5.3 -attrs==19.1.0 -bleach==3.1.0 -certifi==2018.8.24 -chardet==3.0.4 -colorama==0.3.9 -coverage==4.4.1 -decorator==4.4.0 -defusedxml==0.5.0 -dj-database-url==0.4.2 -Django==2.1.1 +dj-database-url==0.5.0 +Django==2.2.5 django-environ==0.4.4 -entrypoints==0.3 -flake8==3.4.1 -graphviz==0.10.1 -gunicorn==19.7.1 -idna==2.6 -ipython-genutils==0.2.0 -isort==4.2.15 -Jinja2==2.10 -jsonschema==3.0.1 -jupyter-core==4.4.0 -lazy-object-proxy==1.3.1 -MarkupSafe==1.1.1 -mccabe==0.6.1 -mistune==0.8.4 -nbconvert==5.4.1 -nbformat==4.4.0 -numpy==1.15.4 -pandas==0.23.4 -pandocfilters==1.4.2 -pew==1.0.2 -Pillow==5.3.0 -pipenv==8.2.6 -ply==3.11 -psutil==5.3.1 +gunicorn==19.9.0 +pytz==2018.9 psycopg2==2.7.3.1 -pycodestyle==2.3.1 -pydotplus==2.0.2 -pyflakes==1.5.0 -Pygments==2.3.1 -pylint==1.7.2 -pyparsing==2.3.1 -pyrsistent==0.14.11 -python-dateutil==2.7.5 -pytz==2017.2 -requests==2.18.4 -selenium==3.5.0 -six==1.10.0 -testpath==0.4.2 -traitlets==4.3.2 -typed-ast==1.1.0 -urllib3==1.22 -virtualenv==15.1.0 -virtualenv-clone==0.2.6 -webencodings==0.5.1 -whitenoise==3.3.0 -wrapt==1.10.11 -xlrd==1.1.0 +selenium==3.141.0 +sqlparse==0.3.0 +urllib3==1.25.3 +whitenoise==4.1.3 \ No newline at end of file -- GitLab From 5808ad7cdb848b3288e23cf71b98dede1c95bbb9 Mon Sep 17 00:00:00 2001 From: Farah Alhaniy Date: Thu, 3 Oct 2019 14:59:41 +0700 Subject: [PATCH 2/3] Make functional_tests an app, use LiveServerTestCase --- functional_tests.py | 14 +++++-- functional_tests/__init__.py | 1 + functional_tests/tests.py | 79 ++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 functional_tests/__init__.py create mode 100644 functional_tests/tests.py diff --git a/functional_tests.py b/functional_tests.py index 5685973..13f81c8 100644 --- a/functional_tests.py +++ b/functional_tests.py @@ -1,3 +1,4 @@ +from django.test import LiveServerTestCase from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.firefox.options import Options @@ -9,7 +10,7 @@ env = environ.Env(DEBUG=(bool, False)) environ.Env.read_env('.env') -class NewVisitorTest(unittest.TestCase): +class NewVisitorTest(LiveServerTestCase): def setUp(self): options = Options() @@ -73,7 +74,12 @@ class NewVisitorTest(unittest.TestCase): #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 -if __name__ == '__main__': - unittest.main(warnings='ignore') + 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) + +#if __name__ == '__main__': +# unittest.main(warnings='ignore') diff --git a/functional_tests/__init__.py b/functional_tests/__init__.py new file mode 100644 index 0000000..8b13789 --- /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 0000000..5685973 --- /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') -- GitLab From 4bcad0075a4b74fa3d088ca0e3560add93e33c8a Mon Sep 17 00:00:00 2001 From: Farah Alhaniy Date: Thu, 3 Oct 2019 16:29:10 +0700 Subject: [PATCH 3/3] Replace sleep with wait function --- functional_tests.py | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/functional_tests.py b/functional_tests.py index 13f81c8..7caeb80 100644 --- a/functional_tests.py +++ b/functional_tests.py @@ -1,3 +1,4 @@ +from selenium.common.exceptions import WebDriverException from django.test import LiveServerTestCase from selenium import webdriver from selenium.webdriver.common.keys import Keys @@ -6,6 +7,8 @@ import time import unittest import environ +MAX_WAIT = 10 + env = environ.Env(DEBUG=(bool, False)) environ.Env.read_env('.env') @@ -53,20 +56,18 @@ class NewVisitorTest(LiveServerTestCase): # 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 @@ -81,5 +82,18 @@ class NewVisitorTest(LiveServerTestCase): # 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') -- GitLab