ByAltText
Esta página foi traduzida por PageTurner AI (beta). Não é oficialmente endossada pelo projeto. Encontrou um erro? Reportar problema →
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
Isso retornará o elemento (normalmente uma <img>) que possui o texto alt
fornecido. Note que ele suporta apenas elementos que aceitam o atributo alt ou
elementos personalizados
(já que não sabemos se um elemento personalizado implementa alt ou não):
<img>,
<input>,
e <area>
(excluindo intencionalmente
<applet>
por estar obsoleto).
<img alt="Incredibles 2 Poster" src="/incredibles-2.png" />
- Native
- 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')
Opções
Opções de TextMatch