Skip to content

Commit

Permalink
Update v1.1
Browse files Browse the repository at this point in the history
- Official Support Scrolling. [Vinyzu]
- Change Exception to Warning when importing CDP-Patches on MacOS. [Vinyzu]
- Fix Documentation. [Vinyzu]
  • Loading branch information
Vinyzu committed Apr 24, 2024
1 parent d5e4e8f commit 22c8d12
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 185 deletions.
5 changes: 3 additions & 2 deletions cdp_patches/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import platform
import warnings

VERSION = 1.0
VERSION = "1.1"

system_name = platform.system()
if system_name == "Windows":
is_windows = True
elif system_name == "Linux":
is_windows = False
else:
raise SystemError("Unknown system (You´re probably using MacOS, which is currently not supported).")
warnings.warn("Unknown system (You´re probably using MacOS, which is currently not supported).", RuntimeWarning)

__all__ = ["VERSION", "is_windows"]
6 changes: 4 additions & 2 deletions cdp_patches/input/async_input.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

import asyncio
import platform
import random
import re
import sys
import time
import warnings
from typing import Any, Generator, Literal, Optional, Union

if sys.version_info.minor >= 10:
Expand Down Expand Up @@ -51,6 +51,9 @@ class AsyncInput:
def __init__(
self, pid: Optional[int] = None, browser: Optional[async_browsers] = None, scale_factor: Optional[float] = 1.0, emulate_behaviour: Optional[bool] = True, window_timeout: Optional[float] = 30.0
) -> None:
if platform.system() not in ('Windows', 'Linux'):
raise SystemError("Unknown system (You´re probably using MacOS, which is currently not supported).")

self.pid = pid
self.browser = browser
self.window_timeout = window_timeout or self.window_timeout
Expand Down Expand Up @@ -163,7 +166,6 @@ async def move(self, x: Union[int, float], y: Union[int, float], emulate_behavio
self.last_x, self.last_y = x, y

async def scroll(self, direction: Literal["up", "down", "left", "right"], amount: int) -> None:
warnings.warn("Scrolling using CDP-Patches is discouraged as Scroll Inputs dont leak the CDP Domain.", UserWarning)
self._base.scroll(direction=direction, amount=amount)

async def type(self, text: str, fill: Optional[bool] = False, timeout: Optional[float] = None) -> None:
Expand Down
6 changes: 4 additions & 2 deletions cdp_patches/input/sync_input.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import platform
import random
import re
import sys
import threading
import time
import warnings
from typing import Literal, Optional, Union

if sys.version_info.minor >= 10:
Expand Down Expand Up @@ -50,6 +50,9 @@ class SyncInput:
def __init__(
self, pid: Optional[int] = None, browser: Optional[sync_browsers] = None, scale_factor: Optional[float] = 1.0, emulate_behaviour: Optional[bool] = True, window_timeout: Optional[float] = 30.0
) -> None:
if platform.system() not in ('Windows', 'Linux'):
raise SystemError("Unknown system (You´re probably using MacOS, which is currently not supported).")

self._scale_factor = scale_factor or self._scale_factor
self.window_timeout = window_timeout or self.window_timeout
self.emulate_behaviour = emulate_behaviour or self.emulate_behaviour
Expand Down Expand Up @@ -158,7 +161,6 @@ def move(self, x: Union[int, float], y: Union[int, float], emulate_behaviour: Op
self.last_x, self.last_y = x, y

def scroll(self, direction: Literal["up", "down", "left", "right"], amount: int) -> None:
warnings.warn("Scrolling using CDP-Patches is discouraged as Scroll Inputs dont leak the CDP Domain.", UserWarning)
self._base.scroll(direction=direction, amount=amount)

def type(self, text: str, fill: Optional[bool] = False, timeout: Optional[float] = None) -> None:
Expand Down
8 changes: 7 additions & 1 deletion docs/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

0.1 (2024-03-23)
1.1 (2024-04-24)
------------------
- Official Support Scrolling. [Vinyzu]
- Change Exception to Warning when importing CDP-Patches on MacOS. [Vinyzu]
- Fix Documentation. [Vinyzu]

1.0(2024-03-23)
------------------
- Initial Release. [Vinyzu]
40 changes: 22 additions & 18 deletions docs/gitbook/input/playwright-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Sync Usage (Sync Playwright)

```python
from playwright.sync_api import sync_playwright
from playwright.sync_api import sync_playwright, Locator
from cdp_patches.input import SyncInput

# Locator Position Helper
Expand Down Expand Up @@ -35,30 +35,34 @@ with sync_playwright() as playwright:
## Async Usage (Async Playwright / Botright)

```python
from playwright.async_api import async_playwright
import asyncio

from playwright.async_api import async_playwright, Locator
from cdp_patches.input import AsyncInput

# Locator Position Helper
async def get_locator_pos(locator: WebElement):
location = await locator.location
size = await locator.size
assert location, size
async def get_locator_pos(locator: Locator):
bounding_box = await locator.bounding_box()
assert bounding_box

x, y, width, height = location.get("x"), location.get("y"), size.get("width"), size.get("height")
x, y, width, height = bounding_box.get("x"), bounding_box.get("y"), bounding_box.get("width"), bounding_box.get("height")
assert x and y and width and height

x, y = x + width // 2, y + height // 2
return x, y

async with async_playwright() as playwright:
browser = await playwright.chromium.launch()
page = await browser.new_page()
async_input = await AsyncInput(browser=browser) # Also works with Contexts

# Example: Click Button
# Find Button Coords
locator = page.locator("button")
x, y = await get_locator_pos(locator)
# Click Coords => Click Button
await async_input.click("left", x, y)
async def main():
async with async_playwright() as playwright:
browser = await playwright.chromium.launch()
page = await browser.new_page()
async_input = await AsyncInput(browser=browser) # Also works with Contexts

# Example: Click Button
# Find Button Coords
locator = page.locator("button")
x, y = await get_locator_pos(locator)
# Click Coords => Click Button
await async_input.click("left", x, y)

asyncio.run(main())
```
1 change: 0 additions & 1 deletion docs/gitbook/input/selenium-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ async def main():
locator = await driver.find_element(By.XPATH, "//button")
x, y = await get_locator_pos(locator)
# Click Coords => Click Button
await driver.find_element(By.XPATH, "//button")
await async_input.click("left", x, y)

asyncio.run(main())
Expand Down
159 changes: 0 additions & 159 deletions docs/index.rst

This file was deleted.

0 comments on commit 22c8d12

Please sign in to comment.