Repository navigation
Expand file tree
/
Copy pathcdp_playwright.py
More file actions
42 lines (32 loc) · 1.77 KB
/
Copy pathcdp_playwright.py
File metadata and controls
42 lines (32 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Attach Playwright to a Verify web session over the Chrome DevTools Protocol (CDP).
cd python && uv run python examples/cdp_playwright.py
Needs a Revyl sign-in (REVYL_API_KEY or 'revyl auth login') and REVYL_WEB_APP_ID, a web app
you created in Revyl. Playwright connects to Revyl's browser, so it needs no browser download.
It starts a real, billable session.
"""
import os
from playwright.sync_api import sync_playwright
from revyl_verify import Target, open
target = Target(
platform="web", app_id=os.environ["REVYL_WEB_APP_ID"], url="https://www.saucedemo.com"
)
with open(target) as session, sync_playwright() as playwright:
endpoint = session.cdp() # works for one connection, within 60 seconds
browser = playwright.chromium.connect_over_cdp(endpoint.url)
try:
page = browser.contexts[0].pages[0] # the session's own tab; new_context() is refused
requests: list[str] = []
page.on("request", lambda request: requests.append(request.url))
page.goto("https://www.saucedemo.com")
print(f"title: {page.title()}, {len(requests)} requests, the first {requests[0]}")
# Sauce Demo publishes these practice credentials on its sign-in page.
page.locator("#user-name").fill("standard_user")
page.locator("#password").fill("secret_sauce")
page.locator("#login-button").click()
print(f"heading: {page.locator('.title').text_content()}")
with page.expect_console_message() as message:
page.evaluate("console.log('hello from Playwright')")
print(f"console: {message.value.text}")
session.validation("A list of products is shown") # Verify steps work while attached
finally:
browser.close() # only disconnects Playwright; session.close() ends the session