Next.js 15 Fullstack (App Router + Server Actions) · Lección

Pruebas E2E con Playwright/Cypress

Implemente pruebas de extremo a extremo con herramientas como Playwright o Cypress para simular las interacciones de los usuarios y validar los flujos completos de la aplicación.

Lección 3 de 411 pasos

Pruebas E2E con Playwright/Cypress es una lección gratuita de Next.js 15 Fullstack (App Router + Server Actions) en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Next.js 15 Fullstack (App Router + Server Actions), y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Next.js 15 Fullstack (App Router + Server Actions) incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

What is End-to-End Testing?

End-to-End (E2E) testing simulates a real user's journey through your application, from start to finish. It tests the entire flow, including the UI, database, network requests, and server logic.

Think of it as ensuring all the pieces of your Next.js application work together seamlessly, just like a user would expect.

Why E2E for Next.js?

For Next.js applications, E2E testing is crucial because they often involve complex interactions between client and server components, API routes, and external services.

  • Verifies Integrations: Checks if different parts (frontend, backend, database) communicate correctly.
  • User Experience: Ensures critical user flows, like login or checkout, function as intended.
  • Confidence: Provides high confidence that new features or changes haven't broken existing functionality.

Introducing Playwright

While Cypress is another popular choice, we'll focus on Playwright for E2E testing in Next.js. Playwright is a powerful open-source framework from Microsoft that enables reliable end-to-end testing.

Key features:

  • Supports all modern browsers (Chromium, Firefox, WebKit).
  • Fast and reliable test execution.
  • Auto-waits for elements to be ready.
  • Rich set of APIs for interacting with web pages.

Setting Up Playwright

Getting Playwright ready in your Next.js project is straightforward. You'll typically run an initialization command that sets up the necessary files and installs browser binaries.

Open your terminal in your Next.js project and run:

npm init playwright@latest

Your First E2E Test

Let's write a basic test to navigate to your application's homepage and verify its title. Playwright tests are written using a test runner similar to Jest.

After setup, you'll find a tests/example.spec.ts file. Modify it:

import { test, expect } from '@playwright/test';

test('homepage has title', async ({ page }) => {
  await page.goto('http://localhost:3000/');
  await expect(page).toHaveTitle(/Next.js App/);
});

Interacting with UI Elements

Playwright allows you to simulate user actions like clicking buttons or typing into input fields. You select elements using CSS selectors, text content, or specific roles.

Here's how to fill an input and click a button:

import { test, expect } from '@playwright/test';

test('user can interact with elements', async ({ page }) => {
  await page.goto('http://localhost:3000/contact'); // Assume a contact page
  await page.fill('input[name="name"]', 'Coddy Kit');
  await page.click('button[type="submit"]');
  // After clicking, we might expect a success message to appear
  await expect(page.locator('.success-message')).toBeVisible();
});

Assertions and Visibility

Assertions are key to E2E tests, verifying that your application behaves as expected. Playwright's expect provides many matchers to check element properties, text, and visibility.

Let's verify text content and visibility:

import { test, expect } from '@playwright/test';

test('elements display correct text', async ({ page }) => {
  await page.goto('http://localhost:3000/about');
  const heading = page.locator('h1');
  await expect(heading).toHaveText('About Us');
  const paragraph = page.locator('p');
  await expect(paragraph).toContainText('Our mission');
});

Testing Form Submissions

E2E tests are perfect for validating entire form submission flows, ensuring data is sent to the server, processed, and the UI updates correctly. This covers both client-side and server-side logic.

Consider a contact form:

import { test, expect } from '@playwright/test';

test('submit contact form successfully', async ({ page }) => {
  await page.goto('http://localhost:3000/contact');
  await page.fill('input[name="email"]', 'test@example.com');
  await page.fill('textarea[name="message"]', 'Hello CoddyKit!');
  await page.click('button[type="submit"]');
  await expect(page.locator('.form-status')).toHaveText('Message sent!');
});

Running Your Tests

Once your tests are written, you can easily run them from the command line. Playwright offers various options, like running tests in headless mode (without opening a browser) or with a UI for debugging.

To run all tests:

npx playwright test

E2E Best Practices

Writing effective E2E tests requires careful planning to make them robust and maintainable. Which of these are good practices?

Recap: E2E Testing

Congratulations! You've explored End-to-End testing for Next.js applications using Playwright.

  • E2E tests simulate real user journeys.
  • Playwright is a powerful tool for cross-browser E2E testing.
  • You learned to set up Playwright, write tests for navigation, user interaction, and form submissions.
  • Remember to follow best practices for robust and maintainable E2E tests.
Gratis para empezar

Aprende TypeScript con un tutor de IA — gratis

Escribe y ejecuta código real en tu navegador, obtén ayuda instantánea de un tutor de IA disponible 24/7 y continúa donde lo dejaste en la web o en la aplicación.

Cursos
22
Lecciones
88

Preguntas frecuentes

¿La lección «Pruebas E2E con Playwright/Cypress» es gratis?

Sí — el texto completo de «Pruebas E2E con Playwright/Cypress» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Next.js 15 Fullstack (App Router + Server Actions), actualiza a CoddyKit PRO. El curso de Next.js 15 Fullstack (App Router + Server Actions) incluye 4 lecciones en total.

¿Qué aprenderé en «Pruebas E2E con Playwright/Cypress»?

Implemente pruebas de extremo a extremo con herramientas como Playwright o Cypress para simular las interacciones de los usuarios y validar los flujos completos de la aplicación. Practicas Next.js 15 Fullstack (App Router + Server Actions) con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Next.js 15 Fullstack (App Router + Server Actions)?

No se requiere experiencia previa. Next.js 15 Fullstack (App Router + Server Actions) en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.

¿Cuánto tiempo toma la lección «Pruebas E2E con Playwright/Cypress»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Next.js 15 Fullstack (App Router + Server Actions)?

Sí. Cada lección de Next.js 15 Fullstack (App Router + Server Actions) incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Pruebas unitarias de componentes
  2. Pruebas de integración de páginas
  3. Pruebas E2E con Playwright/Cypress
  4. Simulación y pruebas de rutas de API
← Volver a Next.js 15 Fullstack (App Router + Server Actions)