okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
c9cb3a0
main
1"""Optional Chrome scenarios exercise the CDP client against a real browser.""" 2 3from __future__import annotations 4 5from pathlib import Path 6import shutil 7import sys 8import unittest 9 10ROOT = Path (__file__ ).resolve ().parents [2 ] 11sys .path .insert (0 ,str (ROOT / "priv" / "python" )) 12 13from albedo_plugins .browser import spawn ,render ,one # noqa: E402 14 15 16class BrowserIntegrationTest (unittest .IsolatedAsyncioTestCase ): 17"""End-to-end integration tests using local Chromium if present.""" 18 19async def asyncSetUp (self ): 20self .has_chrome = ( 21shutil .which ("chromium" )is not None 22or shutil .which ("google-chrome" )is not None 23or Path ("/Applications/Chromium.app/Contents/MacOS/Chromium" ).exists () 24or Path ( 25"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" 26 ).exists () 27 ) 28 29async def test_spawn_navigate_observe_interact_close (self ): 30if not self .has_chrome : 31self .skipTest ("No Chrome or Chromium installed on this system" ) 32 33b = await spawn (headless = True ) 34try : 35page = await b .new_page () 36html = ( 37"data:text/html," 38"<!doctype html>" 39"<html><head><title>Albedo Test</title></head>" 40"<body>" 41"<h1>Header</h1>" 42"<label for='name'>Name</label><input id='name' value='initial'>" 43"<button id='save' onclick='window.clicked=true'>Save</button>" 44"</body></html>" 45 ) 46await page .goto (html ) 47 48# Observe 49obs = await page .observe () 50self .assertEqual (obs ["title" ],"Albedo Test" ) 51text = render (obs ) 52self .assertIn ("Header" ,text ) 53self .assertIn ("Save" ,text ) 54 55# Find and One 56btn = one (obs ,role = "button" ,name = "Save" ) 57self .assertIsNotNone (btn ) 58field = one (obs ,role = "textbox" ,name = "Name" ) 59self .assertIsNotNone (field ) 60 61# Fill and Click 62await page .fill (field ,"new value" ) 63self .assertEqual ( 64await page .evaluate ("document.querySelector('#name').value" ), 65"new value" , 66 ) 67 68await page .click (btn ) 69self .assertTrue (await page .evaluate ("window.clicked" )) 70 71# Close page 72await page .close () 73finally : 74await b .close () 75 76 77if __name__ == "__main__" : 78unittest .main ()