@@ -807,6 +807,41 @@ 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 ) > 1 )
829+
830+ assert len (events_received ) == 2
831+
832+ download_event = events_received [0 ]
833+ assert download_event .download_params is not None
834+ assert download_event .download_params .status == "complete"
835+ assert download_event .download_params .context == context_id
836+ assert download_event .download_params .timestamp is not None
837+ assert "downloads/file_1.txt" in download_event .download_params .url
838+ # we assert that atleast the str "file_1" is present in the downloaded file since multiple downloads
839+ # will have numbered suffix like file_1 (1)
840+ assert "file_1" in download_event .download_params .filepath
841+
842+ driver .browsing_context .remove_event_handler ("download_end" , callback_id )
843+
844+
810845def test_add_event_handler_with_specific_contexts (driver ):
811846 """Test adding event handler with specific browsing contexts."""
812847 events_received = []
0 commit comments