Skip to content

Commit 35480ce

Browse files
authored
api(snake): go snake (microsoft#375)
1 parent 345dc10 commit 35480ce

File tree

90 files changed

+3642
-3401
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+3642
-3401
lines changed

buildbots/assets/stub.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
with sync_playwright() as p:
44
for browser_type in [p.chromium, p.firefox, p.webkit]:
55
browser = browser_type.launch()
6-
page = browser.newPage()
7-
page.setContent("<h1>Test 123</h1>")
6+
page = browser.new_page()
7+
page.set_content("<h1>Test 123</h1>")
88
page.screenshot(path=f"{browser_type.name}.png")
99
browser.close()

client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
def main(playwright: Playwright) -> None:
2020
browser = playwright.chromium.launch(headless=False)
21-
page = browser.newPage(viewport=0)
22-
page.setContent(
21+
page = browser.new_page(viewport=0)
22+
page.set_content(
2323
"<button id=button onclick=\"window.open('http://webkit.org', '_blank')\">Click me</input>"
2424
)
2525

@@ -29,12 +29,12 @@ def main(playwright: Playwright) -> None:
2929

3030
print("Contexts in browser: %d" % len(browser.contexts))
3131
print("Creating context...")
32-
context = browser.newContext(viewport=0)
32+
context = browser.new_context(viewport=0)
3333
print("Contexts in browser: %d" % len(browser.contexts))
3434
print("Pages in context: %d" % len(context.pages))
3535

3636
print("\nCreating page1...")
37-
page1 = context.newPage()
37+
page1 = context.new_page()
3838
print("Pages in context: %d" % len(context.pages))
3939
page1.on("framenavigated", lambda frame: print("Frame navigated to %s" % frame.url))
4040
page1.on("request", lambda request: print("Request %s" % request.url))
@@ -50,13 +50,13 @@ def main(playwright: Playwright) -> None:
5050
)
5151
print("Navigating page1 to https://example.com...")
5252
page1.goto("https://example.com")
53-
print("Page1 main frame url: %s" % page1.mainFrame.url)
53+
print("Page1 main frame url: %s" % page1.main_frame.url)
5454
print("Page1 tile: %s" % page1.title())
5555
print("Frames in page1: %d" % len(page1.frames))
5656
page1.screenshot(path="example.png")
5757

5858
print("\nCreating page2...")
59-
page2 = context.newPage()
59+
page2 = context.new_page()
6060
page2.on("framenavigated", lambda frame: print("Frame navigated to %s" % frame.url))
6161

6262
print("Navigating page2 to https://webkit.org...")
@@ -65,17 +65,17 @@ def main(playwright: Playwright) -> None:
6565
print("Pages in context: %d" % len(context.pages))
6666

6767
print("\nQuerying body...")
68-
body1 = page1.querySelector("body")
68+
body1 = page1.query_selector("body")
6969
assert body1
70-
print("Body text %s" % body1.textContent())
70+
print("Body text %s" % body1.text_content())
7171

7272
print("Closing page1...")
7373
page1.close()
7474
print("Pages in context: %d" % len(context.pages))
7575

7676
print("Navigating page2 to https://cnn.com...")
7777
page2.goto("https://cnn.com")
78-
print("Page2 main frame url: %s" % page2.mainFrame.url)
78+
print("Page2 main frame url: %s" % page2.main_frame.url)
7979
print("Page2 tile: %s" % page2.title())
8080
print("Frames in page2: %d" % len(page2.frames))
8181
print("Pages in context: %d" % len(context.pages))

playwright/__init__.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@
1919
and for the async API [here](async_api.html).
2020
"""
2121

22-
import playwright._types as types
22+
import playwright._api_structures as api_structures
23+
import playwright._api_types as api_types
2324
from playwright._main import AsyncPlaywrightContextManager, SyncPlaywrightContextManager
2425

25-
ConsoleMessageLocation = types.ConsoleMessageLocation
26-
Cookie = types.Cookie
27-
Credentials = types.Credentials
28-
DeviceDescriptor = types.DeviceDescriptor
29-
Error = types.Error
30-
FilePayload = types.FilePayload
31-
FloatRect = types.FloatRect
32-
Geolocation = types.Geolocation
33-
IntSize = types.IntSize
34-
MousePosition = types.MousePosition
35-
PdfMargins = types.PdfMargins
36-
ProxyServer = types.ProxyServer
37-
RecordHarOptions = types.RecordHarOptions
38-
RecordVideoOptions = types.RecordVideoOptions
39-
RequestFailure = types.RequestFailure
40-
ResourceTiming = types.ResourceTiming
41-
SelectOption = types.SelectOption
42-
StorageState = types.StorageState
43-
TimeoutError = types.TimeoutError
26+
DeviceDescriptor = api_types.DeviceDescriptor
27+
Error = api_types.Error
28+
FilePayload = api_types.FilePayload
29+
FloatRect = api_types.FloatRect
30+
Geolocation = api_types.Geolocation
31+
HttpCredentials = api_types.HttpCredentials
32+
PdfMargins = api_types.PdfMargins
33+
ProxySettings = api_types.ProxySettings
34+
RecordHarOptions = api_types.RecordHarOptions
35+
RecordVideoOptions = api_types.RecordVideoOptions
36+
RequestFailure = api_types.RequestFailure
37+
OptionSelector = api_types.OptionSelector
38+
SourceLocation = api_types.SourceLocation
39+
TimeoutError = api_types.TimeoutError
40+
41+
Cookie = api_structures.Cookie
42+
ResourceTiming = api_structures.ResourceTiming
43+
StorageState = api_structures.StorageState
4444

4545

4646
def async_playwright() -> AsyncPlaywrightContextManager:
@@ -54,23 +54,21 @@ def sync_playwright() -> SyncPlaywrightContextManager:
5454
__all__ = [
5555
"async_playwright",
5656
"sync_playwright",
57-
"ConsoleMessageLocation",
5857
"Cookie",
59-
"Credentials",
58+
"HttpCredentials",
6059
"DeviceDescriptor",
6160
"Error",
6261
"FilePayload",
6362
"FloatRect",
6463
"Geolocation",
65-
"IntSize",
66-
"MousePosition",
6764
"PdfMargins",
68-
"ProxyServer",
65+
"ProxySettings",
6966
"RecordHarOptions",
7067
"RecordVideoOptions",
7168
"RequestFailure",
7269
"ResourceTiming",
73-
"SelectOption",
70+
"OptionSelector",
71+
"SourceLocation",
7472
"StorageState",
7573
"TimeoutError",
7674
]

playwright/_api_structures.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import sys
16+
from typing import Dict, List, Optional
17+
18+
if sys.version_info >= (3, 8): # pragma: no cover
19+
from typing import Literal, TypedDict
20+
else: # pragma: no cover
21+
from typing_extensions import Literal, TypedDict
22+
23+
# These are the structures that we like keeping in a JSON form for their potential reuse between SDKs / services.
24+
25+
# Explicitly mark optional params as such for the documentation
26+
# If there is at least one optional param, set total=False for better mypy handling.
27+
28+
29+
class Cookie(TypedDict, total=False):
30+
name: str
31+
value: str
32+
url: Optional[str]
33+
domain: Optional[str]
34+
path: Optional[str]
35+
expires: Optional[int]
36+
httpOnly: Optional[bool]
37+
secure: Optional[bool]
38+
sameSite: Optional[Literal["Strict", "Lax", "None"]]
39+
40+
41+
class StorageState(TypedDict, total=False):
42+
cookies: Optional[List[Cookie]]
43+
origins: Optional[List[Dict]]
44+
45+
46+
class ResourceTiming(TypedDict):
47+
startTime: float
48+
domainLookupStart: float
49+
domainLookupEnd: float
50+
connectStart: float
51+
secureConnectionStart: float
52+
connectEnd: float
53+
requestStart: float
54+
responseStart: float
55+
responseEnd: float

0 commit comments

Comments
 (0)