2
2
from typing import Callable
3
3
4
4
from asgiref .sync import sync_to_async
5
- from playwright .async_api import expect
5
+ from playwright .async_api import TimeoutError , expect
6
6
7
7
from openarchiefbeheer .accounts .tests .factories import UserFactory
8
8
from openarchiefbeheer .destruction .models import DestructionList
@@ -444,6 +444,9 @@ class Then:
444
444
await self.then.page_should_contain_text(page, "Vernietigingslijsten")
445
445
"""
446
446
447
+ # This indicates that the test is inverted (not_), this can be used to optimize tests.
448
+ is_inverted = False
449
+
447
450
def __init__ (self , testcase ):
448
451
self .testcase = testcase
449
452
@@ -452,14 +455,15 @@ def not_(self):
452
455
class InvertedThen :
453
456
def __init__ (self , then ):
454
457
self .then = then
458
+ self .then .is_inverted = True
455
459
456
460
def __getattr__ (self , item ):
457
461
method = getattr (self .then , item )
458
462
459
463
async def inverted_method (* args , ** kwargs ):
460
464
try :
461
465
await method (* args , ** kwargs )
462
- except AssertionError :
466
+ except ( AssertionError , TimeoutError ) :
463
467
return
464
468
465
469
raise AssertionError (
@@ -508,9 +512,16 @@ def get_number_of_items():
508
512
count = await get_number_of_items ()
509
513
self .testcase .assertEqual (number_of_items , count )
510
514
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 )
514
525
515
526
async def path_should_be (self , page , path ):
516
527
await self .url_should_be (page , self .testcase .live_server_url + path )
0 commit comments