@@ -807,6 +807,49 @@ def on_download_will_begin(info):
807807 driver .browsing_context .remove_event_handler ("download_will_begin" , callback_id )
808808
809809
810+ @pytest .mark .xfail_firefox
811+ def test_add_event_handler_download_end (driver , pages ):
812+ """Test adding event handler for download_end event."""
813+ events_received = []
814+
815+ def on_download_end (info ):
816+ events_received .append (info )
817+
818+ callback_id = driver .browsing_context .add_event_handler ("download_end" , on_download_end )
819+ assert callback_id is not None
820+
821+ context_id = driver .current_window_handle
822+ url = pages .url ("downloads/download.html" )
823+ driver .browsing_context .navigate (context = context_id , url = url , wait = ReadinessState .COMPLETE )
824+
825+ driver .find_element (By .ID , "file-1" ).click ()
826+
827+ driver .find_element (By .ID , "file-2" ).click ()
828+ WebDriverWait (driver , 5 ).until (lambda d : len (events_received ) == 2 )
829+
830+ assert len (events_received ) == 2
831+
832+ for ev in events_received :
833+ assert ev .download_params is not None
834+ assert ev .download_params .status == "complete"
835+ assert ev .download_params .context == context_id
836+ assert ev .download_params .timestamp is not None
837+
838+ # we assert that atleast "file_1" is present in the downloaded file since multiple downloads
839+ # will have numbered suffix like file_1 (1)
840+ assert any (
841+ "downloads/file_1.txt" in ev .download_params .url and "file_1" in ev .download_params .filepath
842+ for ev in events_received
843+ )
844+
845+ assert any (
846+ "downloads/file_2.jpg" in ev .download_params .url and "file_2" in ev .download_params .filepath
847+ for ev in events_received
848+ )
849+
850+ driver .browsing_context .remove_event_handler ("download_end" , callback_id )
851+
852+
810853def test_add_event_handler_with_specific_contexts (driver ):
811854 """Test adding event handler with specific browsing contexts."""
812855 events_received = []
0 commit comments