Loading...
Loading...
Compare original and translation side by side
Version: 1.1.0 Category: Development Last Updated: 2026-01-02
版本: 1.1.0 分类: 开发 最后更新时间: 2026-01-02
undefinedundefined
```python
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("http://localhost:3000")
# Take screenshot
page.screenshot(path="screenshot.png")
# Get page content
content = page.content()
print(content)
browser.close()
```python
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("http://localhost:3000")
# Take screenshot
page.screenshot(path="screenshot.png")
# Get page content
content = page.content()
print(content)
browser.close()from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("http://localhost:3000")
# CRITICAL: Wait for JS to execute
page.wait_for_load_state("networkidle")
# Now safe to interact
content = page.content()
browser.close()from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("http://localhost:3000")
# CRITICAL: Wait for JS to execute
page.wait_for_load_state("networkidle")
# Now safe to interact
content = page.content()
browser.close()page.get_by_text("Submit").click()
page.get_by_text("Welcome", exact=False).wait_for()page.get_by_role("button", name="Submit").click()
page.get_by_role("textbox", name="Email").fill("test@example.com")
page.get_by_role("link", name="Home").click()page.locator(".btn-primary").click()
page.locator("#email-input").fill("test@example.com")
page.locator("[data-testid='submit']").click()page.locator("#submit-button").click()page.get_by_text("Submit").click()
page.get_by_text("Welcome", exact=False).wait_for()page.get_by_role("button", name="Submit").click()
page.get_by_role("textbox", name="Email").fill("test@example.com")
page.get_by_role("link", name="Home").click()page.locator(".btn-primary").click()
page.locator("#email-input").fill("test@example.com")
page.locator("[data-testid='submit']").click()page.locator("#submit-button").click()undefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedimport subprocess
import time
from playwright.sync_api import sync_playwrightimport subprocess
import time
from playwright.sync_api import sync_playwrightundefinedundefinedimport requests
def is_server_running(url="http://localhost:3000"):
try:
response = requests.get(url, timeout=2)
return response.status_code == 200
except:
return Falseimport requests
def is_server_running(url="http://localhost:3000"):
try:
response = requests.get(url, timeout=2)
return response.status_code == 200
except:
return Falsefrom playwright.sync_api import expectfrom playwright.sync_api import expectundefinedundefinedimport pytest
from playwright.sync_api import sync_playwright
@pytest.fixture(scope="session")
def browser():
with sync_playwright() as p:
browser = p.chromium.launch()
yield browser
browser.close()
@pytest.fixture
def page(browser):
page = browser.new_page()
yield page
page.close()
def test_homepage(page):
page.goto("http://localhost:3000")
assert page.title() == "My App"
def test_login(page):
page.goto("http://localhost:3000/login")
page.fill("#username", "testuser")
page.fill("#password", "password")
page.click("button[type='submit']")
page.wait_for_url("**/dashboard")import pytest
from playwright.sync_api import sync_playwright
@pytest.fixture(scope="session")
def browser():
with sync_playwright() as p:
browser = p.chromium.launch()
yield browser
browser.close()
@pytest.fixture
def page(browser):
page = browser.new_page()
yield page
page.close()
def test_homepage(page):
page.goto("http://localhost:3000")
assert page.title() == "My App"
def test_login(page):
page.goto("http://localhost:3000/login")
page.fill("#username", "testuser")
page.fill("#password", "password")
page.click("button[type='submit']")
page.wait_for_url("**/dashboard")from playwright.sync_api import sync_playwright
from pathlib import Path
def capture_page_state(url: str, output_dir: str):
"""Capture full page screenshot and HTML content."""
output = Path(output_dir)
output.mkdir(parents=True, exist_ok=True)
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto(url)
page.wait_for_load_state("networkidle")
# Screenshot
page.screenshot(path=str(output / "screenshot.png"), full_page=True)
# HTML content
(output / "content.html").write_text(page.content())
browser.close()
return outputfrom playwright.sync_api import sync_playwright
from pathlib import Path
def capture_page_state(url: str, output_dir: str):
"""Capture full page screenshot and HTML content."""
output = Path(output_dir)
output.mkdir(parents=True, exist_ok=True)
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto(url)
page.wait_for_load_state("networkidle")
# Screenshot
page.screenshot(path=str(output / "screenshot.png"), full_page=True)
# HTML content
(output / "content.html").write_text(page.content())
browser.close()
return outputundefinedundefineddef submit_contact_form(page, name, email, message):
"""Submit a contact form and verify success."""
page.goto("http://localhost:3000/contact")
# Fill form fields
page.fill("[name='name']", name)
page.fill("[name='email']", email)
page.fill("[name='message']", message)
# Submit
page.click("button[type='submit']")
# Wait for success message
page.wait_for_selector(".success-message")
return page.locator(".success-message").text_content()def submit_contact_form(page, name, email, message):
"""Submit a contact form and verify success."""
page.goto("http://localhost:3000/contact")
# Fill form fields
page.fill("[name='name']", name)
page.fill("[name='email']", email)
page.fill("[name='message']", message)
# Submit
page.click("button[type='submit']")
# Wait for success message
page.wait_for_selector(".success-message")
return page.locator(".success-message").text_content()def verify_api_call(page, endpoint_pattern):
"""Intercept and verify API calls."""
api_response = None
def handle_response(response):
nonlocal api_response
if endpoint_pattern in response.url:
api_response = response.json()
page.on("response", handle_response)
page.goto("http://localhost:3000/dashboard")
page.wait_for_load_state("networkidle")
return api_responsedef verify_api_call(page, endpoint_pattern):
"""Intercept and verify API calls."""
api_response = None
def handle_response(response):
nonlocal api_response
if endpoint_pattern in response.url:
api_response = response.json()
page.on("response", handle_response)
page.goto("http://localhost:3000/dashboard")
page.wait_for_load_state("networkidle")
return api_responsebrowser = p.chromium.launch(slow_mo=500) # 500ms delay between actionsbrowser = p.chromium.launch(slow_mo=500) # 500ms delay between actionsbrowser = p.chromium.launch(headless=False) # See the browserbrowser = p.chromium.launch(headless=False) # See the browserpage.pause() # Opens Playwright Inspectorpage.pause() # Opens Playwright Inspectorcontext = browser.new_context()
context.tracing.start(screenshots=True, snapshots=True)
page = context.new_page()context = browser.new_context()
context.tracing.start(screenshots=True, snapshots=True)
page = context.new_page()undefinedundefinednetworkidlenetworkidletime.sleep()time.sleep()| Error | Cause | Solution |
|---|---|---|
| Element not found in time | Use explicit waits or increase timeout |
| Browser closed prematurely | Check cleanup order in fixtures |
| Server not running | Start server before tests |
| Hidden by CSS/JS | Wait for visibility state |
| Navigation during action | Wait for navigation to complete |
| 错误 | 原因 | 解决方案 |
|---|---|---|
| 元素未在指定时间内找到 | 使用显式等待或增加超时时间 |
| 浏览器提前关闭 | 检查fixtures中的清理顺序 |
| 服务器未运行 | 测试前启动服务器 |
| 元素被CSS/JS隐藏 | 等待元素进入可见状态 |
| 操作过程中发生页面跳转 | 等待页面跳转完成后再执行操作 |
undefinedundefinedundefinedundefined| Metric | Target | Description |
|---|---|---|
| Test Duration | <10s per test | Individual test execution time |
| Flakiness Rate | <2% | Tests passing consistently |
| Screenshot Match | 100% | Visual regression accuracy |
| Network Coverage | >90% | API endpoints tested |
| 指标 | 目标值 | 描述 |
|---|---|---|
| 测试时长 | 单测试<10秒 | 单个测试的执行时间 |
| 不稳定率 | <2% | 测试通过率的稳定性 |
| 截图匹配度 | 100% | 视觉回归测试的准确性 |
| 网络覆盖率 | >90% | 已测试的API端点占比 |
pip install playwright pytest requests
playwright install chromiumpip install playwright pytest requests
playwright install chromium