-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathss_tests.py
executable file
·466 lines (344 loc) · 13.4 KB
/
ss_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/usr/bin/python
# General stuff
import unittest
import selenium.webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
import re
import urllib
# Spacescout-specific stuff
import maildata
import mailcheck
import ssdata
import netid
from netid import weblogin
# Connect all our IMAP connections to save time later
# TODO: Make this only happen if we're actually doing mail tests
#for em in maildata.emails.values():
# em.connect()
# Load settings from files
urlbase = ssdata.urlbase
testnetid = netid.testnetid
# Get the last email in a mailUser object's inbox
def getLastMail(user):
return(user.getlast())
# Navigate to login page and send email
def sendEmail(driver, Space = ssdata.exampleSpace, emails = None, From = None, Subject = None, Message = None):
"""Navigates the specified webdriver to the page to share the given space.
sendEmail(driver, Space = exampleSpace, emails = None, From = None, Subject = None, Message = None)
Fills in the form according to emails (an iterable containing the email addresses to put in the "to" field), From, Subject, and Message.
"""
d = driver
url = ssdata.shareurl %{'id' : str(Space)}
d.get(url)
# If we need to sign in, do that
if d.title == 'UW NetID Weblogin':
weblogin(d)
time.sleep(1)
time.sleep(1)
# Check to make sure the back button goes to the right place
e = driver.find_element_by_xpath('//a[@title="Back to space details"]')
url = e.get_attribute('href')
expurl = ssdata.spaceurl % {'id' : str(Space)}
if not(expurl == url):
pass
# TODO
#raise(Exception('Did not find correct back button href. \nExpected "%s", got "%s"' %(expurl, url)))
# If these were supplied, fill out the corresponding fields
# It's necessary to use 'if X != None' rather than 'if X'
# because a blank string will bool() to false.
if From != None:
e = d.find_element_by_name('sender')
e.clear()
e.send_keys(From)
if emails != None:
try:
# Grab page source in case we need to debug later
source = d.page_source.encode('utf8')
try: # The autocomplete script changes this box
e = d.find_element_by_name('recipient')
e.clear()
except:
time.sleep(1)
#e = d.find_element_by_id('id_recipient-tokenfield')
e = d.find_element_by_class_name('tt-input')
e.clear()
# Put in each recipient address
for user in emails:
#address = maildata.emails[user].address
address = user
# Have to send a tab after each one
e.send_keys(address + '\t')
# Debugging stuff:
# If it couldn't find the box, dump the source collected earlier
# so that we can figure out why.
except Exception as E:
outfile = open('source-%s.html' %str(int(time.time())), 'wb')
outfile.write(source)
outfile.close()
raise(E)
if Subject != None:
e = d.find_element_by_name('subject')
e.clear()
e.send_keys(Subject)
if Message != None:
e = d.find_element_by_name('message')
e.clear()
e.send_keys(Message)
# Submit the form
e = d.find_element_by_id('formSubmit_button')
time.sleep(1)
e.submit()
time.sleep(1)
# Checks to see if pattern is in string, accounting for
# different types of newlines by converting all newlines
# in both strings to line feeds
def msgCheck(pattern, string):
if pattern in string: # Simple check first
return True
newPattern = re.sub('(\r\n|\r|\n)', '\n', pattern)
newString = re.sub('(\r\n|\r|\n)', '\n', string)
return newPattern in newString
# Test class for email-related things
class ss_email(unittest.TestCase):
# Load webdriver
def setUp(self):
self.driver = selenium.webdriver.Firefox()
self.driver.maximize_window()
self.action = ActionChains(self.driver)
# Send a space to mailtest with the default settings
def test_one_space(self):
self.send_and_check_email()
# Test sending to multiple addresses at once
def test_multiple_emails(self):
self.send_and_check_email(emails = ('external1', 'gmail1'))
# Send a space to mailtest with a custom subject
def test_custom_subject(self):
self.send_and_check_email(Subject = 'Blah blah')
# Send a space to 'lisatest'
def test_gmail(self):
self.send_and_check_email(emails = ('gmail1',))
# Test a custom body
def test_custom_body(self):
self.send_and_check_email(Message = 'This is a test custom body')
# Test a custom body with multiple lines
def test_custom_body_multiline(self):
self.send_and_check_email(Message = 'This is a\ncustom body\nwith multiple\nlines.')
# Test a custom "from"
def test_custom_from(self):
self.send_and_check_email(From = '[email protected]')
# Test another space
def test_another_space(self):
self.send_and_check_email(Space = 4337)
# Failure tests
# Make sure failure to supply a "To" address gives an error
def test_no_to_address(self):
sendEmail(self.driver)
e = self.driver.find_element_by_xpath('//span[@class="alert-error"]')
self.assertEquals(e.text, u'Required field')
# Make sure invalid "To" address gives us an error
def test_invalid_to_address(self):
sendEmail(self.driver, emails = ('asdf',))
e = self.driver.find_element_by_xpath('//span[@class="alert-error"]')
self.assertIn('not a valid e-mail address', e.text)
# Make sure missing "From" address gives us an error
def test_no_from_address(self):
sendEmail(self.driver, emails = ('[email protected]',), From = '')
e = self.driver.find_element_by_xpath('//span[@class="alert-error"]')
self.assertEquals(e.text, u'Required field')
# Make sure invalid "From" address gives us an error
def test_invalid_from_address(self):
sendEmail(self.driver, emails = ('[email protected]',), From = 'asdf')
e = self.driver.find_element_by_xpath('//span[@class="alert-error"]')
self.assertIn('Enter a valid e-mail address', e.text)
# Function for sending a space via the email page
def send_and_check_email(self, driver = None, emails = ('external1',), Space = ssdata.exampleSpace, From = None, Subject = None, Message = None):
"""Sends an email via the share page, and checks to make sure it was received
send_and_check_email(driver = None, emails = ('eternal1',), Space = exampleSpace, From = None, Subject = None, Message = None)
If driver is not specified, then self.driver will be assumed. All the arguments are optional, and will not be specified nor checked.
"""
users = []
for user in emails:
users.append(maildata.emails[user])
rcpts = []
for user in emails:
rcpts.append(maildata.emails[user].address)
if not(driver):
driver = self.driver
sendEmail(driver, Space, rcpts, From, Subject, Message)
time1 = time.time()
time.sleep(2)
# Back button broken
# TODO
e = driver.find_element_by_xpath('//a[@title="Back to space details"]')
url = e.get_attribute('href')
#self.assertEquals(ssdata.spaceurl %{'id' : str(Space)}, url)
e.click()
#self.assertEquals(ssdata.spaceurl %{'id' : str(Space)}, driver.current_url)
time.sleep(max(time1 - time.time() + maildata.delay, 0))
for user in users:
msg = getLastMail(user)
rcpt = user.address
if Subject:
self.assertEqual(Subject, msg.Subject)
else:
self.assertEqual('Check out this space I found on SpaceScout', msg.Subject)
if From:
self.assertEqual(From, msg.From)
self.assertEqual(rcpt, msg.To)
if Message:
for msgbody in msg.body:
self.assertTrue(msgCheck(Message, msgbody.text), 'Custom message %s not found in body %s' %(Message, msgbody.text))
def tearDown(self):
self.driver.close()
# End of email stuff
# Favorites testing stuff
def go_to_space(d, Space):
"""Navigates to a space page with the specified ID."""
d.get(ssdata.spaceurl % {'id' : str(Space)})
def favorite_current_space(d):
"""Clicks the favorite button on the current space page."""
e = get_favorite_button(d)
e.click()
def ss_login(d):
"""Browses to the login page and logs in."""
d.get(ssdata.loginurl)
netid.weblogin(d)
def get_favorite_button(d):
"""Returns the favorite button for the current space page."""
return d.find_element_by_xpath('//button[@id="favorite_space"]')
def go_to_favorites(d):
"""Browses to the favorites page."""
d.get(ssdata.faveurl)
testspace = ssdata.exampleSpace
class ss_faves(unittest.TestCase):
def setUp(self):
self.driver = selenium.webdriver.Firefox()
self.driver.maximize_window()
# Tests only logging in. Logs out afterwards.
def test_login_only(self):
d = self.driver
d.get(ssdata.urlbase)
time.sleep(2)
try:
d.find_element_by_link_text('Log in')
except:
self.fail('Could not find log in button')
go_to_space(d, testspace)
time.sleep(2)
e = d.find_element_by_link_text('Log in')
e.click()
time.sleep(2)
netid.weblogin(d)
time.sleep(8)
try:
d.find_element_by_link_text('Log out')
except:
self.fail('Could not find log out button')
# Tests favoriting and unfavoriting a space.
def test_favorite_and_unfavorite(self):
s = testspace
d = self.driver
ss_login(self.driver)
time.sleep(2)
go_to_space(d, s)
time.sleep(8)
e = get_favorite_button(d)
e.click()
time.sleep(1)
self.assertEquals(e.text, 'Favorited')
e.click()
time.sleep(1)
self.assertEquals(e.text, 'Favorite')
# Tests favoriting a space, and checks the favorites page
# to make sure it's there.
def test_favorite_and_check(self):
s = testspace
d = self.driver
ss_login(self.driver)
time.sleep(2)
go_to_space(d, ssdata.exampleSpace)
time.sleep(8)
favorite_current_space(d)
time.sleep(1)
go_to_favorites(d)
time.sleep(2)
e = d.find_element_by_xpath('//a[@data-id="%s"]' %str(s))
e.click()
time.sleep(2)
try:
e = d.find_element_by_xpath('//a[@data-id="%s"]' %str(s))
raise Exception('Space still in favorites after being removed')
except selenium.common.exceptions.NoSuchElementException as e:
pass
except Exception as e:
raise e
# Tests to see that upon favoriting a space while logged out, you
# are then redirected to the login page. If you choose to log in,
# then the space will be favorited.
def test_favorite_logged_out(self):
s = testspace
d = self.driver
go_to_space(d, s)
time.sleep(1)
favorite_current_space(d)
time.sleep(1)
try:
a = d.switch_to_alert()
a.dismiss()
except:
pass
netid.weblogin(d)
time.sleep(8)
e = d.find_element_by_xpath('//button[@id="favorite_space"]')
self.assertEquals(e.text, 'Favorited')
favorite_current_space(d)
time.sleep(1)
e = d.find_element_by_xpath('//button[@id="favorite_space"]')
self.assertEquals(e.text, 'Favorite')
# Tests to see if browsing to the favorites page while logged
# out will redirect you to the login page, and then back to the
# favorites page after logging out.
def test_favorites_login_redirect(self):
d = self.driver
go_to_favorites(d)
time.sleep(2)
self.assertIn('weblogin.washington.edu', d.current_url)
netid.weblogin(d)
time.sleep(2)
self.assertEquals(ssdata.faveurl, d.current_url)
# Test favoriting a space that is already favorited while being
# logged out.
# Expected: User is prompted to log in. After logging in,
# space stays favorited.
def test_already_favorited_logged_out(self):
d = self.driver
s = testspace
ss_login(d)
time.sleep(2)
go_to_space(d, s)
time.sleep(5)
favorite_current_space(d)
d2 = selenium.webdriver.Firefox()
go_to_space(d2, s)
time.sleep(5)
favorite_current_space(d2)
time.sleep(2)
try:
a = d2.switch_to_alert()
a.dismiss()
except:
pass
netid.weblogin(d2)
time.sleep(5)
e = get_favorite_button(d2)
self.assertEquals(e.text, 'Favorited')
e.click()
time.sleep(1)
self.assertEquals(e.text, 'Favorite')
d2.close()
def tearDown(self):
self.driver.close()
if __name__ == '__main__':
unittest.main()