Fakultas Ilmu Komputer UI

Commit 1475948e authored by Rayza Arasj Mahardhika's avatar Rayza Arasj Mahardhika
Browse files

Exercise 2

parent ee53307a
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import unittest
class NewVisitorTest(unittest.TestCase):
......@@ -10,15 +12,57 @@ class NewVisitorTest(unittest.TestCase):
def tearDown(self):
def test_can_access_homepage(self):
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
self.assertIn('Rayza\'s Homepage', self.browser.title)
# She notices the page title and header meantion 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)
# She is invited to enter a to-do item straight away
inputbox = self.browser.find_element_by_id('id_new_item')
'Enter a to-do item'
# She types "Buy peacock feathers" into a text box (Edhits' 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
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')
def test_can_access_about_page(self):
# 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')
self.assertIn('Rayza\'s About Page', self.browser.title)
# 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.
# She visits that URL - her to-do list is still there.
# Satisfied, she goes back to sleep
if __name__ == '__main__':
# Generated by Django 2.2.5 on 2019-09-25 15:01
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
operations = [
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
# Generated by Django 2.2.5 on 2019-09-25 15:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('lists', '0001_initial'),
operations = [
from django.db import models
# Create your models here.
class Item(models.Model):
text = models.TextField(default='')
<title>To-Do lists</title>
<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 %}
<table id="id_list_table">
{% for item in items %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
......@@ -3,6 +3,7 @@ from django.test import TestCase
from django.http import HttpRequest
from lists.views import home_page, about_page
from lists.models import Item
class HomePageTest(TestCase):
......@@ -10,14 +11,35 @@ class HomePageTest(TestCase):
found = resolve('/')
self.assertEqual(found.func, home_page)
def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
html = response.content.decode('utf8')
self.assertIn('<title>Rayza\'s Homepage</title>', html)
self.assertIn('<h1>Hello, my name is Rayza Arasj Mahardhika</h1>', html)
def test_uses_home_template(self):
response = self.client.get('/')
self.assertTemplateUsed(response, 'home.html')
def test_can_save_a_POST_request(self):
self.client.post('/', data={'item_text': 'A new list item'})
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.first()
self.assertEqual(new_item.text, 'A new list item')
def test_redirects_after_POST(self):
response = self.client.post('/', data={'item_text': 'A new list item'})
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/')
def test_only_saves_items_when_necessary(self):
self.assertEqual(Item.objects.count(), 0)
def test_displays_all_list_items(self):
Item.objects.create(text='itemey 1')
Item.objects.create(text='itemey 2')
response = self.client.get('/')
self.assertIn('itemey 1', response.content.decode())
self.assertIn('itemey 2', response.content.decode())
class AboutPageTest(TestCase):
......@@ -36,3 +58,22 @@ class AboutPageTest(TestCase):
self.assertIn('<p>I\'m from Bogor</p>', html)
self.assertIn('<p>I\'m currently studying Computer Science at Faculty of Computer Science, Universitas Indonesia</p>', html)
class ItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
first_item = Item()
first_item.text = 'The first (ever) list item'
second_item = Item()
second_item.text = 'Item the second'
saved_items = Item.objects.all()
self.assertEqual(saved_items.count(), 2)
first_saved_item = saved_items[0]
second_saved_item = saved_items[1]
self.assertEqual(first_saved_item.text, 'The first (ever) list item')
self.assertEqual(second_saved_item.text, 'Item the second')
from django.http import HttpResponse
from django.shortcuts import redirect, render
from lists.models import Item
def home_page(request):
return HttpResponse(
'<html>' +
'<title>Rayza\'s Homepage</title>' +
'<h1>Hello, my name is Rayza Arasj Mahardhika</h1>' +
if request.method == 'POST':
return redirect('/')
items = Item.objects.all()
return render(request, 'home.html', {'items': items})
def about_page(request):
return HttpResponse(
......@@ -26,7 +26,8 @@ SECRET_KEY = '-c!mdiysslpd4fvwjvju&y3s93r_xiu2!&w(moz0byj3n)nx)7'
DEBUG = True
......@@ -39,6 +40,7 @@ INSTALLED_APPS = [
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment