/
tests.py
203 lines (154 loc) · 7.71 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
192
193
194
195
196
197
198
199
200
201
from collections import namedtuple
from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
PollInfo = namedtuple('PollInfo', ['question', 'choices'])
POLL1 = PollInfo(
question="How awesome is Test-Driven Development?",
choices=[
'Very awesome',
'Quite awesome',
'Moderately awesome',
],
)
POLL2 = PollInfo(
question="Which workshop treat do you prefer?",
choices=[
'Beer',
'Pizza',
'The Acquisition of Knowledge',
],
)
class PollsTest(LiveServerTestCase):
fixtures = ['admin_user.json']
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
def test_can_create_new_poll_via_admin_site(self):
# Gertrude opens her web browser, and goes to the admin page
self.browser.get(self.live_server_url + '/admin/')
# She sees the familiar 'Django administration' heading
body = self.browser.find_element_by_tag_name('body')
self.assertIn('Django administration', body.text)
# She types in her username and passwords and hits return
username_field = self.browser.find_element_by_name('username')
username_field.send_keys('admin')
password_field = self.browser.find_element_by_name('password')
password_field.send_keys('adm1n')
password_field.send_keys(Keys.RETURN)
# her username and password are accepted, and she is taken to
# the Site Administration page
body = self.browser.find_element_by_tag_name('body')
self.assertIn('Site administration', body.text)
# She now sees a couple of hyperlink that says "Polls"
polls_links = self.browser.find_elements_by_link_text('Polls')
self.assertEquals(len(polls_links), 2)
# The second one looks more exciting, so she clicks it
polls_links[1].click()
# She is taken to the polls listing page, which shows she has
# no polls yet
body = self.browser.find_element_by_tag_name('body')
self.assertIn('0 polls', body.text)
# She sees a link to 'add' a new poll, so she clicks it
new_poll_link = self.browser.find_element_by_link_text('Add poll')
new_poll_link.click()
# She sees some input fields for "Question" and "Date published"
body = self.browser.find_element_by_tag_name('body')
self.assertIn('Question:', body.text)
self.assertIn('Date published:', body.text)
# She types in an interesting question for the Poll
question_field = self.browser.find_element_by_name('question')
question_field.send_keys("How awesome is Test-Driven Development?")
# She sets the date and time of publication - it'll be a new year's
# poll!
date_field = self.browser.find_element_by_name('pub_date_0')
date_field.send_keys('01/01/12')
time_field = self.browser.find_element_by_name('pub_date_1')
time_field.send_keys('00:00')
# She sees she can enter choices for the Poll. She adds three
choice_1 = self.browser.find_element_by_name('choice_set-0-choice')
choice_1.send_keys('Very awesome')
choice_2 = self.browser.find_element_by_name('choice_set-1-choice')
choice_2.send_keys('Quite awesome')
choice_3 = self.browser.find_element_by_name('choice_set-2-choice')
choice_3.send_keys('Moderately awesome')
# Gertrude clicks the save button
save_button = self.browser.find_element_by_css_selector("input[value='Save']")
save_button.click()
# She is returned to the "Polls" listing, where she can see her
# new poll, listed as a clickable link
new_poll_links = self.browser.find_elements_by_link_text(
"How awesome is Test-Driven Development?"
)
self.assertEquals(len(new_poll_links), 1)
# Satisfied, she goes back to sleep
def _setup_polls_via_admin(self):
# Gertrude logs into the admin site
self.browser.get(self.live_server_url + '/admin/')
username_field = self.browser.find_element_by_name('username')
username_field.send_keys('admin')
password_field = self.browser.find_element_by_name('password')
password_field.send_keys('adm1n')
password_field.send_keys(Keys.RETURN)
# She has a number of polls to enter. For each one, she:
for poll_info in [POLL1, POLL2]:
# Follows the link to the Polls app, and adds a new Poll
self.browser.find_elements_by_link_text('Polls')[1].click()
self.browser.find_element_by_link_text('Add poll').click()
# Enters its name, and uses the 'today' and 'now' buttons to set
# the publish date
question_field = self.browser.find_element_by_name('question')
question_field.send_keys(poll_info.question)
self.browser.find_element_by_link_text('Today').click()
self.browser.find_element_by_link_text('Now').click()
# Sees she can enter choices for the Poll on this same page,
# so she does
for i, choice_text in enumerate(poll_info.choices):
choice_field = self.browser.find_element_by_name('choice_set-%d-choice' % i)
choice_field.send_keys(choice_text)
# Saves her new poll
save_button = self.browser.find_element_by_css_selector("input[value='Save']")
save_button.click()
# Is returned to the "Polls" listing, where she can see her
# new poll, listed as a clickable link by its name
new_poll_links = self.browser.find_elements_by_link_text(
poll_info.question
)
self.assertEquals(len(new_poll_links), 1)
# She goes back to the root of the admin site
self.browser.get(self.live_server_url + '/admin/')
# She logs out of the admin site
self.browser.find_element_by_link_text('Log out').click()
def test_voting_on_a_new_poll(self):
# First, Gertrude the administrator logs into the admin site and
# creates a couple of new Polls, and their response choices
self._setup_polls_via_admin()
# Now, Herbert the regular user goes to the homepage of the site. He
# sees a list of polls.
self.fail('finish writing this test!')
# He clicks on the link to the first Poll, which is called
# 'How awesome is test-driven development?'
# He is taken to a poll 'results' page, which says
# "no-one has voted on this poll yet"
# He also sees a form, which offers him several choices.
# There are three options with radio buttons
# The buttons have labels to explain them
# He decided to select "very awesome", which is answer #1
# Herbert clicks 'submit'
# The page refreshes, and he sees that his choice
# has updated the results. they now say
# "100 %: very awesome".
# The page also says "1 vote"
# But not "1 votes" -- Herbert is impressed at the attention to detail
# Herbert suspects that the website isn't very well protected
# against people submitting multiple votes yet, so he tries
# to do a little astroturfing
# The page refreshes, and he sees that his choice has updated the
# results. it still says # "100 %: very awesome".
# But the page now says "2 votes"
# Cackling manically over his l33t haxx0ring skills, he tries
# voting for a different choice
# Now, the percentages update, as well as the votes
# Satisfied, he goes back to sleep