Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

706
Vistas
Can you run multiple tests in one browser context Playwright Javascript?

Is it possible to run multiple tests in one browser window for playwright/test?

currently it will hit browser.close(); after every test even though they are testing on the same page which puts a lot of extra time on the tests.

test.beforeAll(async ({ browser }) => {
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('https://example.com');
});

test('nav test', async ({ page }) => {    
  const name = await page.innerText('.navbar__title');  
  expect(name).toBe('Playwright');
});

test('header test', async ({ page }) => {   
  const name = await page.innerText('.navbar__header');  
  expect(name).toBe('Playwright');
});
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

When you create a tests like this test('header test', async ({page}) => { you're specifying page and telling it to create a new page context.

Remove the page from the test - and share the one you create from your .beforeAll

Try this:

test.describe('1 page multiple tests', () => {
    let page;
    test.beforeAll(async ({ browser }) => {
        const context = await browser.newContext();
        page = await context.newPage();
        await page.goto('https://example.com');
    });

    test.afterAll(async ({ browser }) => {
        browser.close;
    });

    test('nav test', async () => {
        const name = await page.innerText('h1');
        expect(name).toContain('Example');
    });

    test('header test', async () => {
        const name = await page.innerText('h1');
        expect(name).toContain('Domain');
    });
});

Run it like this :

npx playwright test .\StackTests_SinglePage.spec.ts --headed

(you can see the name of my file in there)

You might need to toggle it down to 1 worker if it tries to parallel run your test.

For me, that code opens 1 borwser, 1 page, passes both tests the closes out. enter image description here

over 4 years ago · Santiago Trujillo Denunciar

0

Can you try wrapping the tests in a describe block? So they are treated as a group and not as an individual tests.

test.describe('two tests for same page', () => {
   test('nav test', async ({ page }) => {    
     const name = await page.innerText('.navbar__title');  
     expect(name).toBe('Playwright');
   });

   test('header test', async ({ page }) => {   
    const name = await page.innerText('.navbar__header');  
    expect(name).toBe('Playwright');
  });
});

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda