Fakultas Ilmu Komputer UI

Commit 331fc450 authored by Jordan Muhammad Andrianda's avatar Jordan Muhammad Andrianda
Browse files

Merge branch 'latihan5' into 'master'

Latihan5

See merge request !9
parents abf5d7b0 87c64235
{
"folders": [
{
"path": "/Users/jonsnow/Documents/Tugas/Semester-9/pmpl/kape"
},
{
"path": "."
}
]
}
\ No newline at end of file
import os
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
import time
MAX_WAIT = 10
class FunctionalTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
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 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)
from selenium.webdriver.common.keys import Keys
from .base import FunctionalTest
class LayoutAndStylingTest(FunctionalTest):
def test_layout_and_styling(self):
# Edith goes to the home page
self.browser.get(self.live_server_url)
self.browser.set_window_size(1024, 768)
# She notices the input box is nicely centered
inputbox = self.browser.find_element_by_id("id_new_item")
self.assertAlmostEqual(
inputbox.location["x"] + inputbox.size["width"] / 2, 512, delta=5
)
# She starts a new list and sees the input is nicely
# centered there too
inputbox.send_keys("testing\n")
inputbox = self.browser.find_element_by_id("id_new_item")
self.assertAlmostEqual(
inputbox.location["x"] + inputbox.size["width"] / 2, 512, delta=5
)
if __name__ == "__main__":
unittest.main(warnings="ignore")
from .base import FunctionalTest
from selenium.webdriver.common.keys import Keys
from unittest import skip
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.live_server_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')
if __name__ == "__main__":
unittest.main(warnings="ignore")
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from .base import FunctionalTest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
import time
class NewVisitorTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
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])
class NewVisitorTest(FunctionalTest):
def test_can_start_a_list_and_retrieve_it_later(self):
self.browser.get(self.live_server_url)
self.browser.get(self.live_server_url)
# She 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
......@@ -96,22 +85,7 @@ class NewVisitorTest(StaticLiveServerTestCase):
# self.assertIn('Jordan Muhammad Andrianda', h)
# self.assertIn('1506722765', h)
def test_layout_and_styling(self):
# Edith goes to the home page
self.browser.get(self.live_server_url)
self.browser.set_window_size(1024, 768)
# She notices the input box is nicely centered
inputbox = self.browser.find_element_by_id("id_new_item")
self.assertAlmostEqual(
inputbox.location["x"] + inputbox.size["width"] / 2, 512, delta=5
)
# She starts a new list and sees the input is nicely
# centered there too
inputbox.send_keys("testing\n")
inputbox = self.browser.find_element_by_id("id_new_item")
self.assertAlmostEqual(
inputbox.location["x"] + inputbox.size["width"] / 2, 512, delta=5
)
if __name__ == "__main__":
......
from django.test import TestCase
from lists.models import Item, List
class ListAndItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
list_ = List()
list_.save()
first_item = Item()
first_item.text = "The first (ever) list item"
first_item.list = list_
first_item.save()
second_item = Item()
second_item.text = "Item the second"
second_item.list = list_
second_item.save()
saved_list = List.objects.first()
self.assertEqual(saved_list, list_)
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(first_saved_item.list, list_)
self.assertEqual(second_saved_item.text, "Item the second")
self.assertEqual(second_saved_item.list, list_)
......@@ -14,21 +14,21 @@ class HomePageTest(TestCase):
self.assertEqual(found.func, home_page)
# "<input type='hidden' name='csrfmiddlewaretoken' value='%' />" %csrf_string
def test_home_page_returns_correct_html(self):
request = HttpRequest() #
response = home_page(request) #
#def test_home_page_returns_correct_html(self):
# request = HttpRequest() #
# response = home_page(request) #
# expected_html = render_to_string('home.html')
# self.assertEqual(response.content.decode(), expected_html)
self.assertTrue(response.content.startswith(b"<html>")) #
self.assertIn(b"<h1>Jordan M A</h1>", response.content)
self.assertIn(b"<h2>1506722765</h2>", response.content)
self.assertTrue(response.content.strip().endswith(b"</html>")) #
# self.assertTrue(response.content.startswith(b"<html>")) #
# self.assertIn(b"<h1>Jordan M A</h1>", response.content)
# self.assertIn(b"<h2>1506722765</h2>", response.content)
# self.assertTrue(response.content.strip().endswith(b"</html>")) #
def test_home_page_right_comments_when_zero(self):
request = HttpRequest()
response = home_page(request)
self.assertIn(b'<h1 id="comment">yey, waktunya berlibur</h1>', response.content)
#def test_home_page_right_comments_when_zero(self):
# request = HttpRequest()
# response = home_page(request)
# self.assertIn(b'<h1 id="comment">yey, waktunya berlibur</h1>', response.content)
# def test_home_page_right_comments_when_less_then_five(self):
# Item.objects.create(text="itemey 1")
......@@ -53,35 +53,6 @@ class HomePageTest(TestCase):
# response.content)
class ListAndItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
list_ = List()
list_.save()
first_item = Item()
first_item.text = "The first (ever) list item"
first_item.list = list_
first_item.save()
second_item = Item()
second_item.text = "Item the second"
second_item.list = list_
second_item.save()
saved_list = List.objects.first()
self.assertEqual(saved_list, list_)
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(first_saved_item.list, list_)
self.assertEqual(second_saved_item.text, "Item the second")
self.assertEqual(second_saved_item.list, list_)
class NewListTest(TestCase):
def test_saving_a_POST_request(self):
self.client.post("/lists/new", data={"item_text": "A new list item"})
......
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