From 2709ef08e8a791bb21e85a3bcf5b7bdcc4410140 Mon Sep 17 00:00:00 2001 From: jordan Date: Wed, 25 Sep 2019 19:12:14 +0700 Subject: [PATCH 1/2] Refactor home page view to use a template --- functional_tests.py | 41 ++++++++++++++++++++++++++++++++++----- lists/templates/home.html | 3 +++ lists/tests.py | 20 ++++++++++++++----- lists/views.py | 3 ++- superlists/settings.py | 1 + 5 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 lists/templates/home.html diff --git a/functional_tests.py b/functional_tests.py index 80415eb..da8d82e 100644 --- a/functional_tests.py +++ b/functional_tests.py @@ -1,4 +1,5 @@ from selenium import webdriver +from selenium.webdriver.common.keys import Keys import unittest @@ -8,15 +9,45 @@ class NewVisitorTest(unittest.TestCase): self.browser.implicitly_wait(3) def tearDown(self): - self.browser.close() + self.browser.quit() def test_can_start_a_list_and_retrieve_it_later(self): self.browser.get('http://localhost:8000') - self.assertIn('Rumah', self.browser.title) - h = self.browser.page_source - self.assertIn('Jordan Muhammad Andrianda', h) - self.assertIn('1506722765', h) + # She notices the page title and header mention to-do lists + #self.assertIn('Rumah', self.browser.title) + self.assertIn('To-Do', self.browser.title) + header_text = self.browser.find_element_by_tag_name('h1').header_text + self.assertIn('To-Do', 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) + + table = self.browser.find_element_by_id('id_list_table') + rows = table.find_elements_by_tag_name('tr') + self.assertTrue( + + any(row.text == '1: Buy peacock feathers' for row in rows) + ) + # 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) + self.fail('Finish the test!') + + #h = self.browser.page_source + #self.assertIn('Jordan Muhammad Andrianda', h) + #self.assertIn('1506722765', h) diff --git a/lists/templates/home.html b/lists/templates/home.html new file mode 100644 index 0000000..3b5216c --- /dev/null +++ b/lists/templates/home.html @@ -0,0 +1,3 @@ + + To-Do lists + diff --git a/lists/tests.py b/lists/tests.py index 4a7e493..b02f54c 100644 --- a/lists/tests.py +++ b/lists/tests.py @@ -2,6 +2,7 @@ from django.test import TestCase from django.urls import resolve from django.http import HttpRequest from lists.views import home_page +from django.template.loader import render_to_string # Create your tests here. class HomePageTest(TestCase): @@ -12,11 +13,20 @@ class HomePageTest(TestCase): def test_home_page_returns_correct_html(self): request = HttpRequest() # response = home_page(request) # - self.assertTrue(response.content.startswith(b'')) # - self.assertIn(b'Rumah', response.content) # - self.assertIn(b'

Jordan Muhammad Andrianda

', response.content) # - self.assertIn(b'

1506722765

', response.content) # - self.assertTrue(response.content.endswith(b'')) # + expected_html = render_to_string('home.html') + self.assertEqual(response.content.decode(), expected_html) + + #self.assertTrue(response.content.startswith(b'')) # + + #self.assertTrue(response.content.strip().endswith(b'')) # + #def test_home_page_returns_correct_html(self): + # request = HttpRequest() # + # response = home_page(request) # + # self.assertTrue(response.content.startswith(b'')) # + # self.assertIn(b'Rumah', response.content) # + # self.assertIn(b'

Jordan Muhammad Andrianda

', response.content) # + # self.assertIn(b'

1506722765

', response.content) # + # self.assertTrue(response.content.endswith(b'')) # diff --git a/lists/views.py b/lists/views.py index f7fb6da..8410f5b 100644 --- a/lists/views.py +++ b/lists/views.py @@ -2,4 +2,5 @@ from django.shortcuts import render from django.http import HttpResponse def home_page(request): - return HttpResponse('Rumah

Jordan Muhammad Andrianda

1506722765

') + #return HttpResponse('Rumah

Jordan Muhammad Andrianda

1506722765

') + return render(request,'home.html') diff --git a/superlists/settings.py b/superlists/settings.py index 22e2cbd..973d1ca 100644 --- a/superlists/settings.py +++ b/superlists/settings.py @@ -37,6 +37,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'lists', ] MIDDLEWARE = [ -- GitLab From a1e6e26da12124ef0906773a56011eaebcd2afcf Mon Sep 17 00:00:00 2001 From: jordan Date: Wed, 25 Sep 2019 19:23:30 +0700 Subject: [PATCH 2/2] Front page HTML now generated from a template --- functional_tests.py | 6 +++--- lists/templates/home.html | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/functional_tests.py b/functional_tests.py index da8d82e..f370240 100644 --- a/functional_tests.py +++ b/functional_tests.py @@ -15,9 +15,8 @@ class NewVisitorTest(unittest.TestCase): self.browser.get('http://localhost:8000') # She notices the page title and header mention to-do lists - #self.assertIn('Rumah', self.browser.title) self.assertIn('To-Do', self.browser.title) - header_text = self.browser.find_element_by_tag_name('h1').header_text + header_text = self.browser.find_element_by_tag_name('h1').text self.assertIn('To-Do', header_text) # She is invited to enter a to-do item straight away @@ -38,7 +37,8 @@ class NewVisitorTest(unittest.TestCase): rows = table.find_elements_by_tag_name('tr') self.assertTrue( - any(row.text == '1: Buy peacock feathers' for row in rows) + any(row.text == '1: Buy peacock feathers' for row in rows), + "New to-do item did not appear in table" ) # There is still a text box inviting her to add another item. She # enters "Use peacock feathers to make a fly" (Edith is very diff --git a/lists/templates/home.html b/lists/templates/home.html index 3b5216c..5474b01 100644 --- a/lists/templates/home.html +++ b/lists/templates/home.html @@ -1,3 +1,10 @@ - To-Do lists + + To-Do lists + + +

Your To-Do list

+ + + -- GitLab