|
| 1 | +import { expect, test } from '@playwright/test' |
| 2 | + |
| 3 | +test.describe('Cart', () => { |
| 4 | + test('add product to cart from product page', async ({ page }) => { |
| 5 | + await page.goto('/') |
| 6 | + |
| 7 | + // Click on a product card to navigate to product detail |
| 8 | + const productCard = page.locator('main a').filter({ has: page.locator('img') }).first() |
| 9 | + await productCard.click() |
| 10 | + |
| 11 | + // Should be on product detail page |
| 12 | + await expect(page.locator('h1')).toBeVisible() |
| 13 | + |
| 14 | + // Click "Add to cart" button |
| 15 | + const addButton = page.getByRole('button', { name: /add to cart/i }) |
| 16 | + await expect(addButton).toBeVisible({ timeout: 10_000 }) |
| 17 | + await addButton.click() |
| 18 | + |
| 19 | + // Cart aside (xl viewport) should now show cart content |
| 20 | + const cartTitle = page.locator('aside').getByText('Cart') |
| 21 | + await expect(cartTitle).toBeVisible() |
| 22 | + }) |
| 23 | + |
| 24 | + test('increment and decrement product quantity', async ({ page }) => { |
| 25 | + await page.goto('/') |
| 26 | + |
| 27 | + // Navigate to product and add it |
| 28 | + const productCard = page.locator('main a').filter({ has: page.locator('img') }).first() |
| 29 | + await productCard.click() |
| 30 | + await page.getByRole('button', { name: /add to cart/i }).click() |
| 31 | + |
| 32 | + // After adding, +/- buttons should replace "Add to cart" |
| 33 | + const plusButton = page.locator('main').getByRole('button', { name: 'Plus' }) |
| 34 | + const minusButton = page.locator('main').getByRole('button', { name: 'Minus' }) |
| 35 | + |
| 36 | + await expect(plusButton).toBeVisible() |
| 37 | + await expect(minusButton).toBeVisible() |
| 38 | + |
| 39 | + // Increment |
| 40 | + await plusButton.click() |
| 41 | + |
| 42 | + // Decrement twice to remove |
| 43 | + await minusButton.click() |
| 44 | + await minusButton.click() |
| 45 | + |
| 46 | + // "Add to cart" button should reappear |
| 47 | + await expect(page.getByRole('button', { name: /add to cart/i })).toBeVisible() |
| 48 | + }) |
| 49 | + |
| 50 | + test('cart aside is visible on desktop', async ({ page }) => { |
| 51 | + await page.goto('/') |
| 52 | + |
| 53 | + // On xl viewport, aside is always visible |
| 54 | + const aside = page.locator('aside') |
| 55 | + await expect(aside).toBeVisible() |
| 56 | + }) |
| 57 | + |
| 58 | + test('navigate to checkout from cart', async ({ page }) => { |
| 59 | + await page.goto('/') |
| 60 | + |
| 61 | + // Add a product |
| 62 | + const productCard = page.locator('main a').filter({ has: page.locator('img') }).first() |
| 63 | + await productCard.click() |
| 64 | + await page.getByRole('button', { name: /add to cart/i }).click() |
| 65 | + |
| 66 | + // Click "Okay, next" link in cart aside |
| 67 | + const nextButton = page.locator('aside a[href="/checkout"]') |
| 68 | + await expect(nextButton).toBeVisible() |
| 69 | + await nextButton.click() |
| 70 | + |
| 71 | + await page.waitForURL('**/checkout') |
| 72 | + }) |
| 73 | + |
| 74 | + test('delivery method tabs are available in cart', async ({ page }) => { |
| 75 | + await page.goto('/') |
| 76 | + |
| 77 | + // Add a product |
| 78 | + const productCard = page.locator('main a').filter({ has: page.locator('img') }).first() |
| 79 | + await productCard.click() |
| 80 | + await page.getByRole('button', { name: /add to cart/i }).click() |
| 81 | + |
| 82 | + // Delivery method tabs should be visible in cart aside |
| 83 | + const deliveryTab = page.locator('aside').getByRole('tab', { name: /delivery/i }) |
| 84 | + const pickupTab = page.locator('aside').getByRole('tab', { name: /pickup/i }) |
| 85 | + |
| 86 | + await expect(deliveryTab).toBeVisible() |
| 87 | + await expect(pickupTab).toBeVisible() |
| 88 | + |
| 89 | + // Switch to pickup |
| 90 | + await pickupTab.click() |
| 91 | + await expect(pickupTab).toHaveAttribute('aria-selected', 'true') |
| 92 | + }) |
| 93 | +}) |
0 commit comments