ByText
Esta página foi traduzida por PageTurner AI (beta). Não é oficialmente endossada pelo projeto. Encontrou um erro? Reportar problema →
getByText, queryByText, getAllByText, queryAllByText, findByText, findAllByText
API
getByText(
// If you're using `screen`, then skip the container argument:
container: HTMLElement,
text: TextMatch,
options?: {
selector?: string = '*',
exact?: boolean = true,
ignore?: string|boolean = 'script, style',
normalizer?: NormalizerFn,
}): HTMLElement
Esta função buscará todos os elementos que possuem um nó de texto com textContent
correspondente ao TextMatch fornecido.
<a href="/about">About ℹ️</a>
- Native
- React
- Angular
- Cypress
import {screen} from '@testing-library/dom'
const aboutAnchorNode = screen.getByText(/about/i)
import {render, screen} from '@testing-library/react'
render(<MyComponent />)
const aboutAnchorNode = screen.getByText(/about/i)
import {render, screen} from '@testing-library/angular'
await render(MyComponent)
const aboutAnchorNode = screen.getByText(/about/i)
cy.findByText(/about/i).should('exist')
Também funciona com inputs cujo atributo type seja submit ou
button:
<input type="submit" value="Send data" />
Opções
Opções do TextMatch, além das seguintes:
selector
Nota
Veja
getByLabelTextpara mais detalhes sobre como e quando usar a opçãoselector
ignore
A opção ignore aceita um seletor de consulta. Se
node.matches
retornar verdadeiro para esse seletor, o nó será ignorado. Por padrão, isso é definido como
'script, style' porque geralmente você não deseja selecionar essas tags, mas se
seu conteúdo estiver em um arquivo de script inline, a tag de script pode ser retornada.
Se você preferir desabilitar esse comportamento, defina ignore como false.