Skip to content

Commit a588bdb

Browse files
authored
Adjust selenium helpers to add firefox snap mode and drop python2 (#280)
does what the title says. Should be non-intrusive for common installations.
2 parents d99fb1d + 6c872ee commit a588bdb

File tree

2 files changed

+50
-43
lines changed

2 files changed

+50
-43
lines changed

mig/user/selenium/migcore.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@
5252
from selenium.webdriver.support.ui import Select
5353
try:
5454
from selenium.webdriver.firefox.service import Service as FirefoxService
55-
from webdriver_manager.firefox import GeckoDriverManager
5655
except ImportError:
5756
FirefoxService = None
57+
try:
58+
from webdriver_manager.firefox import GeckoDriverManager
59+
except ImportError:
5860
GeckoDriverManager = None
5961

6062

@@ -83,23 +85,43 @@ def init_driver(browser):
8385
webdriver_service.start()
8486
options = webdriver.FirefoxOptions()
8587
profile = webdriver.FirefoxProfile()
88+
options.profile = profile
8689
driver = webdriver.Remote(webdriver_service.service_url,
87-
options=options,
88-
browser_profile=profile)
90+
options=options)
91+
elif browser.lower() == 'firefox-snap':
92+
if FirefoxService is None:
93+
print("FATAL: FirefoxService is required for firefox-snap mode.")
94+
exit(1)
95+
# With snap the current firefox is ALWAYS symlinked in this system path
96+
firefox_path = '/snap/firefox/current/usr/lib/firefox/firefox'
97+
geckodriver_path = '/snap/firefox/current/usr/lib/firefox/geckodriver'
98+
options = webdriver.FirefoxOptions()
99+
options.binary_location = firefox_path
100+
profile = webdriver.FirefoxProfile()
101+
options.profile = profile
102+
driver = webdriver.Firefox(service=FirefoxService(geckodriver_path),
103+
options=options)
89104
elif browser.lower() == 'safari':
90105
driver = webdriver.Safari()
91106
elif browser.lower() == 'ie':
92107
driver = webdriver.Ie()
93108
elif browser.lower() == 'edge':
94109
driver = webdriver.Edge()
95110
elif browser.lower() == 'phantomjs':
96-
driver = webdriver.PhantomJS()
111+
# NOTE: PhantomJS was discontinued and we haven't used it much anyway
112+
# https://github.com/ariya/phantomjs/
113+
# driver = webdriver.PhantomJS()
114+
print("FATAL: PhantomJS is discontinued and no longer supported.")
115+
exit(2)
97116
else:
98117
print("ERROR: Browser NOT supported: %s" % browser)
99118
driver = None
100119
# Add a little slack for pages to load when finding elems
101120
if driver:
102121
driver.implicitly_wait(5)
122+
else:
123+
print("ERROR: cannot proceed without a valid driver")
124+
exit(42)
103125
return driver
104126

105127

@@ -285,7 +307,7 @@ def shared_twofactor(driver, url, twofactor_key, callbacks={},
285307
# past_twofactor_class)
286308
past_twofactor = True
287309
except Exception as exc:
288-
#print("DEBUG: not past twofactor login: %s" % exc)
310+
# print("DEBUG: not past twofactor login: %s" % exc)
289311
pass
290312

291313
if not past_twofactor:

mig/user/selenium/userguidescreens.py

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,13 @@
3131
corresponding screenshots.
3232
"""
3333

34-
from __future__ import print_function
35-
36-
from builtins import input
37-
from builtins import range
38-
39-
try:
40-
from future import standard_library
41-
standard_library.install_aliases()
42-
except:
43-
print("Failed to import future module - missing install?")
44-
exit(1)
45-
4634
import getpass
4735
import os
4836
import sys
4937
import time
5038
import traceback
5139

52-
if sys.version_info[0] >= 3:
53-
from urllib.parse import urlparse
54-
else:
55-
from urlparse import urlparse
40+
from urllib.parse import urlparse
5641

5742
try:
5843
from mig.user.selenium.migcore import init_driver, ucph_login, mig_login, \
@@ -321,7 +306,7 @@ def archives_actions(driver, url, login, passwd, callbacks):
321306
add_button.click()
322307
# TODO: figure out why this ajax_wait fails with V3
323308
# Wait for fileman popup to accept click handlers
324-
#ajax_wait(driver, nav_name + " file select", "ui-progressbar")
309+
# ajax_wait(driver, nav_name + " file select", "ui-progressbar")
325310
state = 'archive-fileman'
326311
if callbacks.get(state, None):
327312
print("INFO: callback for: %s" % state)
@@ -333,12 +318,12 @@ def archives_actions(driver, url, login, passwd, callbacks):
333318
for select_file in files_area.find_elements(by_what('class_name'),
334319
"ext_txt"):
335320
try:
336-
#print("DEBUG: scroll to file elem: %s" % select_file)
321+
# print("DEBUG: scroll to file elem: %s" % select_file)
337322
scroll_to_elem(driver, select_file)
338323

339324
# TODO: figure out how to get this dclick working
340325
# NOTE: dclick on same target hits a dir here after scroll!?
341-
#print("DEBUG: double click file elem: %s" % select_file)
326+
# print("DEBUG: double click file elem: %s" % select_file)
342327
doubleclick_elem(driver, select_file)
343328
selected = True
344329
except Exception as exc:
@@ -349,21 +334,21 @@ def archives_actions(driver, url, login, passwd, callbacks):
349334
if not selected:
350335
# NOTE: as a workaround we save path, cancel and manually fill for now
351336
file_path = select_file.text
352-
#print("DEBUG: found file path: %s" % file_path)
337+
# print("DEBUG: found file path: %s" % file_path)
353338
# Use parent of unique filechooser to find the right dialog among many
354339
filechooser_elem = driver.find_element(by_what('id'), "fm_filechooser")
355340
filechooser_dialog = filechooser_elem.parent
356341
# print("DEBUG: found filechooser %s and parent dialog: %s" %
357342
# (filechooser_elem, filechooser_dialog))
358343
close_button = filechooser_dialog.find_element(
359344
by_what('class_name'), "ui-dialog-titlebar-close")
360-
#print("DEBUG: found close button: %s" % close_button)
345+
# print("DEBUG: found close button: %s" % close_button)
361346
close_button.click()
362347
# dialog_buttons = filechooser_dialog.find_element(by_what('class_name'),
363348
# "ui-dialog-buttonset")
364349
# action_buttons = dialog_buttons.find_elements(by_what('class_name'),
365350
# "ui-button")
366-
#print("DEBUG: found action buttons: %s" % action_buttons)
351+
# print("DEBUG: found action buttons: %s" % action_buttons)
367352
# for button in action_buttons:
368353
# print("DEBUG: checking action button: %s" % button)
369354
# if button.text == 'Cancel':
@@ -389,7 +374,7 @@ def archives_actions(driver, url, login, passwd, callbacks):
389374

390375
submit_button = driver.find_element(by_what('xpath'),
391376
"//input[@type='submit' and @value='Save and Preview']")
392-
#print("DEBUG: click submit: %s" % submit_button)
377+
# print("DEBUG: click submit: %s" % submit_button)
393378
submit_button.click()
394379

395380
state = 'archive-submitted'
@@ -404,11 +389,11 @@ def archives_actions(driver, url, login, passwd, callbacks):
404389

405390
finalize_button = driver.find_element(by_what('class_name'),
406391
"finalizearchivelink")
407-
#print("DEBUG: click finalize button: %s" % finalize_button)
392+
# print("DEBUG: click finalize button: %s" % finalize_button)
408393
finalize_button.click()
409394

410395
# TMP! while testing
411-
#do_finalize = True
396+
# do_finalize = True
412397
do_finalize = False
413398
finalized = False
414399
confirm_elem = driver.find_element(by_what('id'), "confirm_dialog")
@@ -421,11 +406,11 @@ def archives_actions(driver, url, login, passwd, callbacks):
421406
"ui-button")
422407
for button in confirm_buttons:
423408
if do_finalize and button.text == 'Yes':
424-
#print("DEBUG: click confirm button: %s" % button.text)
409+
# print("DEBUG: click confirm button: %s" % button.text)
425410
button.click()
426411
finalized = True
427412
if button.text == 'No':
428-
#print("DEBUG: click confirm button: %s" % button.text)
413+
# print("DEBUG: click confirm button: %s" % button.text)
429414
button.click()
430415
# else:
431416
# print("DEBUG: ignore confirm button: %s" % button.text)
@@ -434,7 +419,7 @@ def archives_actions(driver, url, login, passwd, callbacks):
434419
print("WARNING: failed to confirm finalize - force close dialog")
435420
close_button = confirm_dialog.find_element(by_what('class_name'),
436421
"ui-dialog-titlebar-close")
437-
#print("DEBUG: found close button: %s" % close_button)
422+
# print("DEBUG: found close button: %s" % close_button)
438423
close_button.click()
439424

440425
state = 'archive-finalized'
@@ -443,7 +428,7 @@ def archives_actions(driver, url, login, passwd, callbacks):
443428
callbacks[state](driver, state)
444429

445430
view_button = driver.find_element(by_what('class_name'), "viewarchivelink")
446-
#print("DEBUG: click view button: %s" % view_button)
431+
# print("DEBUG: click view button: %s" % view_button)
447432
view_button.click()
448433
ajax_wait(driver, nav_name)
449434
state = 'archive-view'
@@ -454,7 +439,7 @@ def archives_actions(driver, url, login, passwd, callbacks):
454439
if finalized:
455440
register_button = driver.find_element(by_what('class_name'),
456441
"registerarchivelink")
457-
#print("DEBUG: click register button: %s" % register_button)
442+
# print("DEBUG: click register button: %s" % register_button)
458443
register_button.click()
459444

460445
dialog_buttons = driver.find_element(by_what('class_name'),
@@ -474,7 +459,7 @@ def archives_actions(driver, url, login, passwd, callbacks):
474459
print("WARNING: failed to confirm register doi - force close dialog")
475460
close_button = confirm_dialog.find_element(
476461
by_what('class_name'), "ui-dialog-titlebar-close")
477-
#print("DEBUG: found close button: %s" % close_button)
462+
# print("DEBUG: found close button: %s" % close_button)
478463
close_button.click()
479464

480465
# Redirecting to KU-IT DOI service where login may be required
@@ -484,7 +469,7 @@ def archives_actions(driver, url, login, passwd, callbacks):
484469
# Maybe need KU-IT DOI service login if requested
485470
try:
486471
logon_button = driver.find_element(by_what('id'), "cmdLogon")
487-
#print("DEBUG: click logon: %s" % logon_button)
472+
# print("DEBUG: click logon: %s" % logon_button)
488473
logon_button.click()
489474
except Exception as exc:
490475
pass
@@ -498,7 +483,7 @@ def archives_actions(driver, url, login, passwd, callbacks):
498483
password_field.send_keys(passwd)
499484
submit_button = driver.find_element(by_what('id'),
500485
"submitButton")
501-
#print("DEBUG: click submit: %s" % submit_button)
486+
# print("DEBUG: click submit: %s" % submit_button)
502487
time.sleep(1)
503488
submit_button.click()
504489
login_done = True
@@ -525,7 +510,7 @@ def archives_actions(driver, url, login, passwd, callbacks):
525510
break
526511
# Then find and click accept button
527512
if popup_dialog.is_displayed():
528-
#print("DEBUG: found visible popup dialog")
513+
# print("DEBUG: found visible popup dialog")
529514
popup_found = True
530515
popup_buttons = popup_dialog.find_elements(
531516
by_what('class_name'), "btn")
@@ -555,7 +540,7 @@ def archives_actions(driver, url, login, passwd, callbacks):
555540
# print "DEBUG: found DOI identifier: %s" % doi_idenfier
556541
break
557542
except Exception as exc:
558-
#print("DEBUG: DOI page not ready: %s" % exc)
543+
# print("DEBUG: DOI page not ready: %s" % exc)
559544
pass
560545

561546
# Keep trying until we get through login and usage accept
@@ -624,7 +609,7 @@ def setup_actions(driver, url, login, passwd, callbacks):
624609
# else:
625610
# print("DEBUG: use sif setup sections: %s" % active_setup_sections)
626611

627-
#print("DEBUG: run through setup section: %s" % active_setup_sections)
612+
# print("DEBUG: run through setup section: %s" % active_setup_sections)
628613
for (key, name) in active_setup_sections:
629614
# print("DEBUG: open setup section: %s" % name)
630615
# Search inside page content to avoid Seafile nav menu interference
@@ -731,7 +716,7 @@ def peers_actions(driver, url, login, passwd, callbacks):
731716
return
732717
# print "DEBUG: found %s link: %s" % (nav_name, link)
733718
link.click()
734-
#ajax_wait(driver, nav_name)
719+
# ajax_wait(driver, nav_name)
735720
state = 'peers-ready'
736721
if callbacks.get(state, None):
737722
print("INFO: callback for: %s" % state)
@@ -1004,7 +989,7 @@ def main():
1004989

1005990
driver = init_driver(browser)
1006991
# Make sure the screenshots have a suitable size
1007-
#driver.set_window_size(1400, 900)
992+
# driver.set_window_size(1400, 900)
1008993
driver.set_window_size(1680, 1080)
1009994
try:
1010995
driver.get(url)

0 commit comments

Comments
 (0)