Skip to content

Commit e932173

Browse files
committed
fix: support Path objects in Page.screenshot
1 parent 759eec8 commit e932173

File tree

6 files changed

+48
-25
lines changed

6 files changed

+48
-25
lines changed

playwright/async_api.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ async def screenshot(
12951295
self,
12961296
timeout: int = None,
12971297
type: Literal["png", "jpeg"] = None,
1298-
path: str = None,
1298+
path: typing.Union[str, pathlib.Path] = None,
12991299
quality: int = None,
13001300
omitBackground: bool = None,
13011301
) -> bytes:
@@ -1309,7 +1309,7 @@ async def screenshot(
13091309
Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout) methods.
13101310
type : Optional[Literal['png', 'jpeg']]
13111311
Specify screenshot type, defaults to `png`.
1312-
path : Optional[str]
1312+
path : Union[str, pathlib.Path, NoneType]
13131313
The file path to save the image to. The screenshot type will be inferred from file extension. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the image won't be saved to the disk.
13141314
quality : Optional[int]
13151315
The quality of the image, between 0-100. Not applicable to `png` images.
@@ -2039,7 +2039,11 @@ def isDetached(self) -> bool:
20392039
return mapping.from_maybe_impl(self._impl_obj.isDetached())
20402040

20412041
async def addScriptTag(
2042-
self, url: str = None, path: str = None, content: str = None, type: str = None
2042+
self,
2043+
url: str = None,
2044+
path: typing.Union[str, pathlib.Path] = None,
2045+
content: str = None,
2046+
type: str = None,
20432047
) -> "ElementHandle":
20442048
"""Frame.addScriptTag
20452049
@@ -2049,7 +2053,7 @@ async def addScriptTag(
20492053
----------
20502054
url : Optional[str]
20512055
URL of a script to be added.
2052-
path : Optional[str]
2056+
path : Union[str, pathlib.Path, NoneType]
20532057
Path to the JavaScript file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory.
20542058
content : Optional[str]
20552059
Raw JavaScript content to be injected into frame.
@@ -2068,7 +2072,10 @@ async def addScriptTag(
20682072
)
20692073

20702074
async def addStyleTag(
2071-
self, url: str = None, path: str = None, content: str = None
2075+
self,
2076+
url: str = None,
2077+
path: typing.Union[str, pathlib.Path] = None,
2078+
content: str = None,
20722079
) -> "ElementHandle":
20732080
"""Frame.addStyleTag
20742081
@@ -2078,7 +2085,7 @@ async def addStyleTag(
20782085
----------
20792086
url : Optional[str]
20802087
URL of the `<link>` tag.
2081-
path : Optional[str]
2088+
path : Union[str, pathlib.Path, NoneType]
20822089
Path to the CSS file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory.
20832090
content : Optional[str]
20842091
Raw CSS content to be injected into frame.
@@ -3966,7 +3973,7 @@ async def screenshot(
39663973
self,
39673974
timeout: int = None,
39683975
type: Literal["png", "jpeg"] = None,
3969-
path: str = None,
3976+
path: typing.Union[str, pathlib.Path] = None,
39703977
quality: int = None,
39713978
omitBackground: bool = None,
39723979
fullPage: bool = None,
@@ -3982,7 +3989,7 @@ async def screenshot(
39823989
Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout) methods.
39833990
type : Optional[Literal['png', 'jpeg']]
39843991
Specify screenshot type, defaults to `png`.
3985-
path : Optional[str]
3992+
path : Union[str, pathlib.Path, NoneType]
39863993
The file path to save the image to. The screenshot type will be inferred from file extension. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the image won't be saved to the disk.
39873994
quality : Optional[int]
39883995
The quality of the image, between 0-100. Not applicable to `png` images.
@@ -5725,7 +5732,7 @@ async def launchServer(
57255732

57265733
async def launchPersistentContext(
57275734
self,
5728-
userDataDir: str,
5735+
userDataDir: typing.Union[str, pathlib.Path],
57295736
executablePath: str = None,
57305737
args: typing.List[str] = None,
57315738
ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None,
@@ -5764,7 +5771,7 @@ async def launchPersistentContext(
57645771
57655772
Parameters
57665773
----------
5767-
userDataDir : str
5774+
userDataDir : Union[str, pathlib.Path]
57685775
Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for Chromium and Firefox.
57695776
executablePath : Optional[str]
57705777
Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is resolved relative to current working directory. **BEWARE**: Playwright is only guaranteed to work with the bundled Chromium, Firefox or WebKit, use at your own risk.

playwright/browser_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async def launchServer(
100100

101101
async def launchPersistentContext(
102102
self,
103-
userDataDir: str,
103+
userDataDir: Union[str, Path],
104104
executablePath: str = None,
105105
args: List[str] = None,
106106
ignoreDefaultArgs: Union[bool, List[str]] = None,

playwright/element_handle.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,12 @@ async def screenshot(
175175
self,
176176
timeout: int = None,
177177
type: Literal["png", "jpeg"] = None,
178-
path: str = None,
178+
path: Union[str, Path] = None,
179179
quality: int = None,
180180
omitBackground: bool = None,
181181
) -> bytes:
182+
if path:
183+
path = str(Path(path))
182184
binary = await self._channel.send("screenshot", locals_to_params(locals()))
183185
return base64.b64decode(binary)
184186

playwright/frame.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,11 @@ def isDetached(self) -> bool:
314314
return self._detached
315315

316316
async def addScriptTag(
317-
self, url: str = None, path: str = None, content: str = None, type: str = None,
317+
self,
318+
url: str = None,
319+
path: Union[str, Path] = None,
320+
content: str = None,
321+
type: str = None,
318322
) -> ElementHandle:
319323
params = locals_to_params(locals())
320324
if path:
@@ -324,7 +328,7 @@ async def addScriptTag(
324328
return from_channel(await self._channel.send("addScriptTag", params))
325329

326330
async def addStyleTag(
327-
self, url: str = None, path: str = None, content: str = None
331+
self, url: str = None, path: Union[str, Path] = None, content: str = None
328332
) -> ElementHandle:
329333
params = locals_to_params(locals())
330334
if path:

playwright/page.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import asyncio
1616
import base64
1717
import sys
18+
from pathlib import Path
1819
from types import SimpleNamespace
1920
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Union, cast
2021

@@ -533,12 +534,14 @@ async def screenshot(
533534
self,
534535
timeout: int = None,
535536
type: Literal["png", "jpeg"] = None,
536-
path: str = None,
537+
path: Union[str, Path] = None,
537538
quality: int = None,
538539
omitBackground: bool = None,
539540
fullPage: bool = None,
540541
clip: FloatRect = None,
541542
) -> bytes:
543+
if path:
544+
path = str(Path(path))
542545
binary = await self._channel.send("screenshot", locals_to_params(locals()))
543546
return base64.b64decode(binary)
544547

playwright/sync_api.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ def screenshot(
13431343
self,
13441344
timeout: int = None,
13451345
type: Literal["png", "jpeg"] = None,
1346-
path: str = None,
1346+
path: typing.Union[str, pathlib.Path] = None,
13471347
quality: int = None,
13481348
omitBackground: bool = None,
13491349
) -> bytes:
@@ -1357,7 +1357,7 @@ def screenshot(
13571357
Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout) methods.
13581358
type : Optional[Literal['png', 'jpeg']]
13591359
Specify screenshot type, defaults to `png`.
1360-
path : Optional[str]
1360+
path : Union[str, pathlib.Path, NoneType]
13611361
The file path to save the image to. The screenshot type will be inferred from file extension. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the image won't be saved to the disk.
13621362
quality : Optional[int]
13631363
The quality of the image, between 0-100. Not applicable to `png` images.
@@ -2115,7 +2115,11 @@ def isDetached(self) -> bool:
21152115
return mapping.from_maybe_impl(self._impl_obj.isDetached())
21162116

21172117
def addScriptTag(
2118-
self, url: str = None, path: str = None, content: str = None, type: str = None
2118+
self,
2119+
url: str = None,
2120+
path: typing.Union[str, pathlib.Path] = None,
2121+
content: str = None,
2122+
type: str = None,
21192123
) -> "ElementHandle":
21202124
"""Frame.addScriptTag
21212125
@@ -2125,7 +2129,7 @@ def addScriptTag(
21252129
----------
21262130
url : Optional[str]
21272131
URL of a script to be added.
2128-
path : Optional[str]
2132+
path : Union[str, pathlib.Path, NoneType]
21292133
Path to the JavaScript file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory.
21302134
content : Optional[str]
21312135
Raw JavaScript content to be injected into frame.
@@ -2146,7 +2150,10 @@ def addScriptTag(
21462150
)
21472151

21482152
def addStyleTag(
2149-
self, url: str = None, path: str = None, content: str = None
2153+
self,
2154+
url: str = None,
2155+
path: typing.Union[str, pathlib.Path] = None,
2156+
content: str = None,
21502157
) -> "ElementHandle":
21512158
"""Frame.addStyleTag
21522159
@@ -2156,7 +2163,7 @@ def addStyleTag(
21562163
----------
21572164
url : Optional[str]
21582165
URL of the `<link>` tag.
2159-
path : Optional[str]
2166+
path : Union[str, pathlib.Path, NoneType]
21602167
Path to the CSS file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory.
21612168
content : Optional[str]
21622169
Raw CSS content to be injected into frame.
@@ -4134,7 +4141,7 @@ def screenshot(
41344141
self,
41354142
timeout: int = None,
41364143
type: Literal["png", "jpeg"] = None,
4137-
path: str = None,
4144+
path: typing.Union[str, pathlib.Path] = None,
41384145
quality: int = None,
41394146
omitBackground: bool = None,
41404147
fullPage: bool = None,
@@ -4150,7 +4157,7 @@ def screenshot(
41504157
Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout) methods.
41514158
type : Optional[Literal['png', 'jpeg']]
41524159
Specify screenshot type, defaults to `png`.
4153-
path : Optional[str]
4160+
path : Union[str, pathlib.Path, NoneType]
41544161
The file path to save the image to. The screenshot type will be inferred from file extension. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the image won't be saved to the disk.
41554162
quality : Optional[int]
41564163
The quality of the image, between 0-100. Not applicable to `png` images.
@@ -5961,7 +5968,7 @@ def launchServer(
59615968

59625969
def launchPersistentContext(
59635970
self,
5964-
userDataDir: str,
5971+
userDataDir: typing.Union[str, pathlib.Path],
59655972
executablePath: str = None,
59665973
args: typing.List[str] = None,
59675974
ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None,
@@ -6000,7 +6007,7 @@ def launchPersistentContext(
60006007
60016008
Parameters
60026009
----------
6003-
userDataDir : str
6010+
userDataDir : Union[str, pathlib.Path]
60046011
Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for Chromium and Firefox.
60056012
executablePath : Optional[str]
60066013
Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is resolved relative to current working directory. **BEWARE**: Playwright is only guaranteed to work with the bundled Chromium, Firefox or WebKit, use at your own risk.

0 commit comments

Comments
 (0)