-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.py
576 lines (409 loc) · 13.9 KB
/
util.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
"""
Miscellaneous utility functions for smokescreen tests
"""
import os
import re
import time
import uuid
import requests
# Selenium imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
# Project imports
import config
caps = webdriver.DesiredCapabilities.FIREFOX
caps['platform'] = "Windows 7"
caps['version'] = "24"
caps['name'] = ''
def launch_driver(
driver_name=None,
desired_capabilities={},
wait_time=config.selenium_wait_time):
"""Create and configure a WebDriver.
Args:
driver_name : Name of WebDriver to use
wait_time : Time to implicitly wait for element load
"""
driver_name = driver_name or config.driver_name
driver_cls = getattr(webdriver, driver_name)
if driver_name == 'Remote':
# Set up command executor
command_executor = 'http://%s:%[email protected]:80/wd/hub' \
% (os.environ.get('SAUCE_USERNAME'), os.environ.get('SAUCE_ACCESS_KEY'))
driver = driver_cls(
desired_capabilities=desired_capabilities,
command_executor=command_executor
)
elif driver_name == 'PhantomJS':
driver = driver_cls('/usr/local/bin/phantomjs')
else:
driver = driver_cls()
# Wait for elements to load
driver.implicitly_wait(wait_time)
# Return driver
return driver
def clear_text(elm):
"""Clear text via backspace. Usually we can skip
this and clear via elm.clear() directly, but this
doesn't work in all cases (e.g. Wiki editing).
"""
for _ in range(len(elm.text)):
elm.send_keys(Keys.BACK_SPACE)
def get_alert_boxes(driver, alert_text):
"""Check page for alert boxes. Asserts that there is exactly
one matching alert.
Args:
driver : WebDriver instance
alert_text : Text to search for in alert box
Returns:
matching alert boxes
"""
# Find alerts
alerts = driver.find_elements_by_xpath(
'//*[text()[contains(translate(., "%s", "%s"), "%s")]]' %
(alert_text.upper(), alert_text.lower(), alert_text.lower())
)
# Return matching alert boxes
return alerts
find_btn = lambda elm: elm.find_element_by_xpath('.//button')
find_create_btn = lambda elm: elm.find_element_by_css_selector(
'BUTTON.btn.btn-submit.btn-success'
)
def fill_creation_form(
root,
fields,
button_finder=find_create_btn):
"""Fill out form fields and click submit button.
Args:
form : root element
fields : dict of id -> value pairs for form
button_finder : function to get button from root element
"""
# Enter field values
for field in fields:
root.find_element_by_css_selector(field).send_keys(fields[field])
# Click submit button
button_finder(root).click()
def fill_form(
root,
fields,
button_finder=find_btn):
"""Fill out form fields and click submit button.
Args:
form : root element
fields : dict of id -> value pairs for form
button_finder : function to get button from root element
"""
# Enter field values
for field in fields:
root.find_element_by_css_selector(field).send_keys(fields[field])
# Click submit button
button_finder(root).click()
def login(driver, username, password):
"""Login to OSF
Args:
driver : selenium.webdriver instance
login_data : dict of id -> value pairs for login form
Examples:
> login(driver, {'username' : '[email protected]', 'password' : 'testtest'})
"""
# Browse to OSF login page
driver.get('%s/account' % (config.osf_home))
# Get login form
driver.find_element_by_css_selector(
'DIV.col-md-5.col-md-offset-2 FORM#signinForm.form-stacked INPUT#username.form-control'
).send_keys(username)
driver.find_element_by_css_selector(
'DIV.col-md-5.col-md-offset-2 FORM#signinForm.form-stacked INPUT#password.form-control'
).send_keys(password)
driver.find_element_by_css_selector(
'DIV.col-md-5.col-md-offset-2 FORM#signinForm.form-stacked BUTTON.btn.btn-submit.btn-primary'
).click()
def gen_user_data(_length=12):
""" Generate data to create a user account. """
uid = unicode(uuid.uuid1())[:_length - 1]
unique = uid + u'\xe4'
username = u'{}@osftest.org'.format(uid)
return {
'username': username,
'username2': username,
'password': unique,
'password2': unique,
'fullname': unique
}
def create_user(driver=None, user_data=None):
"""Create a new user account.
Args:
driver : selenium.webdriver instance
user_data : dict of id -> value pairs for registration form
default: config.registration_data
Returns:
dict of user information
Examples:
> create_user(driver, {
'fullname' : 'test test',
'username' : '[email protected]',
'username2' : '[email protected]',
'password' : 'testtest',
'password2' : 'testtest',
}
"""
user_data = user_data or gen_user_data()
requests.post(
url='/'.join((config.osf_home.strip('/'), 'register/')),
data={
'register-fullname': user_data['fullname'],
'register-username': user_data['username'],
'register-username2': user_data['username'],
'register-password': user_data['password'],
'register-password2': user_data['password'],
},
verify=False
)
# Return user data
return user_data
def goto_dashboard(driver):
"""Browse to dashboard page.
Args:
driver : WebDriver instance
"""
driver.get('%s/dashboard' % (config.osf_home))
def goto_profile(driver):
"""Browse to public profile page.
Args:
driver : WebDriver instance
"""
# Browse to dashboard
goto_dashboard(driver)
# Click Public Profile link
driver.find_element_by_link_text('My Public Profile').click()
def goto_project(driver, project_title=config.project_title):
"""Browse to project page.
Args:
driver : WebDriver instance
project_title : Title of project to be loaded
Returns:
URL of project page
"""
# Browse to dashboard
goto_dashboard(driver)
# Click on project title
driver.find_element_by_link_text(project_title).click()
return driver.current_url
def goto_files(driver, project_title=config.project_title):
""" Browse to files page.
Args:
driver: WebDriver instance
project_title : Title of project to be loaded
"""
# Browse to project page
goto_project(driver, project_title)
# Browse to files page
driver.find_element_by_link_text('Files').click()
def goto_settings(driver, project_name=config.project_title):
"""Browse to project settings page.
Args:
driver : WebDriver instance
project_name : Project name
"""
# Browse to project page
goto_project(driver, project_name)
# Click Settings button
driver.find_element_by_link_text('Settings').click()
def goto_registrations(driver, project_name=config.project_title):
# Browse to project page
goto_project(driver, project_name)
# Click Registrations button
driver.find_element_by_link_text('Registrations').click()
def logout(driver):
""" Log out of OSF.
Args:
driver : selenium.webdriver instance
"""
# browse to OSF page
driver.get(config.osf_home)
# locate and click logout button
try:
driver.find_element_by_xpath('//a[@href="/logout"]').click()
except NoSuchElementException:
# There is no logout link - assume the user is not logged in
pass
def create_project(driver, project_title=config.project_title, project_description=config.project_description):
"""Create new project
Args:
driver : selenium.webdriver instance
project_title : project title
project_description : project description
Returns:
URL of created project
"""
# Browse to dashboard
goto_dashboard(driver)
# Click New Project button
driver.find_element_by_link_text("New Project").click()
# Fill out form and submit
project_form = driver.find_element_by_xpath('//form[@name="newProject"]')
fill_form(
project_form, {
'#title' : project_title,
'#description' : project_description,
}
)
# Return project URL
return driver.current_url
def create_node(
driver,
project_title=config.project_title,
node_title=config.node_title,
project_url=None):
"""
"""
# Browse to project
if project_url is not None:
driver.get(project_url)
else:
goto_project(driver, project_title)
# Click New Node button
driver.find_element_by_link_text('Add Component').click()
# Get form
form = driver.find_element_by_xpath(
'//form[contains(@action, "newnode")]'
)
# Wait for modal to stop moving
WebDriverWait(driver, 3).until(
ec.visibility_of_element_located(
(By.CSS_SELECTOR, 'input[name="title"]')
)
)
# Fill out form
fill_form(
form,
{
'input[name="title"]' : node_title,
'#category' : 'Project',
}
)
def select_partial(driver, id, start, stop):
"""Select a partial range of text from an element.
Args:
driver : WebDriver instance
id : ID of target element
start : Start position
stop : Stop position
"""
# Inject partial selection function
# Adapted from http://stackoverflow.com/questions/646611/programmatically-selecting-partial-text-in-an-input-field
driver.execute_script('''
(function(field, start, end) {
if( field.createTextRange ) {
var selRange = field.createTextRange();
selRange.collapse(true);
selRange.moveStart('character', start);
selRange.moveEnd('character', end-start);
selRange.select();
} else if( field.setSelectionRange ) {
field.setSelectionRange(start, end);
} else if( field.selectionStart ) {
field.selectionStart = start;
field.selectionEnd = end;
}
field.focus();
})(document.getElementById("%s"), %d, %d);
''' % (id, start, stop))
# Wiki functions
def edit_wiki(driver):
edit_button = driver.find_element_by_link_text('Edit')
edit_button.click()
def get_wiki_input(driver):
return driver.find_element_by_id('wmd-input')
def add_wiki_text(driver, text):
get_wiki_input(driver).send_keys(text)
def clear_wiki_text(driver):
clear_text(get_wiki_input(driver))
def submit_wiki_text(driver):
""" Click submit button. """
driver.find_element_by_css_selector(
'DIV.col-md-9 INPUT.btn.btn-primary.pull-right'
).click()
def get_wiki_version(driver):
""" Get current wiki version. """
# Extract version text
version = str(driver.find_element_by_css_selector(
"DIV.container DIV.col-md-3 dl"
).text[-11:])
# Strip (current) from version string
version = re.sub('\s*\(current\)\s*', '', version, flags=re.I)
# Return version number or 0
try:
return int(version)
except ValueError:
return 0
def get_wiki_par(driver):
""" Get <p> containing wiki text. """
# Set implicitly_wait to short value: text may not
# exist, so we don't want to wait too long to find it
driver.implicitly_wait(0.1)
# Extract wiki text
# Hack: Wiki text element isn't uniquely labeled,
# so find its sibling first
try:
wiki_par = driver.find_elements_by_css_selector(
'div.watermarked div.container'
)[0].find_element_by_css_selector(
'div.col-md-9 p'
)
except NoSuchElementException:
wiki_par = None
# Set implicitly_wait to original value
driver.implicitly_wait(config.selenium_wait_time)
# Return element
return wiki_par
def get_wiki_text(driver):
""" Get text from wiki <p>. """
# Get <p> containing wiki text
wiki_par = get_wiki_par(driver)
# Extract text
if wiki_par is not None:
return wiki_par.text
return ''
def get_wiki_preview(driver):
"""
"""
return driver\
.find_element_by_id('wmd-preview')\
.text
def forget_password(driver, email):
"""forgotpassword to OSF
Args:
driver : selenium.webdriver instance
forgotpassword_data : dict of id -> value pairs for forgotpassword form
Examples:
> forgotpassword(driver, {'email' : '[email protected]'})
"""
# Browse to OSF forgotpassword page
driver.get('%s/account' % (config.osf_home))
# Get forgotpassword form
forgotpassword_form = driver.find_element_by_xpath('//form[@name="forgotpassword"]')
fill_form(forgotpassword_form, {
'#forgot_password-email' : email,
})
def project_rename(driver, text):
driver.find_element_by_id('node-title-editable').click()
# select the name field on the new popup
edit_profile_name_field = driver.find_element_by_xpath(
'//div[@class="popover-content"]//input[@class="span2"]'
)
# delete the curr
# ent 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
driver.find_element_by_xpath(
'//div[@class="popover-content"]//button[@class="btn btn-primary"]'
).click()
driver.refresh()