from collections.abc import Callable from typing import Any import pytest from tests.browser.conftest import ALL, assert_accessible, no_horizontal_scroll @pytest.fixture def customer(admin_user: Any) -> Any: from apps.customers.infrastructure.orm import BillingUnitModel, ContactModel, CustomerModel c = CustomerModel.objects.create( name="Oy Esimerkki Ab", type="company", business_id="0112038-9" ) ContactModel.objects.create(customer=c, name="Maija Meikäläinen", email="maija@example.test") BillingUnitModel.objects.create( customer=c, name="Pääkonttori", e_invoice_address="003701120389", e_invoice_operator="003721291126", ) return c @pytest.mark.parametrize("themed_page", ALL, indirect=True, ids=lambda p: f"{p[0]}-{p[1]}") def test_customer_pages( themed_page: Any, live_server: Any, admin_user: Any, customer: Any, login: Callable[[Any, str], None], ) -> None: login(themed_page, admin_user.email) for path in ( "/customers/", "/customers/new/", f"/customers/{customer.pk}/", f"/customers/{customer.pk}/edit/", ): themed_page.goto(f"{live_server.url}{path}") assert_accessible(themed_page) assert no_horizontal_scroll(themed_page), path # The contact dialog (loaded with HTMX) is accessible too. themed_page.goto(f"{live_server.url}/customers/{customer.pk}/") themed_page.get_by_role("link", name="Lisää yhteyshenkilö").click() themed_page.locator("#item-dialog[open] #id_name").wait_for() themed_page.wait_for_timeout(300) assert_accessible(themed_page) def test_add_contact_with_keyboard( page: Any, live_server: Any, admin_user: Any, customer: Any, login: Callable[[Any, str], None] ) -> None: login(page, admin_user.email) page.goto(f"{live_server.url}/customers/{customer.pk}/") page.get_by_role("link", name="Lisää yhteyshenkilö").focus() page.keyboard.press("Enter") name = page.locator("#item-dialog[open] #id_name") name.wait_for() # Submitting empty shows the error and keeps focus in the dialog on the invalid field. page.locator("#item-dialog button[type=submit]").focus() page.keyboard.press("Enter") page.wait_for_selector("#id_name[aria-invalid='true']") # Focus moves in htmx:afterSettle, a moment after the swap. page.wait_for_function("document.activeElement && document.activeElement.id === 'id_name'") page.keyboard.type("Pekka Puhelin") page.keyboard.press("Enter") page.wait_for_function( "document.getElementById('live-region').textContent.includes('tallennettu')" ) assert page.evaluate("document.activeElement.id") == "contacts-heading" assert not page.evaluate("document.getElementById('item-dialog').open") assert "Pekka Puhelin" in page.locator("#contacts-section").inner_text()