-
Notifications
You must be signed in to change notification settings - Fork 9
/
base.py
619 lines (470 loc) · 18.6 KB
/
base.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
"""
Base classes for smoke tests. Test classes can subclass various
classes defined here instead of repetitively defining setUp and
tearDown methods. Note: these classes do NOT inherit from
unittest.TestCase. If subclasses need to be detected by unittest /
nose, they must multiply inherit from TestCase. This is done to
permit abstract test classes that will not be detected by unittest /
nose.
"""
# Imports
from functools import wraps
import os
import shutil
import unittest
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait as wait
import time
# Project imports
import config
import util
import urlparse
import os
import shutil
import tempfile
from datetime import datetime, timedelta
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait as wait
from pages.helpers import WaitForPageReload
from selenium.webdriver.common.action_chains import ActionChains
class SmokeTest(unittest.TestCase):
"""Base class for smoke tests. Creates a WebDriver
on setUp and quits on tearDown.
"""
# Allow multiprocessing for individual tests
_multiprocess_can_split_ = True
def setUp(self):
# Launch Selenium using options specified as class
# variables, which can include driver_name and
# desired_capabilities
if hasattr(self, 'driver_opts'):
self.driver = util.launch_driver(**self.driver_opts)
else:
self.driver = util.launch_driver()
self.site_root = config.osf_home.strip('/')
def tearDown(self):
# Quit Selenium
# Note: Use WebDriver.quit() instead of WebDriver.close();
# otherwise, SauceLabs tests will never finish
self.driver.quit()
def get_element(self, css):
return wait(
driver=self.driver,
timeout=5
).until(
method=ec.visibility_of_element_located(
(By.CSS_SELECTOR, css)
)
)
class UserSmokeTest(SmokeTest):
"""Class for smoke tests that require user login.
Creates a user and logs in on setUp and logs out on
tearDown.
"""
def setUp(self):
# Call parent setUpClass
super(UserSmokeTest, self).setUp()
# Create user account and login
self.user_data = util.create_user(self.driver)
util.login(
self.driver,
self.user_data['username'],
self.user_data['password']
)
def tearDown(self):
# Log out
util.logout(self.driver)
# Call parent tearDown
super(UserSmokeTest, self).tearDown()
def create_user(self):
return util.create_user(self.driver)
def log_in(self, user=None):
if not user:
user = self.user_data
return util.login(
self.driver,
user['username'],
user['password']
)
def log_out(self):
return util.logout(self.driver)
def get_user_url(self):
util.goto_profile(self.driver)
user_url=self.driver.find_element_by_css_selector("tr>td>a:first-child").get_attribute("href")
util.goto_project(self.driver)
return user_url
class ProjectSmokeTest(UserSmokeTest):
"""Class for smoke tests that require project
creation. Creates a project on setUp and deletes it
on tearDown.
"""
def setUp(self):
# Call parent setUp
super(ProjectSmokeTest, self).setUp()
# Create test project
self.project_url = util.create_project(self.driver)
# Browse to project page
util.goto_project(self.driver)
# add file paths
self.image_files = _generate_full_filepaths({
'jpg': 'test.jpg',
'png': 'test.png',
'gif': 'test.gif',
})
self.text_files = _generate_full_filepaths({
'txt': 'txtfile.txt',
'html': 'htmlfile.html',
})
self.archive_files = _generate_full_filepaths({
'tar': 'text_files.tar',
'tar.gz': 'text_files.tar.gz',
'zip': 'text_files.zip',
})
self.archive_file_contents = ('txtfile.txt','htmlfile.html')
self.binary_files = _generate_full_filepaths({
'pdf': 'pdffile.pdf',
})
self.versioned_files = _generate_full_filepaths({
0: 'versioned-0.txt',
1: 'versioned-1.txt',
})
def tearDown(self):
# Delete test project
#util.delete_project(self.driver)
# Call parent tearDown
super(ProjectSmokeTest, self).tearDown()
def goto(self, page, *args, **kwargs):
"""Go to a project's page
:param page: The name of the destination page. Acceptable include
"files", "settings", and "registrations"
:param node_url: Optional. The URL of the project or component to use.
:returns: True on success, KeyError page
"""
base_url = kwargs.get('node_url', self.project_url).strip('/')
build_path = {
'dashboard': lambda: base_url,
'files': lambda: '/'.join([base_url, 'files']),
'file': lambda: '/'.join([base_url, 'files', args[0]]),
'registrations': lambda: '/'.join([base_url, 'registrations']),
'settings': lambda: '/'.join([base_url, 'settings']),
'user-dashboard': lambda: '/'.join([self.site_root, 'dashboard']),
'wiki': lambda: '/'.join([base_url, 'wiki']),
}
# This will throw a KeyError if the page type is not in the above dict.
self.driver.get(
url=build_path[page]()
)
# Node methods
def add_contributor(self, user):
# click the "add" link
self.get_element('#contributors a[href="#addContributors"]').click()
# enter the user's email address
self.get_element('div#addContributors input[data-bind="value:query"]').send_keys(
user['username']
)
# click the search button
self.get_element('#addContributors button.btn').click()
# click the radio button for the first result
self.get_element('#addContributors a.btn.contrib-button').click()
# click the "Add" button
self.get_element('#addContributors a[data-bind="click:submit"]').click()
def remove_contributor(self, user_data):
# mouse over to the contribute's name
element_to_hover_over \
= self.get_element('#contributors a[data-fullname="'
+ user_data["fullname"]+'"]')
hover = ActionChains(self.driver).move_to_element(element_to_hover_over)
hover.perform()
# click the remove icon
element_to_hover_over.find_element_by_css_selector("i").click()
self.get_element("div.modal-dialog button[class='btn btn-primary']")\
.click()
def get_log(self):
log_entry_element = self.get_element("div.span5 dl")
class LogEntry(object):
def __init__(self, log_element):
entry_element = log_element.find_element_by_css_selector('dd:nth-of-type(1)')
self.log_text = entry_element.text
self.log_url=[]
css_url = entry_element.find_elements_by_css_selector('a')
for x in css_url:
self.log_url.append(x.get_attribute('href'))
self.log_time = datetime.strptime(
log_element.find_element_by_css_selector("dt:nth-of-type(1)").text,
"%m/%d/%y %I:%M %p",
)
return LogEntry(log_entry_element)
def edit_title(self, text):
self.get_element('#node-title-editable').click()
# select the name field on the new popup
edit_profile_name_field = self.get_element(
'div.popover-content input.span2'
)
# delete the current project name
edit_profile_name_field.clear()
# enter the new project name
edit_profile_name_field.send_keys(text)
# find and click submit new project name
self.get_element(
'div.popover-content button.btn.btn-primary'
).click()
def set_permission(self, permission, url=None):
url = url or self.project_url
self.driver.get(url)
if permission == 'public':
self.get_element('div.btn-toolbar a#publicButton.btn').click()
self.get_element(
'DIV.bootbox.modal.fade.bootbox-confirm.in button.btn.btn-primary'
).click()
else:
self.get_element('diset_permissionv.btn-toolbar a#privateButton.btn').click()
self.get_element(
'DIV.bootbox.modal.fade.bootbox-confirm.in button.btn.btn-primary'
).click()
def make_private(self, url=None):
"""Make a project or component private.
:param url: Optional. The URL of the project or component - defaults to
``self.project_url``
"""
url = url or self.project_url
if self.is_public(url):
self.set_permission('private', url)
def make_public(self, url=None):
"""Make a project or component private.
:param url: Optional. The URL of the project or component - defaults to
``self.project_url``
"""
url = url or self.project_url
if not self.is_public(url):
self.set_permission('public', url)
def is_public(self, url=None):
"""Test whether a project or component is public.
:param url: Optional. The URL of the project of component to test.
Defaults to ``self.project_url``
:return: ``True`` if public; ``False`` if private
"""
url = url or self.project_url
self.driver.get(url)
state = self.get_element(
'.btn-toolbar .btn-group:first-child button.disabled'
).text.lower()
return state == 'public'
def add_versioned_file(self):
filename = 'versioned.txt'
upload_dir = os.path.dirname(self.text_files['txt']['path'])
f = os.path.join(upload_dir, filename)
# rename and upload version 0.
shutil.copy(self.versioned_files[0]['path'], f)
self.add_file(f)
# rename and upload version 1
shutil.copy(self.versioned_files[1]['path'], f)
self.add_file(f)
# delete the temp file
os.remove(f)
return filename
def add_file(self, path, node_url=None):
"""Add a file. Assumes that the test class is harnessed to a project"""
node_url = node_url or self.project_url
self.goto('files', node_url=node_url)
wait(self.driver, 3).until(
ec.visibility_of_element_located(
(By.CSS_SELECTOR, 'div.container h3 a#clickable.dz-clickable')
)
)
self.driver.execute_script('''
$('input[type="file"]').attr('style', "");
''')
#with WaitForFileUpload(self.driver, wait=5):
## Upload files
#self.driver.find_element_by_css_selector(
# 'div.container h3 A#clickable.dz-clickable'
#).click()
wait(self.driver, 3).until(
ec.visibility_of_element_located(
(By.CSS_SELECTOR, 'input[type="file"]')
)
)
# Find file input
field = self.driver.find_element_by_css_selector('input[type="file"]')
# Enter file into input
field.send_keys(path)
# Component methods
def add_component(self, component_type, name, project_url=None):
"""Adds a component to the current project
:param component_type: a string representing the component type
:param name: the new component's name
:returns: URL of the component"""
# go to the project
self.goto(
'dashboard',
project_url or self.project_url
)
# click "Add Component"
self.get_element('a.btn[data-target="#newComponent"]').click()
modal = self.get_element('div.modal.fade.in')
# enter the component name
modal.find_element_by_css_selector(
'input[name="title"]'
).send_keys(name)
# choose the component type
modal.find_element_by_css_selector(
'select#category'
).send_keys(component_type)
# click OK
modal.find_element_by_css_selector(
'.modal-footer button[type="submit"]'
).click()
time.sleep(3)
# return url of the component
return self.driver.find_elements_by_css_selector(
'#Nodes UL.list-group.sortable.ui-sortable LI.project.list-group-item.list-group-item-node.cite-container'
)[-1].find_element_by_css_selector('h4 span a').get_attribute('href')
def delete_component(self, url, project=None):
"""Deletes the component.
Assumes that you are logged in as a user with contributor access to the
parent project.
:param url: URL of the project to delete
:param project: Optional. The URL of the project from which to delete
the component.
"""
raise NotImplementedError
def assert_error_page(self, error_msg, url=None):
"""Optionally navigate to page, then check for provided error
message.
:param error_msg: Error message
:param url: Optional URL
"""
# if a url was provided, go there
if url:
self.driver.get(url)
# an alert should be present with the error message
self.assertIn(
error_msg,
self.get_element('div.span12 h2').text,
)
def assert_not_found(self, url=None):
self.assert_error_page('Page not found.', url)
def assert_not_authorized(self, url=None):
"""Navigate to the page, and see if the item is accessible.
"""
self.assert_error_page('Unauthorized.', url)
def assert_forbidden(self, url=None):
"""Nav igate to the resource and verify the 403 (Forbidden) error is
present.
"""
self.assert_error_page('Forbidden.', url)
def create_fork(self, url=None):
"""Create a fork, and return its URL
:param url: Optional. The URL of the component or project to fork
"""
if url:
self.driver.get(url)
with WaitForPageReload(self.driver):
# click the fork button
self.get_element(
'div.btn-toolbar div.btn-group a.btn.node-fork-btn'
).click()
return self.driver.current_url
def create_registration(
self,
registration_type='Open-Ended Registration',
node_url=None,
):
"""Create a new registration.
Args:
registration_type : Type of registration
registration_data : Data for registration form
Returns:
URL of registration
"""
# Browse to registrations page
node_url = node_url or self.project_url
if registration_type == 'Open-Ended Registration':
self.driver.get(
node_url.strip('/') + '/register/Open-Ended_Registration'
)
elif registration_type == 'OSF-Standard Pre-Data Collection Registration':
self.driver.get('/'.join([
node_url.strip('/'),
'register',
'OSF-Standard_Pre-Data_Collection_Registration',
]))
else:
raise ValueError('Invalid registration type')
# Fill out the form
self.get_element(
'textarea[data-bind="value:value, attr:{name:id}, disable:disable"]'
).send_keys('Test content for a textarea.')
for elem in self.driver.find_elements_by_css_selector(
'div#registration_template select'):
elem.send_keys('Yes')
self.get_element(
'form.form-horizontal div.control-group input'
).send_keys('continue')
self.get_element('button#register-submit.btn').click()
# Hack: Wait for registration label so that we can get the
# correct URL for the registration
self.get_element('.label-important')
# Return URL of registration
return self.driver.current_url
def _file_exists_in_project(self, filename):
"""Goes to a file's page, verifies by checking the title."""
self.goto('file', filename)
return filename in self.get_element('div.page-header h1').text
def _generate_full_filepaths(self, file_dict):
"""Given a dict of filenames, return a dict that includes the full path
for each."""
# Make each filename a full path
for f in file_dict:
file_dict[f] = {
'path': os.path.join( # append filename to this directory
os.path.dirname(os.path.abspath(__file__)),
'upload_files',
file_dict[f]),
'filename': file_dict[f],
}
return file_dict
def _assert_time(self,time_now):
#assert the time
time_diff = abs(datetime.utcnow()-time_now)
self.assertTrue(time_diff < timedelta(minutes=2))
def _add_wiki(self,text):
#help function to add the wiki "text"
util.edit_wiki(self.driver)
util.clear_wiki_text(self.driver)
util.add_wiki_text(self.driver, text)
util.submit_wiki_text(self.driver)
def get_wiki_text(self):
"""Provided you are on a wiki page, get the raw contents of the page"""
return self.driver.execute_script('''
return $('textarea#wmd-input').val()
''')
def set_wiki_text(self, text, append=True):
textarea = self.get_element('textarea#wmd-input')
if not append:
# clear the input
self.driver.execute_script('''
$('textarea#wmd-input').val('')
''')
textarea.send_keys(text)
def not_implemented(f):
@wraps(f)
@unittest.skip('Not yet implemented')
def wrapper(*args, **kwargs):
return f
return wrapper
def _generate_full_filepaths(file_dict):
"""Given a dict of filenames, return a dict that includes the full path
for each."""
# Make each filename a full path
for f in file_dict:
file_dict[f] = {
'path': os.path.join( # append filename to this directory
os.path.dirname(os.path.abspath(__file__)),
'upload_files',
file_dict[f]),
'filename': file_dict[f],
}
return file_dict