diff --git a/tests/conftest.py b/tests/conftest.py index 4a397b7ec..6c4f94b4f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,6 @@ import copy import json import os -import re from contextlib import contextmanager from datetime import date, datetime, timedelta from unittest.mock import Mock, PropertyMock @@ -36,8 +35,6 @@ from . import ( load_dotenv() -E2E_TEST_URI = os.getenv("NOTIFY_E2E_TEST_URI") - class ElementNotFound(Exception): pass @@ -3521,88 +3518,6 @@ def mock_get_invited_org_user_by_id(mocker, sample_org_invite): ) -def login_for_end_to_end_testing(browser): - # Open a new page and go to the staging site. - context = browser.new_context() - page = context.new_page() - page.goto(f"{E2E_TEST_URI}/") - - sign_in_button = page.get_by_role("link", name="Sign in") - - # Test trying to sign in. - sign_in_button.click() - - # Wait for the next page to fully load. - page.wait_for_load_state("domcontentloaded") - - # Check for the sign in form elements. - # NOTE: Playwright cannot find input elements by role and recommends using - # get_by_label() instead; however, hidden form elements do not have - # labels associated with them, hence the XPath! - # See https://playwright.dev/python/docs/api/class-page#page-get-by-label - # and https://playwright.dev/python/docs/locators#locate-by-css-or-xpath - # for more information. - email_address_input = page.get_by_label("Email address") - password_input = page.get_by_label("Password") - continue_button = page.get_by_role("button", name=re.compile("Continue")) - - # Sign in to the site. - email_address_input.fill(os.getenv("NOTIFY_E2E_TEST_EMAIL")) - password_input.fill(os.getenv("NOTIFY_E2E_TEST_PASSWORD")) - continue_button.click() - - # Wait for the next page to fully load. - page.wait_for_load_state("domcontentloaded") - - # Check for the sign in form elements. - # NOTE: Playwright cannot find input elements by role and recommends using - # get_by_label() instead; however, hidden form elements do not have - # labels associated with them, hence the XPath! - # See https://playwright.dev/python/docs/api/class-page#page-get-by-label - # and https://playwright.dev/python/docs/locators#locate-by-css-or-xpath - # for more information. - # mfa_input = page.get_by_label('Text message code') - # continue_button = page.get_by_role('button', name=re.compile('Continue')) - - # # Enter MFA code and continue. - # TODO: Revisit this at a later point in time. - # totp = pyotp.TOTP( - # os.getenv('MFA_TOTP_SECRET'), - # digits=int(os.getenv('MFA_TOTP_LENGTH')) - # ) - - # mfa_input.fill(totp.now()) - # continue_button.click() - - # page.wait_for_load_state('domcontentloaded') - - # Save storage state into the file. - auth_state_path = os.path.join( - os.getenv("NOTIFY_E2E_AUTH_STATE_PATH"), "state.json" - ) - context.storage_state(path=auth_state_path) - - -@pytest.fixture(scope="session") -def end_to_end_context(browser): - context = browser.new_context() - return context - - -@pytest.fixture(scope="session") -def end_to_end_authenticated_context(browser): - # Create and load a previously authenticated context for Playwright E2E - # tests. - # login_for_end_to_end_testing(browser) - - auth_state_path = os.path.join( - os.getenv("NOTIFY_E2E_AUTH_STATE_PATH"), "state.json" - ) - context = browser.new_context(storage_state=auth_state_path) - - return context - - @pytest.fixture() def fake_markdown_file(): input = "#Test" diff --git a/tests/end_to_end/__init__.py b/tests/end_to_end/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/end_to_end/conftest.py b/tests/end_to_end/conftest.py new file mode 100644 index 000000000..6abd2e231 --- /dev/null +++ b/tests/end_to_end/conftest.py @@ -0,0 +1,105 @@ +import os +import re + +import pytest + +E2E_TEST_URI = os.getenv("NOTIFY_E2E_TEST_URI") + + +def login_for_end_to_end_testing(browser): + # Open a new page and go to the staging site. + context = browser.new_context() + page = context.new_page() + page.goto(f"{E2E_TEST_URI}/") + + sign_in_button = page.get_by_role("link", name="Sign in") + + # Test trying to sign in. + sign_in_button.click() + + # Wait for the next page to fully load. + page.wait_for_load_state("domcontentloaded") + + # Check for the sign in form elements. + # NOTE: Playwright cannot find input elements by role and recommends using + # get_by_label() instead; however, hidden form elements do not have + # labels associated with them, hence the XPath! + # See https://playwright.dev/python/docs/api/class-page#page-get-by-label + # and https://playwright.dev/python/docs/locators#locate-by-css-or-xpath + # for more information. + email_address_input = page.get_by_label("Email address") + password_input = page.get_by_label("Password") + continue_button = page.get_by_role("button", name=re.compile("Continue")) + + # Sign in to the site. + email_address_input.fill(os.getenv("NOTIFY_E2E_TEST_EMAIL")) + password_input.fill(os.getenv("NOTIFY_E2E_TEST_PASSWORD")) + continue_button.click() + + # Wait for the next page to fully load. + page.wait_for_load_state("domcontentloaded") + + # Check for the sign in form elements. + # NOTE: Playwright cannot find input elements by role and recommends using + # get_by_label() instead; however, hidden form elements do not have + # labels associated with them, hence the XPath! + # See https://playwright.dev/python/docs/api/class-page#page-get-by-label + # and https://playwright.dev/python/docs/locators#locate-by-css-or-xpath + # for more information. + # mfa_input = page.get_by_label('Text message code') + # continue_button = page.get_by_role('button', name=re.compile('Continue')) + + # # Enter MFA code and continue. + # TODO: Revisit this at a later point in time. + # totp = pyotp.TOTP( + # os.getenv('MFA_TOTP_SECRET'), + # digits=int(os.getenv('MFA_TOTP_LENGTH')) + # ) + + # mfa_input.fill(totp.now()) + # continue_button.click() + + # page.wait_for_load_state('domcontentloaded') + + # Save storage state into the file. + auth_state_path = os.path.join( + os.getenv("NOTIFY_E2E_AUTH_STATE_PATH"), "state.json" + ) + context.storage_state(path=auth_state_path) + + +@pytest.fixture(scope="session") +def end_to_end_context(browser): + context = browser.new_context() + return context + + +@pytest.fixture(scope="session") +def end_to_end_authenticated_context(browser): + # Create and load a previously authenticated context for Playwright E2E + # tests. + # login_for_end_to_end_testing(browser) + + auth_state_path = os.path.join( + os.getenv("NOTIFY_E2E_AUTH_STATE_PATH"), "state.json" + ) + context = browser.new_context(storage_state=auth_state_path) + + return context + + +@pytest.fixture(scope="session") +def bypass_sign_in(end_to_end_context): + # Open a new page and go to the staging site. + page = end_to_end_context.new_page() + + page.goto(f"{E2E_TEST_URI}/") + + sign_in_button = page.get_by_role("link", name="Sign in") + + # Sign in to the site - E2E test accounts are set to flow through. + sign_in_button.click() + + # Wait for the next page to fully load. + page.wait_for_load_state("domcontentloaded") + return page diff --git a/tests/end_to_end/test_accounts_page.py b/tests/end_to_end/test_accounts_page.py index 8484e58b0..89357c7ed 100644 --- a/tests/end_to_end/test_accounts_page.py +++ b/tests/end_to_end/test_accounts_page.py @@ -7,24 +7,8 @@ from playwright.sync_api import expect E2E_TEST_URI = os.getenv("NOTIFY_E2E_TEST_URI") -def _bypass_sign_in(end_to_end_context): - # Open a new page and go to the staging site. - page = end_to_end_context.new_page() - - page.goto(f"{E2E_TEST_URI}/") - - sign_in_button = page.get_by_role("link", name="Sign in") - - # Sign in to the site - E2E test accounts are set to flow through. - sign_in_button.click() - - # Wait for the next page to fully load. - page.wait_for_load_state("domcontentloaded") - return page - - -def test_add_new_service_workflow(end_to_end_context): - page = _bypass_sign_in(end_to_end_context) +def test_add_new_service_workflow(bypass_sign_in, end_to_end_context): + page = bypass_sign_in(end_to_end_context) page.goto(f"{E2E_TEST_URI}/") # Wait for the next page to fully load.