ByText
Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? 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
Buscará todos los elementos que contengan un nodo de texto con textContent
que coincida con el TextMatch especificado.
<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')
También funciona con inputs cuyo atributo type sea submit o button:
<input type="submit" value="Send data" />
Opciones
Opciones de TextMatch, más las siguientes:
selector
Nota
Consulta
getByLabelTextpara más detalles sobre cómo y cuándo usar la opciónselector
ignore
La opción ignore acepta un selector de consulta. Si
node.matches
devuelve true para ese selector, se ignorará el nodo. Por defecto es
'script, style' porque generalmente no quieres seleccionar estas etiquetas,
pero si tu contenido está en un archivo de script en línea, la etiqueta script
podría devolverse.
Si prefieres deshabilitar este comportamiento, establece ignore como false.