ByPlaceholderText
getByPlaceholderText,queryByPlaceholderText,getAllByPlaceholderText,queryAllByPlaceholderText,findByPlaceholderText,findAllByPlaceholderText
API
getByPlaceholderText(
// If you're using `screen`, then skip the container argument:
container: HTMLElement,
text: TextMatch,
options?: {
exact?: boolean = true,
normalizer?: NormalizerFn,
}): HTMLElement
这将搜索所有具有占位符属性的元素,并找到一个与给定 TextMatch
匹配的元素。
<input placeholder="Username" />
- 原生
- React
- Angular
- Cypress
import {screen} from '@testing-library/dom'
const inputNode = screen.getByPlaceholderText('Username')
import {render, screen} from '@testing-library/react'
render(<MyComponent />)
const inputNode = screen.getByPlaceholderText('Username')
import {render, screen} from '@testing-library/angular'
await render(MyComponent)
const inputNode = screen.getByPlaceholderText('Username')
cy.findByPlaceholderText('Username').should('exist')
注意
占位符不是标签的良好替代品,因此您通常应该使用
getByLabelText
代替。
选项
TextMatch 选项