@@ -631,7 +631,7 @@ async def test_take_screenshot_success(self, web_element, tmp_path):
631631 {'result' : {'data' : screenshot_data }}, # capture_screenshot
632632 ]
633633
634- screenshot_path = tmp_path / 'element.jpg '
634+ screenshot_path = tmp_path / 'element.jpeg '
635635
636636 # Mock aiofiles.open properly for async context manager
637637 mock_file = AsyncMock ()
@@ -654,7 +654,7 @@ async def test_take_screenshot_default_quality(self, web_element, tmp_path):
654654 {'result' : {'data' : screenshot_data }},
655655 ]
656656
657- screenshot_path = tmp_path / 'element_default.jpg '
657+ screenshot_path = tmp_path / 'element_default.jpeg '
658658
659659 # Mock aiofiles.open properly for async context manager
660660 mock_file = AsyncMock ()
@@ -667,6 +667,57 @@ async def test_take_screenshot_default_quality(self, web_element, tmp_path):
667667 # Should call get_bounds_using_js and capture_screenshot
668668 assert web_element ._connection_handler .execute_command .call_count == 2
669669
670+ @pytest .mark .asyncio
671+ async def test_take_screenshot_as_base64 (self , web_element ):
672+ """Test screenshot returned as base64 string."""
673+ bounds = {'x' : 10 , 'y' : 20 , 'width' : 100 , 'height' : 50 }
674+ screenshot_data = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wcAAgAB/edzE+oAAAAASUVORK5CYII='
675+
676+ web_element ._connection_handler .execute_command .side_effect = [
677+ {'result' : {'result' : {'value' : json .dumps (bounds )}}}, # get_bounds_using_js
678+ {'result' : {'data' : screenshot_data }}, # capture_screenshot
679+ ]
680+
681+ # Take screenshot as base64
682+ result = await web_element .take_screenshot (as_base64 = True )
683+
684+ # Should return the base64 data
685+ assert result == screenshot_data
686+ # Should call get_bounds_using_js and capture_screenshot
687+ assert web_element ._connection_handler .execute_command .call_count == 2
688+
689+ @pytest .mark .asyncio
690+ async def test_take_screenshot_missing_path_without_base64 (self , web_element ):
691+ """Test screenshot raises error when no path and as_base64=False."""
692+ from pydoll .exceptions import MissingScreenshotPath
693+
694+ with pytest .raises (MissingScreenshotPath ):
695+ await web_element .take_screenshot (as_base64 = False )
696+
697+ @pytest .mark .asyncio
698+ async def test_take_screenshot_jpg_alias (self , web_element , tmp_path ):
699+ """Test that .jpg extension works as alias for .jpeg."""
700+ bounds = {'x' : 10 , 'y' : 20 , 'width' : 100 , 'height' : 50 }
701+ screenshot_data = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wcAAgAB/edzE+oAAAAASUVORK5CYII='
702+
703+ web_element ._connection_handler .execute_command .side_effect = [
704+ {'result' : {'result' : {'value' : json .dumps (bounds )}}}, # get_bounds_using_js
705+ {'result' : {'data' : screenshot_data }}, # capture_screenshot
706+ ]
707+
708+ screenshot_path = tmp_path / 'element.jpg'
709+
710+ # Mock aiofiles.open properly for async context manager
711+ mock_file = AsyncMock ()
712+ mock_file .write = AsyncMock ()
713+
714+ with patch ('aiofiles.open' ) as mock_aiofiles_open :
715+ mock_aiofiles_open .return_value .__aenter__ .return_value = mock_file
716+ await web_element .take_screenshot (str (screenshot_path ), quality = 90 )
717+
718+ # Should work without raising InvalidFileExtension
719+ assert web_element ._connection_handler .execute_command .call_count == 2
720+
670721
671722class TestWebElementVisibility :
672723 """Test element visibility and interaction checks."""
0 commit comments