자동화 · 45분

기다림과 검증

수동 sleep 대신 Playwright의 자동 대기와 expect 검증으로 화면 변화를 안정적으로 확인한다.

Web에서 개념과 코드 학습운영체제 권한이 필요한 실행은 Local에서 이어집니다.
automation.browser.formInput
01

늦게 바뀌는 상태 기다리기

버튼 클릭 후 비동기로 바뀌는 상태 문구를 expect로 기다린다.

  • 늦게 나타나는 상태 문구를 안정적으로 기다릴 수 있다.
  • strictness 오류를 읽고 locator 범위를 좁힐 수 있다.
  • 실패를 재현하고 고친 뒤 assert로 완료 신호를 남길 수 있다.
직접 해보기클릭 후 상태를 "동기화 완료"로 바꾸고 expect 검증을 통과시키세요.
from playwright.sync_api import sync_playwright, expect

html = """
<button onclick="setTimeout(() => document.querySelector('[data-testid=status]').textContent='완료', 80)">시작</button>
<p data-testid="status">대기</p>
"""

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    page = browser.new_page()
    page.set_content(html)
    page.get_by_role("button", name="시작").click()
    status = page.get_by_test_id("status")
    expect(status).to_have_text("완료")
    result = status.inner_text()
    browser.close()

assert result == "완료"
result
편집기를 준비하고 있습니다.