Skip to content

参考

基于 Vitest v4.x(Browser Mode)编写

速查

  • 配置:test.browser: { enabled, provider, instances, headless }
  • 查询:page.getByRole(...)(语义、惰性、重试)
  • 交互:await locator.click() / userEvent.fill(...)
  • 断言:await expect.element(locator).toBeVisible()
  • 完整说明见 入门 / 配置 / Locators / 交互与断言 / 视觉回归与对比

配置项

配置默认说明
browser.enabledfalse启用 browser mode
browser.providerplaywright() / webdriverio() / preview()
browser.instances浏览器实例列表(至少一个)
browser.headlessprocess.env.CI无头模式
browser.screenshotFailures!browser.ui失败自动截图
browser.locators.testIdAttributedata-testidgetByTestId 属性名

Locator API

API说明
getByRole(role, opts)ARIA 角色(首选)
getByLabelText / getByPlaceholder表单
getByText / getByAltText / getByTitle文本 / alt / title
getByTestIddata-testid 兜底
.nth/.first/.last位置
.filter({ hasText })过滤
.and() / .or()逻辑组合
page.frameLocator(...)iframe(Playwright)

userEvent

API说明
click / dblClick / tripleClick点击
fill / type / clear输入
keyboard / tab键盘
hover / selectOptions / upload悬停 / 选择 / 上传
dragAndDrop / dropTo拖放

expect.element matcher(部分)

matcher说明
toBeVisible / toBeInViewport可见 / 在视口
toBeDisabled / toBeChecked / toHaveFocus表单状态
toHaveTextContent / toHaveValue / toHaveClass内容 / 属性
toHaveRole / toHaveAccessibleName无障碍
toMatchScreenshot视觉回归

官方资源