Saltar al contenido principal

Biblioteca de Pruebas de Puppeteer

[Traducción Beta No Oficial]

Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →

pptr-testing-library es un adaptador ligero que permite usar DOM Testing Library con puppeteer.

npm install --save-dev puppeteer pptr-testing-library

Uso

const puppeteer = require('puppeteer')
const {getDocument, queries, waitFor} = require('pptr-testing-library')

const {getByTestId, getByLabelText} = queries

const browser = await puppeteer.launch()
const page = await browser.newPage()

// Grab ElementHandle for document
const $document = await getDocument(page)
// Your favorite query methods are available
const $form = await getByTestId($document, 'my-form')
// returned elements are Puppeteer ElementHandles too!
const $email = await getByLabelText($form, 'Email')
// interact with puppeteer like usual
await $email.type('pptr@example.com')
// waiting works too!
await waitFor(() => getByText('Loading...'))

¿Te parece demasiado ajeno a Puppeteer? ¡Puedes adjuntar todos los métodos de DOM Testing Library directamente al ElementHandle de Puppeteer.

const puppeteer = require('puppeteer')
require('pptr-testing-library/extend')

const browser = await puppeteer.launch()
const page = await browser.newPage()

// getDocument is added to prototype of Page
const $document = await page.getDocument()
// query methods are added directly to prototype of ElementHandle
const $form = await $document.getByTestId('my-form')
// destructuring works if you explicitly call getQueriesForElement
const {getByLabelText} = $form.getQueriesForElement()
// ...
const $email = await getByLabelText('Email')

API

Métodos exclusivos, no incluidos en DOM Testing Library.

  • getDocument(page: puppeteer.Page): ElementHandle - obtiene un ElementHandle para el documento

Métodos delegados

DOM Testing Library se inyecta en la página controlada por Puppeteer en cada consulta, por lo que todos los resultados serán asíncronos. Aún se recomienda usar los métodos integrados de Puppeteer para interacciones en lugar de fireEvent.

Limitaciones conocidas

  • El método waitForElement no está expuesto. Puppeteer tiene sus propias utilidades de espera que entran en conflicto con el estilo usado en DOM Testing Library. Consulta el issue en GitHub.

  • El método fireEvent no está disponible; usa las funciones nativas de Puppeteer.

  • Las extensiones de aserción expect no están disponibles.