Compare commits
7 Commits
49c79f3bbe
...
main
Author | SHA1 | Date | |
---|---|---|---|
5cedbfe9ce | |||
ceb22cdf90 | |||
35019b3e4b | |||
5157f05996 | |||
1e2cc408de | |||
285725ffdd | |||
e373ec5d69 |
@@ -1,5 +1,8 @@
|
||||
import unittest
|
||||
import time
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.common.by import By
|
||||
from selenium.webdriver.common.keys import Keys
|
||||
|
||||
class NewVisitorTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
@@ -12,22 +15,47 @@ class NewVisitorTest(unittest.TestCase):
|
||||
# Ig has heard about a cool new online to-do app.
|
||||
# He goes to check out its homepage
|
||||
self.browser.get("http://localhost:8000")
|
||||
|
||||
# He 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)
|
||||
|
||||
# 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
|
||||
# (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
|
||||
# "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.
|
||||
# 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
|
||||
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
|
||||
|
||||
|
0
lists/__init__.py
Normal file
0
lists/__init__.py
Normal file
3
lists/admin.py
Normal file
3
lists/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
lists/apps.py
Normal file
6
lists/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ListsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'lists'
|
0
lists/migrations/__init__.py
Normal file
0
lists/migrations/__init__.py
Normal file
3
lists/models.py
Normal file
3
lists/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
15
lists/templates/home.html
Normal file
15
lists/templates/home.html
Normal 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
14
lists/tests.py
Normal 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
10
lists/views.py
Normal 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", "")},
|
||||
)
|
@@ -37,6 +37,7 @@ INSTALLED_APPS = [
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'lists',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
@@ -14,9 +14,9 @@ Including another URLconf
|
||||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import path
|
||||
from lists.views import home_page
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path("", home_page, name="home"),
|
||||
]
|
||||
|
Reference in New Issue
Block a user