import { test, expect } from '@playwright/test'; import { TEST_CONFIG, loginAsUser, setupErrorCapture, waitForToast, } from '../utils/test-helpers'; /** * Purchase E2E Test Suite (Audit 2.10) * * Couvre le flow critique d'achat : * - Marketplace โ†’ Add to cart โ†’ Checkout โ†’ Success * - Cart empty state */ test.describe('Purchase Flow', () => { let consoleErrors: string[] = []; let networkErrors: Array<{ url: string; status: number; method: string }> = []; test.beforeEach(async ({ page }) => { const errorCapture = setupErrorCapture(page); consoleErrors = errorCapture.consoleErrors; networkErrors = errorCapture.networkErrors; }); test('should add product to cart and checkout successfully', async ({ page }) => { console.log('๐Ÿงช [PURCHASE] Running: Add to cart and checkout'); await loginAsUser(page); await page.waitForTimeout(1000); await page.goto(`${TEST_CONFIG.FRONTEND_URL}/marketplace`); await page.waitForLoadState('domcontentloaded'); await page.waitForLoadState('networkidle', { timeout: 15000 }).catch(() => {}); const productCard = page.locator('article[aria-label^="Product:"]').first(); await expect(productCard).toBeVisible({ timeout: 10000 }); await productCard.hover(); await page.waitForTimeout(300); const addToCartButton = page.locator('button:has-text("Add to Cart")').first(); await expect(addToCartButton).toBeVisible({ timeout: 5000 }); await addToCartButton.click(); await page.waitForTimeout(1500); const cartButton = page.locator('button:has-text("Cart")').first(); await expect(cartButton).toBeVisible({ timeout: 5000 }); await cartButton.click(); const cartDialog = page.locator('[role="dialog"]').filter({ hasText: 'Shopping Cart' }); await expect(cartDialog).toBeVisible({ timeout: 5000 }); const checkoutButton = cartDialog.locator('button:has-text("Checkout")'); await expect(checkoutButton).toBeVisible({ timeout: 3000 }); await checkoutButton.click(); const successToast = await waitForToast(page, 'success', 15000); expect(successToast.toLowerCase()).toMatch(/order|success|placed/); console.log('โœ… [PURCHASE] Checkout successful'); }); test('should show cart empty message when no items', async ({ page }) => { console.log('๐Ÿงช [PURCHASE] Running: Cart empty state'); await loginAsUser(page); await page.waitForTimeout(1000); await page.goto(`${TEST_CONFIG.FRONTEND_URL}/marketplace`); await page.waitForLoadState('domcontentloaded'); await page.waitForLoadState('networkidle', { timeout: 15000 }).catch(() => {}); const cartButton = page.locator('button:has-text("Cart")').first(); await expect(cartButton).toBeVisible({ timeout: 5000 }); await cartButton.click(); const cartDialog = page.locator('[role="dialog"]').filter({ hasText: 'Shopping Cart' }); await expect(cartDialog).toBeVisible({ timeout: 5000 }); const emptyMessage = cartDialog.locator('text=/cart is empty|your cart is empty/i'); await expect(emptyMessage).toBeVisible({ timeout: 5000 }); console.log('โœ… [PURCHASE] Cart empty message displayed'); }); test.afterEach(async ({}, testInfo) => { console.log('\n๐Ÿ“Š [PURCHASE] === Final Verifications ==='); if (consoleErrors.length > 0) { console.log(`๐Ÿ”ด [PURCHASE] Console errors (${consoleErrors.length}):`); consoleErrors.forEach((e) => console.log(` - ${e}`)); } if (networkErrors.length > 0) { console.log(`๐Ÿ”ด [PURCHASE] Network errors (${networkErrors.length}):`); networkErrors.forEach((e) => console.log(` - ${e.method} ${e.url}: ${e.status}`)); } }); });