5.3.1 An Unexpected Failure

chap5
Jason Zhu 2020-11-08 20:39:30 +11:00
parent c0661056cf
commit 892cdbf541
4 changed files with 16 additions and 10 deletions

View File

@ -38,16 +38,24 @@ class NewVisitorTest(unittest.TestCase):
time.sleep(1)
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_element_by_tag_name('tr')
self.assertTrue(
any(row.text == '1: Buy peacock feathers' for row in rows),
"New to-do item did not appear in table"
)
# self.assertTrue('1: Buy peacock feathers', [row.text 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!')
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 her list
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_element_by_tag_name('tr')
print(rows)
self.assertIn('1: Buy peacock feathers', [row.text for row in rows])
self.assertIn(
'2: Use peacock feathers to make a fly',
[row.text for row in rows]
)
# 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

View File

@ -10,7 +10,7 @@
</form>
<table id="id_list_table">
<tr><td>{{ new_item_text }}</td></tr>
<tr><td>1: {{ new_item_text }}</td></tr>
</table>
</body>
</html>

View File

@ -18,6 +18,4 @@ class HomePageTest(TestCase):
def test_can_save_a_POST_request(self):
response = self.client.post('/', data={'item_text': 'A new list item'})
self.assertIn('A new list item', response.content.decode())
self.assertTemplateUsed(response, 'home.html')
self.assertTemplateUsed(response, 'home.html')

View File

@ -5,5 +5,5 @@ from django.shortcuts import render
# Create your views here
def home_page(request):
return render(request=request, template_name='home.html', context={
'new_item_text': request.POST['item_text'],
'new_item_text': request.POST.get('item_text', ''),
})