|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { register, generateUniqueUser } from './helpers/auth'; |
| 3 | +import { createArticle, editArticle, deleteArticle, favoriteArticle, unfavoriteArticle, generateUniqueArticle } from './helpers/articles'; |
| 4 | + |
| 5 | +test.describe('Articles', () => { |
| 6 | + test.beforeEach(async ({ page }) => { |
| 7 | + // Register and login before each test |
| 8 | + const user = generateUniqueUser(); |
| 9 | + await register(page, user.username, user.email, user.password); |
| 10 | + }); |
| 11 | + |
| 12 | + test.afterEach(async ({ context }) => { |
| 13 | + // Close the browser context to ensure complete isolation between tests. |
| 14 | + // This releases browser instances, network connections, and other resources. |
| 15 | + await context.close(); |
| 16 | + // Wait 500ms to allow async cleanup operations to complete. |
| 17 | + // Without this delay, running 6+ tests in sequence causes flaky failures |
| 18 | + // due to resource exhaustion (network connections, file descriptors, etc). |
| 19 | + // This timing issue manifests as timeouts when loading article pages. |
| 20 | + // This will be investigated and fixed later. |
| 21 | + await new Promise(resolve => setTimeout(resolve, 500)); |
| 22 | + }); |
| 23 | + |
| 24 | + test('should create a new article', async ({ page }) => { |
| 25 | + const article = generateUniqueArticle(); |
| 26 | + |
| 27 | + await createArticle(page, article); |
| 28 | + |
| 29 | + // Should be on article page |
| 30 | + await expect(page).toHaveURL(/\/article\/.+/); |
| 31 | + |
| 32 | + // Should show article content |
| 33 | + await expect(page.locator('h1')).toHaveText(article.title); |
| 34 | + await expect(page.locator('.article-content p')).toContainText(article.body); |
| 35 | + |
| 36 | + // Should show tags |
| 37 | + for (const tag of article.tags || []) { |
| 38 | + await expect(page.locator(`.tag-list .tag-default:has-text("${tag}")`)).toBeVisible(); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + test('should edit an existing article', async ({ page }) => { |
| 43 | + const article = generateUniqueArticle(); |
| 44 | + |
| 45 | + await createArticle(page, article); |
| 46 | + |
| 47 | + // Get the article slug from URL |
| 48 | + const url = page.url(); |
| 49 | + const slug = url.split('/article/')[1]; |
| 50 | + |
| 51 | + // Edit the article |
| 52 | + const updates = { |
| 53 | + title: `Updated ${article.title}`, |
| 54 | + description: `Updated ${article.description}`, |
| 55 | + }; |
| 56 | + |
| 57 | + await editArticle(page, slug, updates); |
| 58 | + |
| 59 | + // Should show updated content |
| 60 | + await expect(page.locator('h1')).toHaveText(updates.title); |
| 61 | + }); |
| 62 | + |
| 63 | + test('should delete an article', async ({ page }) => { |
| 64 | + const article = generateUniqueArticle(); |
| 65 | + |
| 66 | + await createArticle(page, article); |
| 67 | + |
| 68 | + // Delete the article |
| 69 | + await deleteArticle(page); |
| 70 | + |
| 71 | + // Should be redirected to home |
| 72 | + await expect(page).toHaveURL('/'); |
| 73 | + |
| 74 | + // Article should not appear on home page |
| 75 | + await expect(page.locator(`h1:has-text("${article.title}")`)).not.toBeVisible(); |
| 76 | + }); |
| 77 | + |
| 78 | + test('should favorite an article', async ({ page }) => { |
| 79 | + // Use an existing article from the demo backend (can't favorite own articles) |
| 80 | + await page.goto('/', { waitUntil: 'load' }); |
| 81 | + |
| 82 | + // Click on the first article to go to its detail page |
| 83 | + await page.click('.article-preview h1'); |
| 84 | + await page.waitForLoadState('load'); |
| 85 | + |
| 86 | + // Favorite the article using the helper (which expects to be on article detail page) |
| 87 | + await favoriteArticle(page); |
| 88 | + |
| 89 | + // Should see unfavorite button (use .first() since there are 2 buttons on the page) |
| 90 | + await expect(page.locator('button:has-text("Unfavorite")').first()).toBeVisible(); |
| 91 | + }); |
| 92 | + |
| 93 | + test('should unfavorite an article', async ({ page }) => { |
| 94 | + // Go to home page to find an article from demo backend (not own article) |
| 95 | + await page.goto('/', { waitUntil: 'load' }); |
| 96 | + |
| 97 | + // Wait for articles to load |
| 98 | + await page.waitForSelector('.article-preview', { timeout: 10000 }); |
| 99 | + |
| 100 | + // Get the username of the currently logged in user from the navbar |
| 101 | + const currentUsername = await page.locator('nav a[href^="/profile/"]').first().textContent(); |
| 102 | + |
| 103 | + // Find an article that's NOT from the current user |
| 104 | + const articles = await page.locator('.article-preview').all(); |
| 105 | + let articleToFavorite = null; |
| 106 | + |
| 107 | + for (const article of articles) { |
| 108 | + const authorName = await article.locator('.author').textContent(); |
| 109 | + if (authorName?.trim() !== currentUsername?.trim()) { |
| 110 | + articleToFavorite = article; |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (!articleToFavorite) { |
| 116 | + throw new Error('No articles from other users found'); |
| 117 | + } |
| 118 | + |
| 119 | + // Click on the article |
| 120 | + await articleToFavorite.locator('h1').click(); |
| 121 | + await page.waitForURL(/\/article\/.+/); |
| 122 | + |
| 123 | + // Wait for article page to load - should see Favorite button (not Delete button) |
| 124 | + await page.waitForSelector('button:has-text("Favorite")', { timeout: 10000 }); |
| 125 | + |
| 126 | + // Favorite it first |
| 127 | + await favoriteArticle(page); |
| 128 | + |
| 129 | + // Then unfavorite it |
| 130 | + await unfavoriteArticle(page); |
| 131 | + |
| 132 | + // Should see favorite button again (use .first() since there are 2 buttons on the page) |
| 133 | + await expect(page.locator('button:has-text("Favorite")').first()).toBeVisible(); |
| 134 | + }); |
| 135 | + |
| 136 | + test('should view article from home feed', async ({ page }) => { |
| 137 | + const article = generateUniqueArticle(); |
| 138 | + |
| 139 | + await createArticle(page, article); |
| 140 | + |
| 141 | + // Go to home |
| 142 | + await page.goto('/', { waitUntil: 'load' }); |
| 143 | + |
| 144 | + // Wait for articles to load |
| 145 | + await page.waitForSelector('.article-preview', { timeout: 10000 }); |
| 146 | + |
| 147 | + // Wait for our specific article to appear |
| 148 | + await page.waitForSelector(`h1:has-text("${article.title}")`, { timeout: 10000 }); |
| 149 | + |
| 150 | + // Click on the article link in the feed (h1 is inside a link) |
| 151 | + await Promise.all([ |
| 152 | + page.waitForURL(/\/article\/.+/), |
| 153 | + page.locator(`h1:has-text("${article.title}")`).first().click() |
| 154 | + ]); |
| 155 | + |
| 156 | + // Should be on article page |
| 157 | + await expect(page).toHaveURL(/\/article\/.+/); |
| 158 | + await expect(page.locator('h1')).toHaveText(article.title); |
| 159 | + }); |
| 160 | + |
| 161 | + test('should display article preview correctly', async ({ page }) => { |
| 162 | + const article = generateUniqueArticle(); |
| 163 | + |
| 164 | + await createArticle(page, article); |
| 165 | + |
| 166 | + // Go to home |
| 167 | + await page.goto('/'); |
| 168 | + |
| 169 | + // Article preview should show correct information |
| 170 | + const preview = page.locator('.article-preview').first(); |
| 171 | + await expect(preview.locator('h1')).toHaveText(article.title); |
| 172 | + await expect(preview.locator('p')).toContainText(article.description); |
| 173 | + |
| 174 | + // Should show author info |
| 175 | + await expect(preview.locator('.author')).toBeVisible(); |
| 176 | + |
| 177 | + // Should show tags |
| 178 | + for (const tag of article.tags || []) { |
| 179 | + await expect(preview.locator(`.tag-list .tag-default:has-text("${tag}")`)).toBeVisible(); |
| 180 | + } |
| 181 | + }); |
| 182 | + |
| 183 | + test('should only allow author to edit/delete article', async ({ page, browser }) => { |
| 184 | + const article = generateUniqueArticle(); |
| 185 | + |
| 186 | + // Create article as first user |
| 187 | + await createArticle(page, article); |
| 188 | + |
| 189 | + // Get article URL |
| 190 | + const articleUrl = page.url(); |
| 191 | + |
| 192 | + // Create a second user in new context (not sharing cookies with first user) |
| 193 | + const context2 = await browser.newContext(); |
| 194 | + const page2 = await context2.newPage(); |
| 195 | + const user2 = generateUniqueUser(); |
| 196 | + await register(page2, user2.username, user2.email, user2.password); |
| 197 | + |
| 198 | + // Visit the article as second user |
| 199 | + await page2.goto(articleUrl); |
| 200 | + |
| 201 | + // Should not see Edit/Delete buttons |
| 202 | + await expect(page2.locator('a:has-text("Edit Article")')).not.toBeVisible(); |
| 203 | + await expect(page2.locator('button:has-text("Delete Article")')).not.toBeVisible(); |
| 204 | + |
| 205 | + await context2.close(); |
| 206 | + }); |
| 207 | +}); |
0 commit comments