Cleaned up readme, un-removed e2e test services, updated e2e test to cleanup better

This commit is contained in:
Alex Janousek
2025-10-20 10:38:42 -04:00
parent 484525477f
commit 18d58c69ca
16 changed files with 138 additions and 272 deletions

View File

@@ -1,7 +1,9 @@
import datetime
import os
import pytest
from axe_core_python.sync_playwright import Axe
from playwright.sync_api import Page
E2E_TEST_URI = os.getenv("NOTIFY_E2E_TEST_URI")
@@ -14,13 +16,9 @@ def end_to_end_context(browser):
@pytest.fixture
def authenticated_page(end_to_end_context):
# Open a new page and go to the site.
page = end_to_end_context.new_page()
page.goto(f"{E2E_TEST_URI}/sign-in")
# Wait for the next page to fully load.
page.wait_for_load_state("domcontentloaded")
return page
@@ -64,3 +62,98 @@ def _mock_common_api_calls(mocker):
# Optional: silence New Relic or other external integrations
mocker.patch("newrelic.agent.initialize", return_value=None)
@pytest.fixture
def e2e_test_service(authenticated_page: Page, end_to_end_context):
"""
Creates a test service for E2E tests with guaranteed cleanup.
This fixture ensures services are deleted even if tests fail, preventing
database pollution from abandoned E2E test services.
"""
page = authenticated_page
current_date_time = datetime.datetime.now()
service_name = "E2E Federal Test Service {now} - {browser_type}".format(
now=current_date_time.strftime("%m/%d/%Y %H:%M:%S"),
browser_type=end_to_end_context.browser.browser_type.name,
)
service_info = {
"name": service_name,
"page": page,
}
yield service_info
# runs even if test fails
try:
_cleanup_test_service(page, service_name)
except Exception as e:
print(f"Warning: Failed to cleanup service '{service_name}': {e}") # noqa: T201
@pytest.fixture
def e2e_created_service(e2e_test_service): # noqa: PT022
"""
Creates a test service AND completes the service creation workflow.
This fixture not only generates a unique service name but also creates
the service through the UI, providing a ready-to-use service for tests.
"""
service_info = e2e_test_service
page = service_info["page"]
service_name = service_info["name"]
page.goto(f"{E2E_TEST_URI}/accounts")
page.wait_for_load_state("domcontentloaded")
add_service_button = page.get_by_role("button", name="Add a new service")
add_service_button.click()
page.wait_for_load_state("domcontentloaded")
service_name_input = page.locator('xpath=//input[@name="name"]')
service_name_input.fill(service_name)
add_button = page.get_by_role("button", name="Add service")
add_button.click()
page.wait_for_load_state("domcontentloaded")
service_info["url"] = page.url
yield service_info
def _cleanup_test_service(page: Page, service_name: str):
"""
Delete a test service by navigating to its settings and clicking delete.
Args:
page: Playwright page object
service_name: Name of the service to delete
"""
try:
page.goto(f"{E2E_TEST_URI}/accounts")
page.wait_for_load_state("domcontentloaded")
service_link = page.get_by_role("link", name=service_name)
if service_link.count() > 0:
service_link.click()
page.wait_for_load_state("domcontentloaded")
page.click("text='Settings'")
page.wait_for_load_state("domcontentloaded")
page.click("text='Delete this service'")
page.wait_for_load_state("domcontentloaded")
page.click("text='Yes, delete'")
page.wait_for_load_state("domcontentloaded")
print(f"Successfully cleaned up test service: {service_name}") # noqa: T201
else:
print(f"Service '{service_name}' not found, may have been deleted already") # noqa: T201
except Exception as e:
raise Exception(f"Failed to cleanup service '{service_name}': {str(e)}")

View File

@@ -1,4 +1,3 @@
import datetime
import os
import re
@@ -9,15 +8,11 @@ from tests.end_to_end.conftest import check_axe_report
E2E_TEST_URI = os.getenv("NOTIFY_E2E_TEST_URI")
def test_add_new_service_workflow(authenticated_page, end_to_end_context):
page = authenticated_page
# Prepare for adding a new service later in the test.
current_date_time = datetime.datetime.now()
new_service_name = "E2E Federal Test Service {now} - {browser_type}".format(
now=current_date_time.strftime("%m/%d/%Y %H:%M:%S"),
browser_type=end_to_end_context.browser.browser_type.name,
)
def test_add_new_service_workflow(e2e_test_service):
"""Test creating and deleting a service with automatic cleanup."""
# Get service info from fixture (cleanup is automatic)
new_service_name = e2e_test_service["name"]
page = e2e_test_service["page"]
page.goto(f"{E2E_TEST_URI}/accounts")
@@ -81,24 +76,5 @@ def test_add_new_service_workflow(authenticated_page, end_to_end_context):
# expect(service_heading).to_be_visible()
expect(page).to_have_title(re.compile(new_service_name))
page.click("text='Settings'")
# Check to make sure that we've arrived at the next page.
page.wait_for_load_state("domcontentloaded")
check_axe_report(page)
page.click("text='Delete this service'")
# Check to make sure that we've arrived at the next page.
page.wait_for_load_state("domcontentloaded")
check_axe_report(page)
page.click("text='Yes, delete'")
# Check to make sure that we've arrived at the next page.
page.wait_for_load_state("domcontentloaded")
check_axe_report(page)
# Check to make sure that we've arrived at the next page.
# Check the page title exists and matches what we expect.
expect(page).to_have_title(re.compile("Choose service"))
# Service will be automatically cleaned up by the e2e_test_service fixture
# even if the test fails before reaching this point