Files
robotex/src/engine/actions.py
T

48 lines
954 B
Python

from playwright.async_api import Page
from engine.schemas import ActionRequest
_actions: dict = {}
def register(fun) -> None:
_actions[fun.__name__] = fun
return fun
@register
async def fill(page: Page, act: ActionRequest) -> str | None:
...
@register
async def mouse_move(page: Page, act: ActionRequest) -> str | None:
...
@register
async def click(page: Page, act: ActionRequest) -> str | None:
...
@register
async def wait_for(page: Page, act: ActionRequest) -> str | None:
try:
assert act.selector
await page.wait_for_selector(act.selector, timeout=act.timeout * 1000)
except Exception as e:
return str(e)
else:
return None
async def execute(page: Page, actions: list[ActionRequest]) -> list[str]:
actions_result = []
for act in actions:
try:
await _actions[act.type](page, act)
except Exception as e:
actions_result.append({"type": act.type, "ok": "false", "error": str(e)})
break
return actions_result