Fakultas Ilmu Komputer UI

tests.py 4.68 KB
Newer Older
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
1
from selenium import webdriver
2
from selenium.common.exceptions import WebDriverException
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
3
from selenium.webdriver.common.keys import Keys
4
from django.test import LiveServerTestCase
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
5
import time
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
6
import os
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
7

8
9
MAX_WAIT = 10

Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
10

11
class NewVisitorTest(LiveServerTestCase):
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
12
13
14
15
16
17
    def setUp(self):
        self.browser = webdriver.Chrome()

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

18
19
20
21
22
23
24
25
26
27
28
29
    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)
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
30

Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
31
32
33
    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
34
        self.browser.get(self.live_server_url)
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
35

36
        # She notices the page title and header mention to-do lists
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
37
        self.assertIn('To-Do', self.browser.title)
38
39
        header3_text = self.browser.find_element_by_tag_name('h3').text
        self.assertIn('To-Do', header3_text)
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
40
41
42
43
44

        # She notices the page header mention your name
        header_text = self.browser.find_element_by_tag_name('h1').text
        self.assertIn('Syahrul Findi Ardiansyah', header_text)

45
46
47
        # She check the comment when there is no item
        comment = self.browser.find_element_by_id('comment')
        self.assertEqual(
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
48
49
            comment.text,
            'yey, waktunya berlibur'
50
51
        )

Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        # She is invited to enter a to-do item straight away
        inputbox = self.browser.find_element_by_id('id_new_item')
        self.assertEqual(
            inputbox.get_attribute('placeholder'),
            'Enter a to-do item'
        )

        # She types "Buy peacock feathers" into a text box (Edith's hobby
        # is tying fly-fishing lures)
        inputbox.send_keys('Buy peacock feathers')

        # 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
        inputbox.send_keys(Keys.ENTER)
66
        self.wait_for_row_in_list_table('1: Buy peacock feathers')
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
67
68
69
70

        # There is still a text box inviting her to add another item. She
        # enters "Use peacock feathers to make a fly" (Edith is very
        # methodical)
71
72
73
74
75
        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)

        # The page updates again, and now shows both items on her list
76
77
        self.wait_for_row_in_list_table('1: Buy peacock feathers')
        self.wait_for_row_in_list_table(
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
78
            '2: Use peacock feathers to make a fly')
79

80
81
82
        # She check the comment when there is few items
        comment = self.browser.find_element_by_id('comment')
        self.assertEqual(
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
83
            comment.text,
84
85
86
87
88
89
90
91
92
93
94
95
96
            'sibuk tapi santai'
        )

        # Add additional items to change the comment
        for i in range(4):
            inputbox = self.browser.find_element_by_id('id_new_item')
            inputbox.send_keys('Additional item %d' % i)
            inputbox.send_keys(Keys.ENTER)
            time.sleep(1)

        # She check the comment when there is many items
        comment = self.browser.find_element_by_id('comment')
        self.assertEqual(
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
97
            comment.text,
98
99
100
            'oh tidak'
        )

101
102
103
        # Edith wonders whether the site will remember her list. Then she sees
        # that the site has generated a unique URL for her -- there is some
        # explanatory text to that effect.
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
104
        self.fail('Finish the test!')
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

    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
        )

        # 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
        )