跳转到内容

Testing

此内容尚不支持你的语言。

All AsterUI components support the data-testid prop for end-to-end testing with tools like Playwright, Cypress, or Testing Library.

Pass a data-testid prop to any component:

import { Button, Input, Modal } from 'asterui'
function LoginForm() {
return (
<form>
<Input data-testid="email-input" placeholder="Email" />
<Input data-testid="password-input" type="password" placeholder="Password" />
<Button data-testid="submit-btn" color="primary">
Sign In
</Button>
</form>
)
}
// Click the submit button
await page.getByTestId('submit-btn').click()
// Fill the email input
await page.getByTestId('email-input').fill('user@example.com')
// Assert element is visible
await expect(page.getByTestId('submit-btn')).toBeVisible()

Components with multiple interactive elements use a suffix pattern for nested test IDs. When you pass data-testid="my-id", child elements receive IDs like my-id-suffix.

<Input data-testid="search" prefix={<SearchIcon />} allowClear />
ElementTest ID
Wrappersearch
Input fieldsearch-input
Prefix areasearch-prefix
Suffix areasearch-suffix
Clear buttonsearch-clear
<Modal data-testid="confirm-modal" title="Confirm" open={isOpen}>
Are you sure?
</Modal>
ElementTest ID
Modal containerconfirm-modal
Titleconfirm-modal-title
Contentconfirm-modal-content
Backdropconfirm-modal-backdrop
<Select data-testid="country-select" options={countries} />
ElementTest ID
Containercountry-select
Triggercountry-select-trigger
Dropdowncountry-select-dropdown
Option (by value)country-select-option-{value}

Many components also expose data-state attributes that reflect their current state, useful for assertions:

// Button states
<Button loading /> // data-state-loading="true"
<Button disabled /> // data-state-disabled="true"
<Button active /> // data-state-active="true"
// Toggle state
<Toggle checked /> // data-state="checked"
<Toggle /> // data-state="unchecked"
// Tooltip/Popover state
<Tooltip open /> // data-state="open"
<Tooltip /> // data-state="closed"

Example assertions:

// Assert button is loading
await expect(page.getByTestId('submit-btn')).toHaveAttribute('data-state-loading', 'true')
// Assert toggle is checked
await expect(page.getByTestId('dark-mode')).toHaveAttribute('data-state', 'checked')