@@ -805,6 +805,173 @@ async def test_expect_file_chooser_with_path_objects(self, tab):
805805 mock_enable_intercept .assert_called_once ()
806806 mock_disable_intercept .assert_called_once ()
807807
808+ @pytest .mark .asyncio
809+ async def test_expect_file_chooser_event_handler_single_file (self , tab ):
810+ """Test the real event_handler function with single file."""
811+ from pydoll .protocol .dom .types import EventFileChooserOpened
812+ from pydoll .protocol .page .events import PageEvent
813+
814+ # Mock execute_command to capture the call
815+ tab ._execute_command = AsyncMock ()
816+
817+ # Create mock event data
818+ mock_event : EventFileChooserOpened = {
819+ 'method' : 'Page.fileChooserOpened' ,
820+ 'params' : {
821+ 'frameId' : 'test-frame-id' ,
822+ 'mode' : 'selectSingle' ,
823+ 'backendNodeId' : 12345
824+ }
825+ }
826+
827+ # Capture the real event handler from expect_file_chooser
828+ captured_handler = None
829+
830+ async def mock_on (event_name , handler , temporary = False ):
831+ nonlocal captured_handler
832+ if event_name == PageEvent .FILE_CHOOSER_OPENED :
833+ captured_handler = handler
834+ return 123
835+
836+ # Mock the required methods
837+ with patch .object (tab , 'enable_page_events' , AsyncMock ()):
838+ with patch .object (tab , 'enable_intercept_file_chooser_dialog' , AsyncMock ()):
839+ with patch .object (tab , 'disable_intercept_file_chooser_dialog' , AsyncMock ()):
840+ with patch .object (tab , 'disable_page_events' , AsyncMock ()):
841+ with patch .object (tab , 'on' , mock_on ):
842+ async with tab .expect_file_chooser ('test.txt' ):
843+ # Execute the captured real handler
844+ assert captured_handler is not None
845+ await captured_handler (mock_event )
846+
847+ # Verify the command was called correctly
848+ tab ._execute_command .assert_called_once ()
849+ call_args = tab ._execute_command .call_args [0 ][0 ]
850+ assert call_args ['method' ] == 'DOM.setFileInputFiles'
851+ assert call_args ['params' ]['files' ] == ['test.txt' ]
852+ assert call_args ['params' ]['backendNodeId' ] == 12345
853+
854+ @pytest .mark .asyncio
855+ async def test_expect_file_chooser_event_handler_multiple_files (self , tab ):
856+ """Test the real event_handler function with multiple files."""
857+ from pydoll .protocol .dom .types import EventFileChooserOpened
858+ from pydoll .protocol .page .events import PageEvent
859+
860+ # Mock execute_command to capture the call
861+ tab ._execute_command = AsyncMock ()
862+
863+ # Create mock event data
864+ mock_event : EventFileChooserOpened = {
865+ 'method' : 'Page.fileChooserOpened' ,
866+ 'params' : {
867+ 'frameId' : 'test-frame-id' ,
868+ 'mode' : 'selectMultiple' ,
869+ 'backendNodeId' : 67890
870+ }
871+ }
872+
873+ # Capture the real event handler from expect_file_chooser
874+ captured_handler = None
875+
876+ async def mock_on (event_name , handler , temporary = False ):
877+ nonlocal captured_handler
878+ if event_name == PageEvent .FILE_CHOOSER_OPENED :
879+ captured_handler = handler
880+ return 123
881+
882+ # Mock the required methods
883+ with patch .object (tab , 'enable_page_events' , AsyncMock ()):
884+ with patch .object (tab , 'enable_intercept_file_chooser_dialog' , AsyncMock ()):
885+ with patch .object (tab , 'disable_intercept_file_chooser_dialog' , AsyncMock ()):
886+ with patch .object (tab , 'disable_page_events' , AsyncMock ()):
887+ with patch .object (tab , 'on' , mock_on ):
888+ async with tab .expect_file_chooser (['file1.txt' , 'file2.pdf' , 'file3.jpg' ]):
889+ # Execute the captured real handler
890+ assert captured_handler is not None
891+ await captured_handler (mock_event )
892+
893+ # Verify the command was called correctly
894+ tab ._execute_command .assert_called_once ()
895+ call_args = tab ._execute_command .call_args [0 ][0 ]
896+ assert call_args ['method' ] == 'DOM.setFileInputFiles'
897+ assert call_args ['params' ]['files' ] == ['file1.txt' , 'file2.pdf' , 'file3.jpg' ]
898+ assert call_args ['params' ]['backendNodeId' ] == 67890
899+
900+ async def _test_event_handler_with_files (self , tab , files , expected_files , backend_node_id ):
901+ """Helper method to test event handler with different file types."""
902+ from pydoll .protocol .dom .types import EventFileChooserOpened
903+ from pydoll .protocol .page .events import PageEvent
904+
905+ # Mock execute_command to capture the call
906+ tab ._execute_command = AsyncMock ()
907+
908+ # Create mock event data
909+ mock_event : EventFileChooserOpened = {
910+ 'method' : 'Page.fileChooserOpened' ,
911+ 'params' : {
912+ 'frameId' : 'test-frame-id' ,
913+ 'mode' : 'selectMultiple' ,
914+ 'backendNodeId' : backend_node_id
915+ }
916+ }
917+
918+ # Capture the real event handler from expect_file_chooser
919+ captured_handler = None
920+
921+ async def mock_on (event_name , handler , temporary = False ):
922+ nonlocal captured_handler
923+ if event_name == PageEvent .FILE_CHOOSER_OPENED :
924+ captured_handler = handler
925+ return 123
926+
927+ # Mock the required methods
928+ with patch .object (tab , 'enable_page_events' , AsyncMock ()):
929+ with patch .object (tab , 'enable_intercept_file_chooser_dialog' , AsyncMock ()):
930+ with patch .object (tab , 'disable_intercept_file_chooser_dialog' , AsyncMock ()):
931+ with patch .object (tab , 'disable_page_events' , AsyncMock ()):
932+ with patch .object (tab , 'on' , mock_on ):
933+ async with tab .expect_file_chooser (files ):
934+ # Execute the captured real handler
935+ assert captured_handler is not None
936+ await captured_handler (mock_event )
937+
938+ # Verify the command was called correctly
939+ tab ._execute_command .assert_called_once ()
940+ call_args = tab ._execute_command .call_args [0 ][0 ]
941+ assert call_args ['method' ] == 'DOM.setFileInputFiles'
942+ assert call_args ['params' ]['files' ] == expected_files
943+ assert call_args ['params' ]['backendNodeId' ] == backend_node_id
944+
945+ @pytest .mark .asyncio
946+ async def test_expect_file_chooser_event_handler_path_objects (self , tab ):
947+ """Test the real event_handler function with Path objects."""
948+ from pathlib import Path
949+
950+ files = [Path ('documents/file1.txt' ), Path ('images/file2.jpg' )]
951+ expected_files = ['documents/file1.txt' , 'images/file2.jpg' ]
952+
953+ await self ._test_event_handler_with_files (tab , files , expected_files , 54321 )
954+
955+ @pytest .mark .asyncio
956+ async def test_expect_file_chooser_event_handler_single_path_object (self , tab ):
957+ """Test the real event_handler function with single Path object."""
958+ from pathlib import Path
959+
960+ files = Path ('documents/important.pdf' )
961+ expected_files = ['documents/important.pdf' ]
962+
963+ await self ._test_event_handler_with_files (tab , files , expected_files , 98765 )
964+
965+ @pytest .mark .asyncio
966+ async def test_expect_file_chooser_event_handler_empty_list (self , tab ):
967+ """Test the real event_handler function with empty file list."""
968+ files = []
969+ expected_files = []
970+
971+ await self ._test_event_handler_with_files (tab , files , expected_files , 11111 )
972+
973+
974+
808975
809976class TestTabCloudflareBypass :
810977 """Test Tab Cloudflare bypass functionality."""
0 commit comments