Fakultas Ilmu Komputer UI

base.py 1.45 KB
Newer Older
1
2
3
4
5
6
7
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
import time

MAX_WAIT = 10

Syahrul Findi's avatar
Syahrul Findi committed
8
9
10
11
12
13
14
15
16
17
18
19
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 
20
21
22
23
24
25
26
27

class FunctionalTest(StaticLiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()

    def tearDown(self):
        self.browser.quit()

Syahrul Findi's avatar
Syahrul Findi committed
28
    @wait
29
    def wait_for_row_in_list_table(self, row_text):
Syahrul Findi's avatar
Syahrul Findi committed
30
31
32
        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])
33
    
Syahrul Findi's avatar
Syahrul Findi committed
34
    @wait
35
    def wait_for(self, fn):
Syahrul Findi's avatar
Syahrul Findi committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
        return fn()

    @wait
    def wait_to_be_logged_in(self, email):
        self.browser.find_element_by_link_text('Log out')
        navbar = self.browser.find_element_by_css_selector('.navbar')
        self.assertIn(email, navbar.text)
        
    @wait
    def wait_to_be_logged_out(self, email):
        self.browser.find_element_by_name('email')
        navbar = self.browser.find_element_by_css_selector('.navbar')
        self.assertNotIn(email, navbar.text)