Pesquisa de site

node.js - Executar testes agrupados sequencialmente usando Playwright


Estou usando Playwright com Node.js e agrupei alguns testes juntos assim

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

test.describe('Add a simple invoice test', () => {
    test('01.Login & add an invoice', async ({ page }) => {
        await page.goto("https://someUrl.com");
        await page.fill('input[id="email"]', "someEmailAddress");
        await page.fill('input[ng-model="ctrl.user.password"]', "somePassword");
        await page.click('button[id="login-btn"]');
    });

    test('02.Add an invoice', async ({ page }) => {
        await page.click('[name="invoice"]');
        await page.click('button[id="addInvoiceButton"]');
        await page.click('a[ng-click="ctrl.goToAddInvoice()"]');
        await page.fill('#invoiceTitle', Math.random().toString(36).substring(7));
        await page.fill('#exampleInputAmount', "120");
        await page.click("#invoiceCategory")
        await page.fill("#invoiceCategory > input", "Car")
        await page.keyboard.press("Enter");
        await page.click('button[id="submitInvoiceButton"]');
    });
});

O problema é que esses 2 testes rodam em paralelo, enquanto o 02 depende do 01, já que há um login necessário.

Como faço para que 2 testes agrupados sejam executados no mesmo contexto?

Resposta #1

A solução é muito simples. Está sendo executada como um teste independente, já que você está passando {page} em cada teste, então, se você quiser usar o mesmo contexto para ambos os testes, você precisa modificar o teste como abaixo.

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

test.describe('Add a simple invoice test', () => {
  let page: Page;
  test.beforeAll(async ({ browser }) => {
    page = await browser.newPage();
  });

    test('01.Login & add an invoice', async () => { // do not pass page
        await page.goto("https://someUrl.com");
        await page.fill('input[id="email"]', "someEmailAddress");
        await page.fill('input[ng-model="ctrl.user.password"]', "somePassword");
        await page.click('button[id="login-btn"]');
    });

    test('02.Add an invoice', async () => { //do not pass page 
        await page.click('[name="invoice"]');
        await page.click('button[id="addInvoiceButton"]');
        await page.click('a[ng-click="ctrl.goToAddInvoice()"]');
        await page.fill('#invoiceTitle', Math.random().toString(36).substring(7));
        await page.fill('#exampleInputAmount', "120");
        await page.click("#invoiceCategory")
        await page.fill("#invoiceCategory > input", "Car")
        await page.keyboard.press("Enter");
        await page.click('button[id="submitInvoiceButton"]');
    });
});

Isso deve funcionar como você esperava. Você também pode se referir ao Artigo da Dzone sobre o mesmo assunto.

Nota: O Playwright não recomenda esta abordagem, mas esta resposta deve atender à sua necessidade.

Resposta #2

Infelizmente, navegadores/sistemas operacionais possuem estado global, como o conteúdo da área de transferência, então você terá condições de corrida se testar tais recursos em paralelo.

Se todos os seus testes estão rodando em paralelo, você provavelmente ativou o paralelismo na configuração do Playwright:

const config: PlaywrightTestConfig = {
  fullyParallel: true,
  ...

Isso é bom - você deve deixar fullyParallel: true para executar seus testes mais rapidamente e, em seguida, optar por não executar os testes em um único arquivo (ou bloco describe único) de forma serial, adicionando test.describe.configure({ mode: 'serial' }); assim:

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

test.describe('Add a simple invoice test', () => {
  test.describe.configure({ mode: 'serial' });

  test('01.Login & add an invoice', async ({ page }) => {
    await page.goto("https://someUrl.com");
    await page.fill('input[id="email"]', "someEmailAddress");
    await page.fill('input[ng-model="ctrl.user.password"]', "somePassword");
    await page.click('button[id="login-btn"]');
  });

  test('02.Add an invoice', async ({ page }) => {
    await page.click('[name="invoice"]');
    await page.click('button[id="addInvoiceButton"]');
    await page.click('a[ng-click="ctrl.goToAddInvoice()"]');
    await page.fill('#invoiceTitle', Math.random().toString(36).substring(7));
    await page.fill('#exampleInputAmount', "120");
    await page.click("#invoiceCategory")
    await page.fill("#invoiceCategory > input", "Car")
    await page.keyboard.press("Enter");
    await page.click('button[id="submitInvoiceButton"]');
  });
});

Leia mais na documentação do Playwright aqui: https://playwright.dev/docs/next/test-parallel#parallelize-tests-in-a-single-file

Artigos relacionados