Skip to content

Commit be79e3f

Browse files
✅ - test: attempt to make the tests less flaky
1 parent 4a875cb commit be79e3f

File tree

1 file changed

+16
-5
lines changed
  • backend/src/openarchiefbeheer/utils/tests

1 file changed

+16
-5
lines changed

backend/src/openarchiefbeheer/utils/tests/gherkin.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Callable
33

44
from asgiref.sync import sync_to_async
5-
from playwright.async_api import expect
5+
from playwright.async_api import TimeoutError, expect
66

77
from openarchiefbeheer.accounts.tests.factories import UserFactory
88
from openarchiefbeheer.destruction.models import DestructionList
@@ -444,6 +444,9 @@ class Then:
444444
await self.then.page_should_contain_text(page, "Vernietigingslijsten")
445445
"""
446446

447+
# This indicates that the test is inverted (not_), this can be used to optimize tests.
448+
is_inverted = False
449+
447450
def __init__(self, testcase):
448451
self.testcase = testcase
449452

@@ -452,14 +455,15 @@ def not_(self):
452455
class InvertedThen:
453456
def __init__(self, then):
454457
self.then = then
458+
self.then.is_inverted = True
455459

456460
def __getattr__(self, item):
457461
method = getattr(self.then, item)
458462

459463
async def inverted_method(*args, **kwargs):
460464
try:
461465
await method(*args, **kwargs)
462-
except AssertionError:
466+
except (AssertionError, TimeoutError):
463467
return
464468

465469
raise AssertionError(
@@ -508,9 +512,16 @@ def get_number_of_items():
508512
count = await get_number_of_items()
509513
self.testcase.assertEqual(number_of_items, count)
510514

511-
async def page_should_contain_text(self, page, text):
512-
locator = page.get_by_text(text).nth(0)
513-
await expect(locator).to_be_visible()
515+
async def page_should_contain_text(self, page, text, timeout=None):
516+
if timeout is None:
517+
timeout = 500 if self.is_inverted else 10000
518+
519+
# Wait for the text to appear in the DOM
520+
await page.wait_for_selector(f"text={text}", timeout=timeout)
521+
522+
# Confirm the element with the text is visible
523+
element = page.locator(f"text={text}")
524+
await expect(element.nth(0)).to_be_visible(timeout=timeout)
514525

515526
async def path_should_be(self, page, path):
516527
await self.url_should_be(page, self.testcase.live_server_url + path)

0 commit comments

Comments
 (0)