Fakultas Ilmu Komputer UI

Commit 9266f968 authored by RANI LASMA ULI's avatar RANI LASMA ULI
Browse files

Testinggoat/ch12

Latihan 5 - Input Validation and Test Organizing
parent 47929a5c
......@@ -34,17 +34,22 @@ FunctionalTest:
image: python:3.6
stage: functionalTest
before_script:
- wget -qO- https://cli-assets.heroku.com/install-ubuntu.sh | sh
- export HEROKU_API_KEY=$HEROKU_APIKEY
- heroku pg:reset DATABASE --app $HEROKU_NAME --confirm $HEROKU_NAME || true
- heroku restart --app $HEROKU_NAME || true
- heroku run --app $HEROKU_NAME migrate || true
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
- echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list
- pip install -r requirements.txt
- python manage.py makemigrations
- python manage.py migrate
- apt-get update -qq && apt-get install -y -qq unzip
- apt-get install -y google-chrome-stable
- apt-get install -y xvfb
- wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/2.11/chromedriver_linux64.zip && unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/;
- wget https://chromedriver.storage.googleapis.com/77.0.3865.40/chromedriver_linux64.zip
- unzip chromedriver_linux64.zip
- pip install -r requirements.txt
when: on_success
script:
- python manage.py test functional_tests
except:
- master
after_script:
- heroku pg:reset DATABASE --app $HEROKU_NAME --confirm $HEROKU_NAME || true
- heroku run --app $HEROKU_NAME migrate || true
from selenium.common.exceptions import WebDriverException
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import os
import time
import unittest
from unittest import skip
MAX_WAIT = 10
class FunctionalTest(StaticLiveServerTestCase):
def setUp(self):
self.BASE_URL = os.environ.get("HEROKU_HOST", self.live_server_url)
self.options = Options()
self.options.add_argument('--dns-prefetch-disable')
self.options.add_argument('--no-sandbox')
self.options.add_argument('--headless')
self.options.add_argument('disable-gpu')
self.options.add_argument('--dns-prefetch-disable')
self.createBrowserInstance()
def createBrowserInstance(self):
try:
self.browser = webdriver.Chrome(options=self.options)
except WebDriverException:
self.browser = webdriver.Chrome('./chromedriver',
options=self.options)
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
def wait_for_row_in_list_table(self, row_text):
start_time = time.time()
while True:
try:
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])
return
except (AssertionError, WebDriverException) as e:
if time.time() - start_time > MAX_WAIT:
raise e
time.sleep(0.5)
def wait_for(self, fn):
start_time = time.time()
while True:
try:
return fn
except (AssertionError, WebDriverException) as e:
if time.time() - start_time > MAX_WAIT:
raise e
time.sleep(0.5)
\ No newline at end of file
from selenium.webdriver.common.keys import Keys
from .base import FunctionalTest
class LayoutAndStylingTest(FunctionalTest):
def test_layout_and_styling(self):
# goes to the homepage
self.browser.get(self.live_server_url)
self.browser.set_window_size(1024, 768)
# the input box centered
inputbox = self.browser.find_element_by_id('id_new_item')
self.assertAlmostEqual(inputbox.location['x'] + inputbox.size['width'] / 2, 512, delta=10)
\ No newline at end of file
from selenium.webdriver.common.keys import Keys
from unittest import skip
from .base import FunctionalTest
class ItemValidationTest(FunctionalTest):
def test_cannot_add_empty_list_items(self):
# Edith goes to the home page and accidentally tries to submit
# an empty list item. She hits Enter on the empty input box
self.browser.get(self.BASE_URL)
self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER)
# The home page refreshes, and there is an error message saying
# that list items cannot be blank
self.wait_for(lambda: self.assertEqual(
self.browser.find_element_by_css_selector('.has-error').text,
"You can't have an empty list item"
))
# She tries again with some text for the item, which now works
self.browser.find_element_by_id('id_new_item').send_keys('Buy milk')
self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER)
self.wait_for_row_in_list_table('1: Buy milk')
# Perversely, she now decides to submit a second blank list item
self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER)
# She receives a similar warning on the list page
self.wait_for(lambda: self.assertEqual(
self.browser.find_element_by_css_selector('.has-error').text,
"You can't have an empty list item"
))
# And she can correct it by filling some text in
self.browser.find_element_by_id('id_new_item').send_keys('Make tea')
self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER)
self.wait_for_row_in_list_table('1: Buy milk')
self.wait_for_row_in_list_table('2: Make tea')
\ No newline at end of file
from selenium.common.exceptions import WebDriverException
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from .base import FunctionalTest
import unittest
MAX_WAIT = 10
class NewVisitorTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
def tearDown(self):
self.browser.quit()
class NewVisitorTest(FunctionalTest):
def check_for_row_in_list_table(self, row_text):
table = self.browser.find_element_by_id('id_list_table')
......@@ -60,35 +52,4 @@ class NewVisitorTest(StaticLiveServerTestCase):
# methodical)
# self.fail('Finish the test!')
# The page updates again, and now shows both items on her list
def wait_for_row_in_list_table(self, row_text):
start_time = time.time()
while True:
try:
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])
return
except (AssertionError, WebDriverException) as e:
if time.time() - start_time > MAX_WAIT:
raise e
time.sleep(0.5)
def test_layout_and_styling(self):
# goes to the homepage
self.browser.get(self.live_server_url)
self.browser.set_window_size(1024, 768)
# the input box centered
inputbox = self.browser.find_element_by_id('id_new_item')
self.assertAlmostEqual(inputbox.location['x'] + inputbox.size['width'] / 2, 512, delta=10)
# She starts a new list and sees the input is nicely
# centered there too
inputbox.send_keys('testing')
inputbox.send_keys(Keys.ENTER)
self.wait_for_row_in_list_table('1: testing')
inputbox = self.browser.find_element_by_id('id_new_item')
self.assertAlmostEqual(inputbox.location['x'] + inputbox.size['width'] / 2, 512, delta=10)
\ No newline at end of file
# The page updates again, and now shows both items on her list
\ No newline at end of file
from django.db import models
class Item(models.Model):
text = models.TextField(default='')
\ No newline at end of file
text = models.TextField(blank=False)
\ No newline at end of file
......@@ -7,8 +7,10 @@
<tr><td>{{forloop.counter}}: {{ item.text }}</td></tr>
{% endfor %}
</table>
{% endblock %}
<section id="comment">
<h3>{{ comment }}</h3>
<h3>{{ comment }}</h3>
</section>
{% endblock %}
from django.urls import resolve
from django.test import TestCase
from django.http import HttpRequest
from django.template.loader import render_to_string
from lists.models import Item
from lists.views import index
TO_DO_LIST_URL = '/'
class ItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
first_item = Item()
first_item.text = 'The first (ever) list item'
first_item.save()
second_item = Item()
second_item.text = 'Item the second'
second_item.save()
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')
......@@ -7,7 +7,7 @@ from lists.models import Item
from lists.views import index
TO_DO_LIST_URL = '/'
class ExampleTest(TestCase):
class ListViewTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
found = resolve(TO_DO_LIST_URL)
......@@ -68,28 +68,7 @@ class ExampleTest(TestCase):
response = self.client.get(TO_DO_LIST_URL)
html = response.content.decode('utf8')
self.assertIn("oh tidak", html)
class ItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
first_item = Item()
first_item.text = 'The first (ever) list item'
first_item.save()
second_item = Item()
second_item.text = 'Item the second'
second_item.save()
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')
......
......@@ -4,7 +4,7 @@ from lists.models import Item
response = {}
def index(request):
if request.method == 'POST':
if request.method == 'POST' and request.POST['item_text'] != '':
Item.objects.create(text=request.POST['item_text'])
return redirect('/')
......@@ -18,6 +18,6 @@ def index(request):
return render(request, 'to-do-list.html', {
'items': items,
'comment':message,
'comment':message,
})
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