first commit

This commit is contained in:
jianxin committed 2025-01-24 21:03:41 +08:00
commit 8757705ea7
1488 files changed
+325179

No files matched your search

@@ -0,0 +1,196 @@
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Python package `playwright` is a Python library to automate Chromium,
Firefox and WebKit with a single API. Playwright is built to enable cross-browser
web automation that is ever-green, capable, reliable and fast.
"""
from typing import Any, Optional, Union, overload
import playwright._impl._api_structures
import playwright._impl._errors
import playwright.sync_api._generated
from playwright._impl._assertions import (
APIResponseAssertions as APIResponseAssertionsImpl,
)
from playwright._impl._assertions import LocatorAssertions as LocatorAssertionsImpl
from playwright._impl._assertions import PageAssertions as PageAssertionsImpl
from playwright.sync_api._context_manager import PlaywrightContextManager
from playwright.sync_api._generated import (
Accessibility,
APIRequest,
APIRequestContext,
APIResponse,
APIResponseAssertions,
Browser,
BrowserContext,
BrowserType,
CDPSession,
ConsoleMessage,
Dialog,
Download,
ElementHandle,
FileChooser,
Frame,
FrameLocator,
JSHandle,
Keyboard,
Locator,
LocatorAssertions,
Mouse,
Page,
PageAssertions,
Playwright,
Request,
Response,
Route,
Selectors,
Touchscreen,
Video,
WebSocket,
WebSocketRoute,
Worker,
)
ChromiumBrowserContext = BrowserContext
Cookie = playwright._impl._api_structures.Cookie
FilePayload = playwright._impl._api_structures.FilePayload
FloatRect = playwright._impl._api_structures.FloatRect
Geolocation = playwright._impl._api_structures.Geolocation
HttpCredentials = playwright._impl._api_structures.HttpCredentials
PdfMargins = playwright._impl._api_structures.PdfMargins
Position = playwright._impl._api_structures.Position
ProxySettings = playwright._impl._api_structures.ProxySettings
ResourceTiming = playwright._impl._api_structures.ResourceTiming
SourceLocation = playwright._impl._api_structures.SourceLocation
StorageState = playwright._impl._api_structures.StorageState
ViewportSize = playwright._impl._api_structures.ViewportSize
Error = playwright._impl._errors.Error
TimeoutError = playwright._impl._errors.TimeoutError
def sync_playwright() -> PlaywrightContextManager:
return PlaywrightContextManager()
class Expect:
_unset: Any = object()
def __init__(self) -> None:
self._timeout: Optional[float] = None
def set_options(self, timeout: Optional[float] = _unset) -> None:
"""
This method sets global `expect()` options.
Args:
timeout (float): Timeout value in milliseconds. Default to 5000 milliseconds.
Returns:
None
"""
if timeout is not self._unset:
self._timeout = timeout
@overload
def __call__(
self, actual: Page, message: Optional[str] = None
) -> PageAssertions: ...
@overload
def __call__(
self, actual: Locator, message: Optional[str] = None
) -> LocatorAssertions: ...
@overload
def __call__(
self, actual: APIResponse, message: Optional[str] = None
) -> APIResponseAssertions: ...
def __call__(
self, actual: Union[Page, Locator, APIResponse], message: Optional[str] = None
) -> Union[PageAssertions, LocatorAssertions, APIResponseAssertions]:
if isinstance(actual, Page):
return PageAssertions(
PageAssertionsImpl(actual._impl_obj, self._timeout, message=message)
)
elif isinstance(actual, Locator):
return LocatorAssertions(
LocatorAssertionsImpl(actual._impl_obj, self._timeout, message=message)
)
elif isinstance(actual, APIResponse):
return APIResponseAssertions(
APIResponseAssertionsImpl(
actual._impl_obj, self._timeout, message=message
)
)
raise ValueError(f"Unsupported type: {type(actual)}")
expect = Expect()
__all__ = [
"expect",
"Accessibility",
"APIRequest",
"APIRequestContext",
"APIResponse",
"Browser",
"BrowserContext",
"BrowserType",
"CDPSession",
"ChromiumBrowserContext",
"ConsoleMessage",
"Cookie",
"Dialog",
"Download",
"ElementHandle",
"Error",
"FileChooser",
"FilePayload",
"FloatRect",
"Frame",
"FrameLocator",
"Geolocation",
"HttpCredentials",
"JSHandle",
"Keyboard",
"Locator",
"Mouse",
"Page",
"PdfMargins",
"Position",
"Playwright",
"ProxySettings",
"Request",
"ResourceTiming",
"Response",
"Route",
"Selectors",
"SourceLocation",
"StorageState",
"sync_playwright",
"TimeoutError",
"Touchscreen",
"Video",
"ViewportSize",
"WebSocket",
"WebSocketRoute",
"Worker",
]
@@ -0,0 +1,98 @@
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
from typing import TYPE_CHECKING, Any, Optional, cast
from greenlet import greenlet
from playwright._impl._connection import ChannelOwner, Connection
from playwright._impl._errors import Error
from playwright._impl._greenlets import MainGreenlet
from playwright._impl._object_factory import create_remote_object
from playwright._impl._playwright import Playwright
from playwright._impl._transport import PipeTransport
from playwright.sync_api._generated import Playwright as SyncPlaywright
if TYPE_CHECKING:
from asyncio.unix_events import AbstractChildWatcher
class PlaywrightContextManager:
def __init__(self) -> None:
self._playwright: SyncPlaywright
self._loop: asyncio.AbstractEventLoop
self._own_loop = False
self._watcher: Optional[AbstractChildWatcher] = None
self._exit_was_called = False
def __enter__(self) -> SyncPlaywright:
try:
self._loop = asyncio.get_running_loop()
except RuntimeError:
self._loop = asyncio.new_event_loop()
self._own_loop = True
if self._loop.is_running():
raise Error(
"""It looks like you are using Playwright Sync API inside the asyncio loop.
Please use the Async API instead."""
)
# Create a new fiber for the protocol dispatcher. It will be pumping events
# until the end of times. We will pass control to that fiber every time we
# block while waiting for a response.
def greenlet_main() -> None:
self._loop.run_until_complete(self._connection.run_as_sync())
dispatcher_fiber = MainGreenlet(greenlet_main)
self._connection = Connection(
dispatcher_fiber,
create_remote_object,
PipeTransport(self._loop),
self._loop,
)
g_self = greenlet.getcurrent()
def callback_wrapper(channel_owner: ChannelOwner) -> None:
playwright_impl = cast(Playwright, channel_owner)
self._playwright = SyncPlaywright(playwright_impl)
g_self.switch()
# Switch control to the dispatcher, it'll fire an event and pass control to
# the calling greenlet.
self._connection.call_on_object_with_known_name("Playwright", callback_wrapper)
dispatcher_fiber.switch()
playwright = self._playwright
playwright.stop = self.__exit__ # type: ignore
return playwright
def start(self) -> SyncPlaywright:
return self.__enter__()
def __exit__(self, *args: Any) -> None:
if self._exit_was_called:
return
self._exit_was_called = True
self._connection.stop_sync()
if self._watcher:
self._watcher.close()
if self._own_loop:
tasks = asyncio.all_tasks(self._loop)
for t in [t for t in tasks if not (t.done() or t.cancelled())]:
t.cancel()
self._loop.run_until_complete(self._loop.shutdown_asyncgens())
self._loop.close()
File diff suppressed because it is too large. Load diff