Skip to content

Commit 7ed7abc

Browse files
committed
test: add tests to new take_screenshot behavior
1 parent 90e6e27 commit 7ed7abc

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

tests/test_browser/test_browser_tab.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ async def test_print_to_pdf_as_base64(self, tab):
464464
'result': {'data': pdf_data}
465465
}
466466

467-
result = await tab.print_to_pdf('', as_base64=True)
467+
result = await tab.print_to_pdf(as_base64=True)
468468

469469
assert result == pdf_data
470470
assert_mock_called_at_least_once(tab._connection_handler)
@@ -1779,17 +1779,15 @@ async def test_take_screenshot_invalid_extension(self, tab):
17791779

17801780
@pytest.mark.asyncio
17811781
async def test_print_to_pdf_with_invalid_path(self, tab):
1782-
"""Test print_to_pdf with invalid path handling."""
1782+
"""Test print_to_pdf with missing path when not using base64."""
17831783
# Mock the response
17841784
tab._connection_handler.execute_command.return_value = {
17851785
'result': {'data': 'JVBERi0xLjQKJdPr6eEKMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwo+PgplbmRvYmoKdHJhaWxlcgo8PAovUm9vdCAxIDAgUgo+PgpzdGFydHhyZWYKMTgKJSVFT0Y='}
17861786
}
17871787

1788-
# Should not raise exception - print_to_pdf doesn't validate extensions
1789-
result = await tab.print_to_pdf('document.txt', as_base64=True)
1790-
1791-
assert result is not None
1792-
assert_mock_called_at_least_once(tab._connection_handler)
1788+
# Should raise ValueError when path is not provided and as_base64=False
1789+
with pytest.raises(ValueError, match="path is required when as_base64=False"):
1790+
await tab.print_to_pdf(as_base64=False)
17931791

17941792
@pytest.mark.asyncio
17951793
async def test_execute_script_with_none_element(self, tab):

tests/test_web_element.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

671722
class TestWebElementVisibility:
672723
"""Test element visibility and interaction checks."""

0 commit comments

Comments
 (0)