Best Python code snippet using playwright-python
test_scheduling.py
Source:test_scheduling.py
...151 resources={"custom": 1},152 memory=1e9,153 object_store_memory=object_size * 2)154 @ray.remote(resources={"custom": 1})155 def create_remote_object():156 return np.zeros(int(object_size), dtype=np.uint8)157 local_obj = ray.put(np.zeros(int(object_size * 1.5), dtype=np.uint8))158 print(local_obj)159 @ray.remote160 def f(x):161 return162 dep = create_remote_object.remote()163 ray.wait([dep], fetch_local=False)164 # Wait for resource availabilities to propagate.165 time.sleep(1)166 # This task can't run on the local node. Make sure it gets spilled even167 # though we have the local CPUs to run it.168 ray.get(f.remote(dep), timeout=30)169def test_locality_aware_leasing(ray_start_cluster):...
context.py
Source:context.py
...155 self, address: str = None, level: StorageLevel = StorageLevel.MEMORY156 ):157 return self._call(self._get_backend_info(address, level))158 @implements(Context.create_remote_object)159 def create_remote_object(self, name: str, object_cls, *args, **kwargs):160 ref = self._call(161 self._session_api.create_remote_object(162 self.session_id, name, object_cls, *args, **kwargs163 )164 )165 return _RemoteObjectWrapper(ref, self._loop)166 @implements(Context.get_remote_object)167 def get_remote_object(self, name: str):168 ref = self._call(self._session_api.get_remote_object(self.session_id, name))169 return _RemoteObjectWrapper(ref, self._loop)170 @implements(Context.destroy_remote_object)171 def destroy_remote_object(self, name: str):172 return self._call(173 self._session_api.destroy_remote_object(self.session_id, name)174 )175 @implements(Context.register_custom_log_path)...
connect_to_browser.py
Source:connect_to_browser.py
1import asyncio2import importlib.metadata3from typing import Any, Dict, cast4from greenlet import greenlet5from playwright._impl._api_types import Error6from playwright._impl._browser import Browser as BrowserImpl7from playwright._impl._connection import Connection, from_channel8from playwright._impl._object_factory import create_remote_object9from playwright._impl._playwright import Playwright as PlaywrightImpl10from playwright._impl._transport import WebSocketTransport11from playwright.sync_api._generated import Browser as SyncBrowser12class ConnectToBrowserContextManager:13 def __init__(14 self,15 ws_endpoint: str,16 slow_mo: float = None,17 headers: Dict[str, str] = None,18 ) -> None:19 self._browser: SyncBrowser20 self._ws_endpoint = ws_endpoint21 self._slow_mo = slow_mo22 self._headers = headers23 def _make_connection(self, dispatcher_fiber, object_factory, transport, loop) -> Connection:24 if importlib.metadata.version('playwright') < '1.15.0':25 return Connection(26 dispatcher_fiber,27 create_remote_object,28 transport)29 else:30 return Connection(31 dispatcher_fiber,32 create_remote_object,33 transport,34 loop)35 def _setup_browser(self, browser: BrowserImpl):36 version = importlib.metadata.version('playwright')37 if version < '1.17.0':38 browser._is_remote = True39 browser._is_connected_over_websocket = True40 elif version < '1.19.0':41 browser._should_close_connection_on_close = True42 else:43 browser._should_close_connection_on_close = True44 browser._local_utils = self._playwright_impl._utils45 def __enter__(self) -> SyncBrowser:46 loop: asyncio.AbstractEventLoop47 own_loop = None48 try:49 loop = asyncio.get_running_loop()50 except RuntimeError:51 loop = asyncio.new_event_loop()52 own_loop = loop53 if loop.is_running():54 raise Error(55 """It looks like you are using Playwright Sync API inside the asyncio loop.56Please use the Async API instead."""57 )58 def greenlet_main() -> None:59 loop.run_until_complete(self._connection.run_as_sync())60 if own_loop:61 loop.run_until_complete(loop.shutdown_asyncgens())62 loop.close()63 dispatcher_fiber = greenlet(greenlet_main)64 transport = WebSocketTransport(65 loop,66 self._ws_endpoint,67 self._headers,68 self._slow_mo)69 self._connection = self._make_connection(70 dispatcher_fiber,71 create_remote_object,72 transport,73 loop)74 g_self = greenlet.getcurrent()75 def callback_wrapper(playwright_impl: PlaywrightImpl) -> None:76 self._playwright_impl = playwright_impl77 g_self.switch()78 self._connection.call_on_object_with_known_name(79 "Playwright", callback_wrapper)80 dispatcher_fiber.switch()81 pre_launched_browser = self._playwright_impl._initializer.get(82 "preLaunchedBrowser")83 assert pre_launched_browser84 browser = cast(BrowserImpl, from_channel(pre_launched_browser))85 self._setup_browser(browser)86 def handle_transport_close() -> None:87 for context in browser.contexts:88 for page in context.pages:89 page._on_close()90 context._on_close()91 browser._on_close()92 transport.once("close", handle_transport_close)93 self._browser = SyncBrowser(browser)94 return self._browser95 def start(self) -> SyncBrowser:96 return self.__enter__()97 def __exit__(self, *args: Any) -> None:98 self._browser.close()99def connect_to_browser(100 ws_endpoint: str,101 slow_mo: float = None,102 headers: Dict[str, str] = None,103) -> SyncBrowser:...
_object_factory.py
Source:_object_factory.py
...32 def __init__(33 self, parent: ChannelOwner, type: str, guid: str, initializer: Dict34 ) -> None:35 super().__init__(parent, type, guid, initializer)36def create_remote_object(37 parent: ChannelOwner, type: str, guid: str, initializer: Dict38) -> Any:39 if type == "BindingCall":40 return BindingCall(parent, type, guid, initializer)41 if type == "Browser":42 return Browser(cast(BrowserType, parent), type, guid, initializer)43 if type == "BrowserType":44 return BrowserType(parent, type, guid, initializer)45 if type == "BrowserContext":46 browser_name: str = ""47 if isinstance(parent, Browser):48 browser_name = parent._browser_type.name49 if isinstance(parent, BrowserType):50 browser_name = parent.name...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!