Skip to content

Commit 04b625a

Browse files
committed
Refactor institutions login test
1 parent e1c3ebd commit 04b625a

File tree

2 files changed

+60
-32
lines changed

2 files changed

+60
-32
lines changed

pages/login.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,16 @@ class GenericInstitutionEmailLoginPage(BasePage):
102102
class GenericInstitutionUsernameLoginPage(BasePage):
103103
# This is for institution login pages that first ask for a Username or User ID to
104104
# initiate the login process before asking for a password. The page has a form
105-
# element and a usenrname or user id text input box.
105+
# element and a username or user id text input box.
106106
identity = Locator(By.CSS_SELECTOR, 'form[method="post"]')
107107

108108

109+
class GenericInstitutionIDLoginPage(BasePage):
110+
# This is for institution login pages that first ask for a User ID to
111+
# initiate the login process before asking for a password.
112+
identity = Locator(By.CSS_SELECTOR, 'input[autocomplete="username"]')
113+
114+
109115
class ForgotPasswordPage(BasePage):
110116
url = settings.OSF_HOME + '/forgotpassword/'
111117

tests/test_login.py

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
ForgotPasswordPage,
1616
GenericCASPage,
1717
GenericInstitutionEmailLoginPage,
18+
GenericInstitutionIDLoginPage,
1819
GenericInstitutionLoginPage,
1920
GenericInstitutionUsernameLoginPage,
2021
InstitutionalLoginPage,
@@ -330,6 +331,17 @@ def test_account_disabled_page(self, driver):
330331
assert exception_page.status_message.text == 'Account disabled'
331332

332333

334+
def try_login_page(driver, page_class):
335+
"""
336+
Helper function to try and verify a login page.
337+
Returns True if successful, otherwise False.
338+
"""
339+
try:
340+
return page_class(driver, verify=True)
341+
except (PageException, NoSuchElementException):
342+
return False
343+
344+
333345
@markers.smoke_test
334346
@markers.core_functionality
335347
class TestInstitutionLoginPage:
@@ -415,40 +427,50 @@ def test_individual_institution_login_pages(
415427
not have working testing environments.
416428
"""
417429
failed_list = []
430+
418431
for institution in institution_list:
419-
if institution != '-- select an institution --':
420-
institution_select = Select(institution_login_page.institution_dropdown)
421-
institution_select.select_by_visible_text(institution)
422-
institution_login_page.sign_in_button.click()
432+
# This value represents a placeholder or default option in the dropdown list,
433+
# which isn't a valid institution for testing. Avoid wrapping the rest of the loop's logic
434+
# in an additional if statement by using an early continue
435+
if institution == '-- select an institution --':
436+
continue
437+
438+
# Select institution and click sign in
439+
Select(institution_login_page.institution_dropdown).select_by_visible_text(
440+
institution
441+
)
442+
institution_login_page.sign_in_button.click()
443+
444+
try:
445+
# Verify that we get to a valid login page by checking for a
446+
# password input field
447+
assert GenericInstitutionLoginPage(driver, verify=True)
448+
except PageException:
423449
try:
424-
# Verify that we get to a valid login page by checking for a
425-
# password input field
426-
assert GenericInstitutionLoginPage(driver, verify=True)
427-
except PageException:
428-
try:
429-
# For a small number of institutions the initial login page
430-
# first asks for just an email without the passord field.
431-
assert GenericInstitutionEmailLoginPage(driver, verify=True)
432-
except PageException:
433-
try:
450+
# Try different login page verifications
451+
if not any(
452+
[
453+
# For a small number of institutions the initial login page
454+
# first asks for just an email without the password field.
455+
try_login_page(driver, GenericInstitutionEmailLoginPage),
434456
# A few institutions use a login page with a generic username
435-
# or user id text input field. The page definition checks for
436-
# a form element with methdo="post". Then check that the page
437-
# also has an input box.
438-
assert GenericInstitutionUsernameLoginPage(
439-
driver, verify=True
440-
)
441-
driver.find_element(By.CSS_SELECTOR, 'input[type="text"]')
442-
except (PageException, NoSuchElementException):
443-
# if there is a failure add the name of the institution to the
444-
# failed list
445-
failed_list.append(institution)
446-
# Need to go back to the original OSF Institution Login page
447-
institution_login_page.goto()
448-
# If there are any failed institutions then fail the test and print the list
449-
assert len(failed_list) == 0, 'The following Institutions Failed: ' + str(
450-
failed_list
451-
)
457+
# or user id text input field.
458+
try_login_page(driver, GenericInstitutionUsernameLoginPage),
459+
# Chicago University has autocomplete="username"
460+
try_login_page(driver, GenericInstitutionIDLoginPage),
461+
]
462+
):
463+
failed_list.append(institution)
464+
except Exception as e:
465+
failed_list.append(institution)
466+
467+
# Return to the original OSF Institution Login page
468+
institution_login_page.goto()
469+
470+
# Fail the test if there are any failed institutions and print the list
471+
assert (
472+
len(failed_list) == 0
473+
), f'The following Institutions Failed: {failed_list}'
452474

453475

454476
@markers.dont_run_on_prod

0 commit comments

Comments
 (0)