@@ -250,16 +250,23 @@ def safe_accept_dialog(self, locator: Locator) -> None:
250250 except Exception as e :
251251 logging .error (f"Click failed: { e } " )
252252
253- def assert_dialog_text (self , expected_text : str ) -> None :
253+ def assert_dialog_text (self , expected_text : str , accept : bool = False ) -> None :
254254 """
255255 Asserts that a dialog appears and contains the expected text.
256256 If no dialog appears, logs an error.
257257 Args:
258258 expected_text (str): The text that should be present in the dialog.
259+ accept (bool): Set to True if you want to accept the dialog, by deafult is is set to False.
259260 """
260261 self ._dialog_assertion_error = None
261262
262- def handle_dialog (dialog : Dialog ):
263+ def handle_dialog (dialog : Dialog , accept : bool = False ):
264+ """
265+ Handles the dialog and asserts that the dialog contains the expected text.
266+ Args:
267+ dialog (Dialog): the playwright dialog object
268+ accept (bool): Set to True if you want to accept the dialog, by deafult is is set to False.
269+ """
263270 logging .info (f"Dialog appeared with message: { dialog .message } " )
264271 actual_text = dialog .message
265272 try :
@@ -268,9 +275,12 @@ def handle_dialog(dialog: Dialog):
268275 ), f"Expected '{ expected_text } ', but got '{ actual_text } '"
269276 except AssertionError as e :
270277 self ._dialog_assertion_error = e
271- dialog .dismiss () # Dismiss dialog
278+ if accept :
279+ dialog .accept ()
280+ else :
281+ dialog .dismiss () # Dismiss dialog
272282
273- self .page .once ("dialog" , handle_dialog )
283+ self .page .once ("dialog" , lambda dialog : handle_dialog ( dialog , accept ) )
274284
275285 def go_to_log_in_page (self ) -> None :
276286 """Click on the Log in button to navigate to the login page."""
0 commit comments