GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing robotframework-browser.org — Python plugins — Robot Framework Browser The Python plugin API adds keywords to Browser, or replaces existing ones, without forking the library. It is provided by PythonLibCore. A Python-only pluginSubclass import jsonfrom robot.api import loggerfrom robot.api.deco import keywordfrom Browser.base.librarycomponent import LibraryComponentfrom Browser.generated.playwright_pb2 import Requestclass SimplePythonPlugin(LibraryComponent): @keyword def cookie_via_public_api(self) -> dict: """Uses Browser's own public API.""" cookies = self.library.get_cookies() logger.debug(json.dumps(cookies, indent=4, default=str)) assert len(cookies) == 1, "Too many cookies." return {"name": cookies[0]["name"], "value": cookies[0]["value"]} @keyword def cookie_via_grpc(self) -> dict: """Calls the Node side directly over gRPC.""" with self.playwright.grpc_channel() as stub: response = stub.GetCookies(Request().Empty()) cookies = json.loads(response.json) return {"name": cookies[0]["name"], "value": cookies[0]["value"]} Load it at import: *** Settings ***Library Browser plugins=${CURDIR}/SimplePythonPlugin.py*** Test Cases ***Read A Cookie New Page https://example.com Add Cookie session abc123 url=https://example.com ${cookie} = Cookie Via Public Api Should Be Equal ${cookie}[name] session Two routes are shown above deliberately. Several plugins can be loaded at once — Two rules that fail loudly, and one that does not:
Selectors inside a pluginThe public keywords resolve the @keyworddef disable_element(self, selector): """Disables an element.""" self.library.evaluate_javascript(selector, "e => e.disabled = true") Resolve it yourself with For a keyword that should also honour presenter mode — highlighting the element
and pausing so a human can follow along — use @keyworddef highlight_and_blur(self, selector): """Blurs the element, honouring presenter mode.""" selector = self.presenter_mode(selector, self.strict_mode) self.call_js_keyword("myBlur", selector=selector)
Calling JavaScript from a Python pluginLoad a JS module in the constructor, then call into it: from pathlib import Pathfrom robot.api.deco import keywordfrom Browser import Browserfrom Browser.base.librarycomponent import LibraryComponentclass PythonPlugin(LibraryComponent): def __init__(self, library: Browser): super().__init__(library) self.initialize_js_extension(Path(__file__).parent.resolve() / "JSPlugin.js") @keyword def my_mouse_wheel(self, x: int, y: int): """Calls a custom JavaScript keyword from JSPlugin.js.""" return self.call_js_keyword("myMouseWheel", x=x, y=y) This combination is usually the one you want: the keyword signature, type conversion and documentation are Python, and only the part that genuinely needs the page is JavaScript. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |