跳至主要內容

ByAltText

getByAltText、queryByAltText、getAllByAltText、queryAllByAltText、findByAltText、findAllByAltText

API

getByAltText(
// If you're using `screen`, then skip the container argument:
container: HTMLElement,
text: TextMatch,
options?: {
exact?: boolean = true,
normalizer?: NormalizerFn,
}): HTMLElement

這將返回具有指定 alt 文字的元素(通常是 <img>)。請注意,它僅支援接受 alt 屬性的元素或自訂元素(因為我們不知道自訂元素是否實作了 alt):<img><input><area> (有意排除 <applet>,因為它已棄用)。

<img alt="Incredibles 2 Poster" src="/incredibles-2.png" />
import {screen} from '@testing-library/dom'

const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i)

選項

TextMatch 選項