Compare commits

...

7 Commits

11 changed files with 83 additions and 3 deletions

View File

@@ -1,5 +1,8 @@
import unittest import unittest
import time
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
class NewVisitorTest(unittest.TestCase): class NewVisitorTest(unittest.TestCase):
def setUp(self): def setUp(self):
@@ -12,22 +15,47 @@ class NewVisitorTest(unittest.TestCase):
# Ig has heard about a cool new online to-do app. # Ig has heard about a cool new online to-do app.
# He goes to check out its homepage # He goes to check out its homepage
self.browser.get("http://localhost:8000") self.browser.get("http://localhost:8000")
# He notices the page title and header mention to-do lists # He notices the page title and header mention to-do lists
self.assertIn("To-Do", self.browser.title) self.assertIn("To-Do", self.browser.title)
header_text = self.browser.find_element(By.TAG_NAME, "h1").text
self.assertIn("To-Do", header_text)
# He is invited to enter a to-do item straight away # He is invited to enter a to-do item straight away
self.fail("Finish the test!") inputbox = self.browser.find_element(By.ID, "id_new_item")
self.assertEqual(inputbox.get_attribute("placeholder"), "Enter a to-do item")
# He types "Buy peacock feathers" into a text box # He types "Buy peacock feathers" into a text box
# (Ig's hobby is tying fly-fishing lures) # (Ig's hobby is tying fly-fishing lures)
inputbox.send_keys("Buy peacock feathers")
# When he hits enter, the page updates, and now the page lists # When he hits enter, the page updates, and now the page lists
# "1: Buy peacock feathers" as an item in a to-do list # "1: Buy peacock feathers" as an item in a to-do list
inputbox.send_keys(Keys.ENTER)
time.sleep(1)
table = self.browser.find_element(By.ID, "id_list_table")
rows = table.find_elements(By.TAG_NAME, "tr")
self.assertIn("1: Buy peacock feathers", [row.text for row in rows])
# There is still a text box inviting him to add another item. # There is still a text box inviting him to add another item.
# He enters "Use peacock feathers to make a fly" (Ig is very methodical) # He enters "Use peacock feathers to make a fly" (Ig 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(1)
# The page updates again, and now shows both items on his list # The page updates again, and now shows both items on his list
table = self.browser.find_element(By.ID, "id_list_table")
rows = table.find_elements(By.TAG_NAME, "tr")
self.assertIn(
"1: Buy peacock feathers",
[row.text for row in rows],
)
self.assertIn(
"2: Use peacock feather sto make a fly",
[row.text for row in rows],
)
# Satisfied, he goes back to sleep # Satisfied, he goes back to sleep

0
lists/__init__.py Normal file
View File

3
lists/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
lists/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class ListsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'lists'

View File

3
lists/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

15
lists/templates/home.html Normal file
View File

@@ -0,0 +1,15 @@
<html>
<head>
<title>To-Do lists</title>
</head>
<body>
<h1>Your To-Do list</h1>
<form method="POST">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
{% csrf_token %}
</form>
<table id="id_list_table">
<tr><td>1: {{ new_item_text }}</td></tr>
</table>
</body>
</html>

14
lists/tests.py Normal file
View File

@@ -0,0 +1,14 @@
from django.test import TestCase
from django.http import HttpRequest
from lists.views import home_page
# Create your tests here.
class HomePageTest(TestCase):
def test_uses_home_page_template(self):
response = self.client.get("/")
self.assertTemplateUsed(response, "home.html")
def test_can_save_a_POST_request(self):
response = self.client.post("/", data={"item_text": "A new list item"})
self.assertContains(response, "A new list item")
self.assertTemplateUsed(response, "home.html")

10
lists/views.py Normal file
View File

@@ -0,0 +1,10 @@
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_page(request):
return render(
request,
"home.html",
{"new_item_text": request.POST.get("item_text", "")},
)

View File

@@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'lists',
] ]
MIDDLEWARE = [ MIDDLEWARE = [

View File

@@ -14,9 +14,9 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path 1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin
from django.urls import path from django.urls import path
from lists.views import home_page
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path("", home_page, name="home"),
] ]