|
1 | 1 | import { test, expect } from '@playwright/test' |
| 2 | +import { cleanupTestRecords } from './utils/cleanup' |
2 | 3 |
|
3 | | -test.describe('Fluxo de Cadastro', () => { |
| 4 | +test.afterEach(async () => { |
| 5 | + await cleanupTestRecords() |
| 6 | +}) |
| 7 | + |
| 8 | +test.describe('Fluxo de Cadastro Online', () => { |
4 | 9 | test('Fluxo completo do formulário', async ({ page }) => { |
5 | | - // 1. Acessa e aguarda o carregamento inicial (lazy load) |
6 | 10 | await page.goto('/staffs') |
7 | | - |
8 | | - // Aguarda o título da página ou o botão aparecer (resiliência para lazy load) |
9 | 11 | await expect(page.getByRole('heading', { name: /colaboradores/i })).toBeVisible({ timeout: 20000 }) |
10 | 12 |
|
11 | | - // 2. Clica no botão |
12 | | - const btnNovo = page.getByRole('button', { name: /novo colaborador/i }) |
13 | | - await btnNovo.click() |
| 13 | + await page.getByRole('link', { name: /novo colaborador/i }).click() |
| 14 | + await expect(page).toHaveURL(/\/staffs\/new/) |
| 15 | + |
| 16 | + const timestamp = Date.now() |
| 17 | + const uniqueName = `Wellington Teste E2E ${timestamp}` |
| 18 | + const uniqueEmail = `e2e.${timestamp}@test.com` |
| 19 | + |
| 20 | + await page.getByLabel(/nome completo/i).fill(uniqueName) |
| 21 | + await page.getByLabel(/e-mail corporativo/i).fill(uniqueEmail) |
| 22 | + await page.getByRole('button', { name: /próximo passo/i }).click() |
| 23 | + |
| 24 | + await page.getByLabel(/selecione o departamento/i).click() |
| 25 | + await page.getByRole('option', { name: 'TI', exact: true }).click() |
| 26 | + await page.getByRole('button', { name: /finalizar cadastro/i }).click() |
| 27 | + |
| 28 | + await expect( |
| 29 | + page.getByText(/colaborador cadastrado com sucesso/i) |
| 30 | + ).toBeVisible({ timeout: 15000 }) |
| 31 | + |
| 32 | + await expect(page).toHaveURL(/\/staffs$/, { timeout: 5000 }) |
| 33 | + await expect(page.getByRole('heading', { name: /colaboradores/i })).toBeVisible({ timeout: 10000 }) |
| 34 | + |
| 35 | + await expect(page.getByRole('cell', { name: uniqueName })).toBeVisible({ timeout: 15000 }) |
| 36 | + }) |
| 37 | +}) |
| 38 | + |
| 39 | +test.describe('Fluxo de Cadastro Offline', () => { |
| 40 | + test('salva localmente e exibe status de sincronização', async ({ context, page }) => { |
| 41 | + // 1. Garante que a página carregue online primeiro para ter a aplicação disponível |
| 42 | + await page.goto('/staffs') |
| 43 | + await expect(page.getByRole('heading', { name: /colaboradores/i })).toBeVisible({ timeout: 20000 }) |
14 | 44 |
|
15 | | - // 3. Preenche o Passo 0: Infos Básicas |
| 45 | + // 2. Navega para o formulário |
| 46 | + await page.getByRole('link', { name: /novo colaborador/i }).click() |
16 | 47 | await expect(page).toHaveURL(/\/staffs\/new/) |
17 | | - await page.getByLabel(/nome/i).fill('Wellington Teste E2E') |
18 | | - await page.getByLabel(/e-mail/i).fill(`e2e.${Date.now()}@test.com`) |
19 | | - await page.getByRole('button', { name: /próximo/i }).click() |
20 | | - |
21 | | - // 4. Preenche o Passo 1: Infos Profissionais |
22 | | - await page.getByLabel(/departamento/i).click() |
23 | | - await page.getByRole('option', { name: /ti/i }).click() |
24 | | - await page.getByRole('button', { name: /salvar/i }).click() |
25 | | - |
26 | | - // 5. Validação final na listagem |
27 | | - await expect(page).toHaveURL(/\/staffs/, { timeout: 15000 }) |
28 | | - await expect(page.getByText('Wellington Teste E2E')).toBeVisible({ timeout: 15000 }) |
| 48 | + |
| 49 | + // 3. Simula a perda de conexão de rede |
| 50 | + await context.setOffline(true) |
| 51 | + await page.evaluate(() => window.dispatchEvent(new Event('offline'))) |
| 52 | + |
| 53 | + const timestamp = Date.now() |
| 54 | + const uniqueName = `Offline User ${timestamp}` |
| 55 | + const uniqueEmail = `offline.${timestamp}@test.com` |
| 56 | + |
| 57 | + // 4. Preenche e submete o formulário |
| 58 | + await page.getByLabel(/nome completo/i).fill(uniqueName) |
| 59 | + await page.getByLabel(/e-mail corporativo/i).fill(uniqueEmail) |
| 60 | + await page.getByRole('button', { name: /próximo passo/i }).click() |
| 61 | + |
| 62 | + await page.getByLabel(/selecione o departamento/i).click() |
| 63 | + await page.getByRole('option', { name: 'TI', exact: true }).click() |
| 64 | + await page.getByRole('button', { name: /finalizar cadastro/i }).click() |
| 65 | + |
| 66 | + // 5. Verifica se o toast de sucesso é exibido (mesmo offline) |
| 67 | + await expect( |
| 68 | + page.getByText(/colaborador cadastrado com sucesso/i) |
| 69 | + ).toBeVisible({ timeout: 15000 }) |
| 70 | + |
| 71 | + // 6. Verifica se o redirecionamento para a lista acontece |
| 72 | + await expect(page).toHaveURL(/\/staffs$/, { timeout: 10000 }) |
| 73 | + await expect(page.getByRole('heading', { name: /colaboradores/i })).toBeVisible({ timeout: 10000 }) |
| 74 | + |
| 75 | + // 7. Verifica se o novo usuário aparece na lista |
| 76 | + const userRow = page.locator('tr', { hasText: uniqueName }) |
| 77 | + await expect(userRow).toBeVisible({ timeout: 10000 }) |
| 78 | + await expect(userRow.getByText(/Ativo/i)).toBeVisible({ timeout: 5000 }) |
| 79 | + |
| 80 | + // 8. Restaura a conexão |
| 81 | + await context.setOffline(false) |
| 82 | + await page.evaluate(() => window.dispatchEvent(new Event('online'))) |
29 | 83 | }) |
30 | 84 | }) |
0 commit comments