"""CDP button click test."""

import sys

sys.path.insert(0, "src")
from nomo_e2e import WebViewController

c = WebViewController.connect("http://localhost:9222", driver="cdp")
page = c._adapter._page

loc = page.locator("button")
print(f"Total buttons: {loc.count()}")
for i in range(min(7, loc.count())):
    btn = loc.nth(i)
    text = btn.text_content()
    is_visible = btn.is_visible()
    is_enabled = btn.is_enabled()
    print(f"  [{i}] text={repr(str(text)[:20])}, visible={is_visible}, enabled={is_enabled}")

# Try clicking visible button
for i in range(loc.count()):
    btn = loc.nth(i)
    if btn.is_visible() and btn.is_enabled():
        text = btn.text_content()
        print(f"\nTrying to click button [{i}]: {repr(str(text)[:30])}")
        try:
            btn.click(timeout=2000)
            print("  SUCCESS!")
            break
        except Exception as e:
            print(f"  FAIL: {str(e)[:100]}")

c.disconnect()
