Fakultas Ilmu Komputer UI

Commit d48ddc7f authored by Syahrul Ardiansyah's avatar Syahrul Ardiansyah
Browse files

Use wait after adding item and explanation in readme

parent 9a8d9f94
Pipeline #21461 failed with stage
in 1 minute and 42 seconds
# Practice Repository # Practice Repository
Production URL: https://pmpl-1506757352-latihan1.herokuapp.com/ Production URL: https://pmpl-1506757352-latihan1.herokuapp.com/
## Latihan 3
Perbedaan dari test yang sudah dibuat adalah integration test yang baru akan menunggu sampai ada perubahan pada rows hingga 10 detik setelah selenium menambahkan item. Pengecekan dilakukan setiap 0.5 detik, jika saat lewat dari 10 detik fungsi wait_for_row_in_list_table tidak me-return, maka akan ada error yang di-raise. Dibandingkan dengan test yang lama, fungsi test akan melakukan sleep selama 1 detik dan belum tentu dalam 1 detik tersebut item berhasil ditambahkan.
Selain itu integration test sekarang sudah bisa me-listen request dari url liveserver yang sedang berjalan, tidak perlu ditentukan lagi url nya apa.
from selenium import webdriver from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
from django.test import LiveServerTestCase from django.test import LiveServerTestCase
import time import time
import os import os
MAX_WAIT = 10
class NewVisitorTest(LiveServerTestCase): class NewVisitorTest(LiveServerTestCase):
def setUp(self): def setUp(self):
...@@ -12,10 +15,18 @@ class NewVisitorTest(LiveServerTestCase): ...@@ -12,10 +15,18 @@ class NewVisitorTest(LiveServerTestCase):
def tearDown(self): def tearDown(self):
self.browser.quit() self.browser.quit()
def check_for_row_in_list_table(self, row_text): def wait_for_row_in_list_table(self, row_text):
table = self.browser.find_element_by_id('id_list_table') start_time = time.time()
rows = table.find_elements_by_tag_name('tr') while True:
self.assertIn(row_text, [row.text for row in rows]) 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_can_start_a_list_and_retrieve_it_later(self): def test_can_start_a_list_and_retrieve_it_later(self):
# Edith has heard about a cool new online to-do app. She goes # Edith has heard about a cool new online to-do app. She goes
...@@ -52,8 +63,7 @@ class NewVisitorTest(LiveServerTestCase): ...@@ -52,8 +63,7 @@ class NewVisitorTest(LiveServerTestCase):
# When she hits enter, the page updates, and now the page lists # 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 # "1: Buy peacock feathers" as an item in a to-do list table
inputbox.send_keys(Keys.ENTER) inputbox.send_keys(Keys.ENTER)
time.sleep(1) self.wait_for_row_in_list_table('1: Buy peacock feathers')
self.check_for_row_in_list_table('1: Buy peacock feathers')
# There is still a text box inviting her to add another item. She # There is still a text box inviting her to add another item. She
# enters "Use peacock feathers to make a fly" (Edith is very # enters "Use peacock feathers to make a fly" (Edith is very
...@@ -61,11 +71,10 @@ class NewVisitorTest(LiveServerTestCase): ...@@ -61,11 +71,10 @@ class NewVisitorTest(LiveServerTestCase):
inputbox = self.browser.find_element_by_id('id_new_item') inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Use peacock feathers to make a fly') inputbox.send_keys('Use peacock feathers to make a fly')
inputbox.send_keys(Keys.ENTER) inputbox.send_keys(Keys.ENTER)
time.sleep(1)
# The page updates again, and now shows both items on her list # The page updates again, and now shows both items on her list
self.check_for_row_in_list_table('1: Buy peacock feathers') self.wait_for_row_in_list_table('1: Buy peacock feathers')
self.check_for_row_in_list_table( self.wait_for_row_in_list_table(
'2: Use peacock feathers to make a fly') '2: Use peacock feathers to make a fly')
# She check the comment when there is few items # She check the comment when there is few items
......
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