Fakultas Ilmu Komputer UI

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

Complete wait decorator

parent 3d46de1e
Pipeline #26241 passed with stage
in 1 minute and 46 seconds
...@@ -49,7 +49,7 @@ class SendLoginEmailViewTest(TestCase): ...@@ -49,7 +49,7 @@ class SendLoginEmailViewTest(TestCase):
'email': 'edith@example.com' 'email': 'edith@example.com'
}) })
token = Token.objects.first() token = Token.objects.first()
expected_url = f'http://testserver/accounts/login?token={token.uid}' expected_url = 'http://testserver/accounts/login?token=%s' % token.uid
(subject, body, from_email, to_list), kwargs = mock_send_mail.call_args (subject, body, from_email, to_list), kwargs = mock_send_mail.call_args
self.assertIn(expected_url, body) self.assertIn(expected_url, body)
......
...@@ -5,6 +5,18 @@ import time ...@@ -5,6 +5,18 @@ import time
MAX_WAIT = 10 MAX_WAIT = 10
def wait(fn):
def modified_fn(*args, **kwargs):
start_time = time.time()
while True:
try:
return fn(*args, **kwargs)
except (AssertionError, WebDriverException) as e:
if time.time() - start_time > MAX_WAIT:
raise e
time.sleep(0.5)
return modified_fn
class FunctionalTest(StaticLiveServerTestCase): class FunctionalTest(StaticLiveServerTestCase):
def setUp(self): def setUp(self):
...@@ -13,39 +25,25 @@ class FunctionalTest(StaticLiveServerTestCase): ...@@ -13,39 +25,25 @@ class FunctionalTest(StaticLiveServerTestCase):
def tearDown(self): def tearDown(self):
self.browser.quit() self.browser.quit()
@wait
def wait_for_row_in_list_table(self, row_text): def wait_for_row_in_list_table(self, row_text):
start_time = time.time() table = self.browser.find_element_by_id('id_list_table')
while True: rows = table.find_elements_by_tag_name('tr')
try: self.assertIn(row_text, [row.text for row in rows])
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)
@wait
def wait_for(self, fn): def wait_for(self, fn):
start_time = time.time() return fn()
while True:
try:
return fn()
except (AssertionError, WebDriverException) as e:
if time.time() - start_time > MAX_WAIT:
raise e
time.sleep(0.5)
@wait
def wait_to_be_logged_in(self, email): def wait_to_be_logged_in(self, email):
self.wait_for( self.browser.find_element_by_link_text('Log out')
lambda: self.browser.find_element_by_link_text('Log out')
)
navbar = self.browser.find_element_by_css_selector('.navbar') navbar = self.browser.find_element_by_css_selector('.navbar')
self.assertIn(email, navbar.text) self.assertIn(email, navbar.text)
@wait
def wait_to_be_logged_out(self, email): def wait_to_be_logged_out(self, email):
self.wait_for( self.browser.find_element_by_name('email')
lambda: self.browser.find_element_by_name('email')
)
navbar = self.browser.find_element_by_css_selector('.navbar') navbar = self.browser.find_element_by_css_selector('.navbar')
self.assertNotIn(email, navbar.text) self.assertNotIn(email, navbar.text)
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