Fakultas Ilmu Komputer UI

tests.py 7.67 KB
Newer Older
1
from selenium import webdriver
2
3
# from django.test import LiveServerTestCase
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
4
from selenium.webdriver.common.keys import Keys
5
from selenium.webdriver.chrome.options import Options
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
6
from selenium.common.exceptions import WebDriverException
7
import unittest
8
9
import time

LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
10
MAX_WAIT = 10  
11

12
class NewVisitorTest(StaticLiveServerTestCase):
13

14
    def setUp(self):  
15
16
17
        chrome_options = Options()
        chrome_options.add_argument('--dns-prefetch-disable')
        chrome_options.add_argument('--no-sandbox')
18
        chrome_options.add_argument('--headless')
19
20
        chrome_options.add_argument('disable-gpu')
        self.browser = webdriver.Chrome(executable_path="./chromedriver", options=chrome_options)
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
21
        print(self.live_server_url)
22

23
24
    def tearDown(self):  
        self.browser.quit()
25

LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
26
27
28
29
30
31
32
33
34
35
36
37
    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) 
38

39
40
    def test_can_view_homepage(self):  
        # Luthfi has heard about a cool homepageg app. He goes
41
        # to check out its homepage
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
42
        self.browser.get(self.live_server_url)
43

44
        # He notices the page title and header mention
45
46
        self.assertIn('Homepage', self.browser.title)  

47
        # He notices the name of owner page
48
49
50
        header_text = self.browser.find_element_by_tag_name('h1').text  
        self.assertIn('Luthfi Dzaky Saifuddin', header_text)

51
        # He notices the university of the owner page
52
53
        id_text = self.browser.find_element_by_id('university').text  
        self.assertIn('Universitas Indonesia', id_text)
54

55
56
57
58
59
60
61
62
63
64
65
    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=10
Luthfi Dzaky Saifuddin's avatar
Luthfi Dzaky Saifuddin committed
66
67
68
69
70
71
72
73
        )

        # She notices the card is centered
        card = self.browser.find_element_by_id('card-luthfi')
        self.assertAlmostEqual(
            card.location['x'] + card.size['width'] / 2,
            512,
            delta=10
74
75
        )

76
77
78
79
80
81
82
83
84
85
86
87
        # 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
        )

88
89
90
    def test_can_start_a_list_and_retrieve_it_later(self):
        # Edith has heard about a cool new online to-do app. She goes
        # to check out its homepage
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
91
        self.browser.get(self.live_server_url)
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
92

93
94
        # She notices the page title and header mention to-do lists
        self.assertIn('Homepage', self.browser.title)
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
95
        header_text = self.browser.find_element_by_tag_name('h1').text  
96
        self.assertIn('Luthfi Dzaky Saifuddin', header_text)
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
97

98
99
100
101
        # She notices comment in todo
        comment = self.browser.find_element_by_id('comment-todo').text
        self.assertIn('yey, waktunya berlibur', comment)

102
        # She is invited to enter a to-do item straight away
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
103
        inputbox = self.browser.find_element_by_id('id_new_item')  
104
105
106
107
        self.assertEqual(
            inputbox.get_attribute('placeholder'),
            'Enter a to-do item'
        )
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
108

109
110
        # She types "Buy peacock feathers" into a text box (Edith's hobby
        # is tying fly-fishing lures)
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
111
        inputbox.send_keys('Buy peacock feathers')  
112
113
114

        # 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
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
115
        inputbox.send_keys(Keys.ENTER)  
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
116
        self.wait_for_row_in_list_table('1: Buy peacock feathers')
117

118
119
120
121
        # She notices comment is changed
        comment = self.browser.find_element_by_id('comment-todo').text
        self.assertIn('sibuk tapi santai', comment)

122
        # There is still a text box inviting her to add another item. She
123
124
        # enters "Use peacock feathers to make a fly" (Edith is very
        # methodical)
125
126
127
        inputbox = self.browser.find_element_by_id('id_new_item')
        inputbox.send_keys('Use peacock feathers to make a fly')
        inputbox.send_keys(Keys.ENTER)
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
128
    
129
        # The page updates again, and now shows both items on her list
LUTHFI DZAKY SAIFUDDIN's avatar
LUTHFI DZAKY SAIFUDDIN committed
130
        self.wait_for_row_in_list_table('2: Use peacock feathers to make a fly')
Luthfi Dzaky Saifuddin's avatar
Luthfi Dzaky Saifuddin committed
131
        self.wait_for_row_in_list_table('1: Buy peacock feathers')
132

133
134
135
        # She want to add another 3 to do
        for x in range(3):
            inputbox = self.browser.find_element_by_id('id_new_item')
136
            inputbox.send_keys('To Do')
137
138
139
140
            inputbox.send_keys(Keys.ENTER)

        # She notices when to do is greater or equal than five the comment is change    
        comment = self.browser.find_element_by_id('comment-todo').text
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
        self.assertIn('oh tidak', comment)

    def test_multiple_users_can_start_lists_at_different_urls(self):
        # Edith starts a new to-do list
        self.browser.get(self.live_server_url)
        inputbox = self.browser.find_element_by_id('id_new_item')
        inputbox.send_keys('Buy peacock feathers')
        inputbox.send_keys(Keys.ENTER)
        self.wait_for_row_in_list_table('1: Buy peacock feathers')

        # She notices that her list has a unique URL
        edith_list_url = self.browser.current_url
        self.assertRegex(edith_list_url, '/lists/.+')

        # Now a new user, Francis, comes along to the site.

        ## We use a new browser session to make sure that no information
        ## of Edith's is coming through from cookies etc
        self.browser.quit()
        chrome_options = Options()
        chrome_options.add_argument('--dns-prefetch-disable')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('disable-gpu')
        self.browser = webdriver.Chrome(executable_path="./chromedriver", options=chrome_options)

        # Francis visits the home page.  There is no sign of Edith's
        # list
        self.browser.get(self.live_server_url)
        page_text = self.browser.find_element_by_tag_name('body').text
        self.assertNotIn('Buy peacock feathers', page_text)
        self.assertNotIn('make a fly', page_text)

        # Francis starts a new list by entering a new item. He
        # is less interesting than Edith...
        inputbox = self.browser.find_element_by_id('id_new_item')
        inputbox.send_keys('Buy milk')
        inputbox.send_keys(Keys.ENTER)
        self.wait_for_row_in_list_table('1: Buy milk')

        # Francis gets his own unique URL
        francis_list_url = self.browser.current_url
        self.assertRegex(francis_list_url, '/lists/.+')
        self.assertNotEqual(francis_list_url, edith_list_url)

        # Again, there is no trace of Edith's list
        page_text = self.browser.find_element_by_tag_name('body').text
        self.assertNotIn('Buy peacock feathers', page_text)
        self.assertIn('Buy milk', page_text)

        # Satisfied, they both go back to sleep