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" />
- 原生
- React
- Angular
- Cypress
import {screen} from '@testing-library/dom'
const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i)
import {render, screen} from '@testing-library/react'
render(<MyComponent />)
const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i)
import {render, screen} from '@testing-library/angular'
await render(MyComponent)
const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i)
cy.findByAltText(/incredibles.*? poster/i).should('exist')
選項
TextMatch 選項