跳至主要内容

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" />
import {screen} from '@testing-library/dom'

const inputNode = screen.getByPlaceholderText('Username')

注意

占位符不是标签的良好替代品,因此您通常应该使用 getByLabelText 代替。

选项

TextMatch 选项