Skip to content

Commit 0cd946d

Browse files
authored
Merge pull request #30 from Patolox/fix_file_extension
fix: Get file extension from file path and changes use of reserved word 'format' to 'fmt'
2 parents 2f46c78 + abdc33a commit 0cd946d

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

pydoll/browser/page.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pydoll.commands.storage import StorageCommands
1212
from pydoll.connection.connection import ConnectionHandler
1313
from pydoll.element import WebElement
14+
from pydoll.exceptions import InvalidFileExtension
1415
from pydoll.mixins.find_elements import FindElementsMixin
1516
from pydoll.utils import decode_image_to_bytes
1617

@@ -200,7 +201,13 @@ async def get_screenshot(self, path: str):
200201
Args:
201202
path (str): The file path to save the screenshot to.
202203
"""
203-
response = await self._execute_command(PageCommands.screenshot())
204+
fmt = path.split('.')[-1]
205+
if fmt not in {'jpeg', 'jpg', 'png'}:
206+
raise InvalidFileExtension(f'{fmt} extension is not supported.')
207+
208+
response = await self._execute_command(
209+
PageCommands.screenshot(fmt=fmt)
210+
)
204211
screenshot_b64 = response['result']['data'].encode('utf-8')
205212
screenshot_bytes = decode_image_to_bytes(screenshot_b64)
206213
async with aiofiles.open(path, 'wb') as file:

pydoll/commands/page.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ def set_download_path(cls, path: str) -> dict:
7171

7272
@classmethod
7373
def screenshot(
74-
cls, format: str = 'jpeg', quality: int = 100, clip: dict = None
74+
cls, fmt: str = 'jpeg', quality: int = 100, clip: dict = None
7575
) -> dict:
7676
"""
7777
Generates the command to capture a screenshot of the current page.
7878
7979
Args:
80-
format (str): The format of the image to be captured.
80+
fmt (str): The format of the image to be captured.
8181
Can be 'png' or 'jpeg'.
8282
quality (int): The quality of the image to be captured,
8383
applicable only if the format is 'jpeg'.
@@ -89,7 +89,7 @@ def screenshot(
8989
containing the method and parameters for the screenshot.
9090
"""
9191
command = cls.SCREENSHOT_TEMPLATE.copy()
92-
command['params']['format'] = format
92+
command['params']['format'] = fmt
9393
command['params']['quality'] = quality
9494
if clip:
9595
command['params']['clip'] = clip

pydoll/element.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ async def get_screenshot(self, path: str):
189189
'scale': 1,
190190
}
191191
screenshot = await self._connection_handler.execute_command(
192-
PageCommands.screenshot(format='jpeg', clip=clip)
192+
PageCommands.screenshot(fmt='jpeg', clip=clip)
193193
)
194194
async with aiofiles.open(path, 'wb') as file:
195195
image_bytes = decode_image_to_bytes(screenshot['result']['data'])

pydoll/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,10 @@ class ElementNotInteractable(Exception):
8080

8181
def __str__(self):
8282
return self.message
83+
84+
85+
class InvalidFileExtension(Exception):
86+
message = 'The file extension provided is not supported'
87+
88+
def __str__(self):
89+
return self.message

tests/test_page_commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_screenshot_jpeg():
3333
},
3434
}
3535
assert (
36-
PageCommands.screenshot(format='jpeg', quality=80) == expected_command
36+
PageCommands.screenshot(fmt='jpeg', quality=80) == expected_command
3737
)
3838

3939

@@ -45,7 +45,7 @@ def test_screenshot_png():
4545
'quality': 100,
4646
},
4747
}
48-
assert PageCommands.screenshot(format='png') == expected_command
48+
assert PageCommands.screenshot(fmt='png') == expected_command
4949

5050

5151
def test_screenshot_with_clip():

0 commit comments

Comments
 (0)