Skip to content

Latest commit

 

History

History
89 lines (69 loc) · 2.51 KB

File metadata and controls

89 lines (69 loc) · 2.51 KB
title browser.locators | 配置
outline deep

browser.locators

内置 浏览器定位器 的选项。

browser.locators.testIdAttribute

  • 类型: string
  • 默认值: data-testid

用于通过 getByTestId 定位器查找元素的属性。

browser.locators.exact

<<<<<<< HEAD

  • 类型: boolean
  • 默认值: false

当设置为 true 时,定位器 默认会执行精确文本匹配,要求完全且区分大小写的匹配。单个定位器调用可通过自身的 exact 选项覆盖此默认行为。

// 当 exact: false(默认值)时,会匹配 "Hello, World!"、"Say Hello, World" 等文本
// 当 exact: true 时,仅精确匹配字符串 "Hello, World"
=======
- **Type:** `boolean`
- **Default:** `true`

When set to `true`, [locators](/api/browser/locators) match text exactly by default, requiring a full, case-sensitive match. Individual locator calls can override this default via their own `exact` option.

```ts
// With exact: true (default), this only matches the string "Hello, World" exactly.
// With exact: false, this matches "Hello, World!", "Say Hello, World", etc.
>>>>>>> b8458066305a759bf414605c23780c31dccbd917
const locator = page.getByText('Hello, World', { exact: true })
await locator.click()

browser.locators.errorFormat 5.0.0 {#browser-locators-errorformat}

  • Type: 'html' | 'aria' | 'all'
  • Default: 'all'

Controls what Vitest prints when a locator cannot find an element. Vitest prints information for the DOM subtree where the locator search ran, or document.body for page-level locators.

  • 'html' prints that DOM subtree as HTML using utils.prettyDOM.
  • 'aria' prints that DOM subtree as an ARIA snapshot, which focuses on accessible roles, names, and state.
  • 'all' prints the ARIA snapshot first, followed by the HTML output.
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    browser: {
      enabled: true,
      locators: {
        errorFormat: 'aria',
      },
    },
  },
})

For example, all displays a following error:

VitestBrowserElementError: Cannot find element with locator: getByRole('button', { name: 'Save' })

ARIA tree:
- main:
  - heading "Settings" [level=1]
  - button "Cancel"

HTML:
<body>
  <main>
    <h1>
      Settings
    </h1>
    <button>
      Cancel
    </button>
  </main>
</body>