|
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | +import { NEXTJS_API_PORT, NEXTJS_PORT, NUXT_PORT, SVELTE_PORT, fetchCaptures, getBaseUrl, getLatestCapture, waitMs } from './util' |
| 3 | +import { join } from 'path' |
| 4 | +import { existsSync, rmSync } from 'fs' |
| 5 | + |
| 6 | +test.afterAll(() => { |
| 7 | + // Delete cache directories |
| 8 | + const cacheDirectories = [ |
| 9 | + join(process.cwd(), 'examples', 'with-nextjs', '.next'), |
| 10 | + join(process.cwd(), 'examples', 'with-nextjs-api', '.next') |
| 11 | + ] |
| 12 | + cacheDirectories.forEach((cacheFilePath) => { |
| 13 | + if (existsSync(cacheFilePath)) { |
| 14 | + rmSync(cacheFilePath, { recursive: true }) |
| 15 | + } |
| 16 | + }) |
| 17 | +}) |
| 18 | + |
| 19 | +test('should capture back-end bug in nextjs-api example', async ({ page }) => { |
| 20 | + const startTime = new Date(); |
| 21 | + const capturesBefore = await fetchCaptures('sk_3X-otNR_ZxIdw8cVxNbFcDivQF9B5Vw_bGu564jR35GMyVXX'); |
| 22 | + // Start from the index page (the baseURL is set via the webServer in the playwright.config.ts) |
| 23 | + await page.goto(getBaseUrl(NEXTJS_API_PORT)); |
| 24 | + await page.reload() |
| 25 | + await waitMs(3_000) |
| 26 | + |
| 27 | + // Find the input |
| 28 | + await page.locator('input').fill('user-1'); |
| 29 | + |
| 30 | + // Click the submit button |
| 31 | + await page.click('text=Fetch') |
| 32 | + |
| 33 | + await waitMs(3_000) |
| 34 | + |
| 35 | + // ^^ Happy path, we don't expect anything |
| 36 | + const capturesDuring = await fetchCaptures('sk_3X-otNR_ZxIdw8cVxNbFcDivQF9B5Vw_bGu564jR35GMyVXX'); |
| 37 | + expect(capturesBefore.length).toBe(capturesDuring.length) |
| 38 | + |
| 39 | + // Now error path |
| 40 | + |
| 41 | + // Find the input |
| 42 | + await page.locator('input').fill('user-51'); |
| 43 | + |
| 44 | + // Click the submit button |
| 45 | + await page.click('text=Fetch') |
| 46 | + |
| 47 | + await waitMs(5_000) |
| 48 | + |
| 49 | + const capturesAfter = await fetchCaptures('sk_3X-otNR_ZxIdw8cVxNbFcDivQF9B5Vw_bGu564jR35GMyVXX'); |
| 50 | + const latestCapture = getLatestCapture(capturesAfter, startTime) |
| 51 | + expect(latestCapture.capturedUserId).toEqual('user-51'); |
| 52 | + expect(latestCapture.functionName).toEqual('User not found'); |
| 53 | + expect(capturesAfter.length).toBeGreaterThan(capturesBefore.length); |
| 54 | +}) |
| 55 | + |
| 56 | +// @todo: refactor the client-side e2e |
| 57 | + |
| 58 | +test('should capture front-end bug in nextjs example', async ({ page }) => { |
| 59 | + const startTime = new Date(); |
| 60 | + const capturesBefore = await fetchCaptures('sk_Y8dYLe5VoNJfDI_CPEqF8AqMTEwWAvTwBi7Ml-pN9bzWsgsP'); |
| 61 | + // Start from the index page (the baseURL is set via the webServer in the playwright.config.ts) |
| 62 | + await page.goto(getBaseUrl(NEXTJS_PORT)); |
| 63 | + await page.reload() |
| 64 | + |
| 65 | + // Find the input |
| 66 | + await page.locator('input').fill('wrong') |
| 67 | + |
| 68 | + // Click the submit button |
| 69 | + await page.click('text=Submit') |
| 70 | + |
| 71 | + // Wait for some console .log |
| 72 | + const consolePromise = await page.waitForEvent('console'); |
| 73 | + const consoleLogsArgs = await consolePromise.args() |
| 74 | + // expect(await consoleLogsArgs[0]?.jsonValue()).toContain('[🐛 post] https://www.useflytrap.com/api/v1/captures') |
| 75 | + |
| 76 | + await waitMs(10_000); |
| 77 | + |
| 78 | + // Check the API for the capture |
| 79 | + const capturesAfter = await fetchCaptures('sk_Y8dYLe5VoNJfDI_CPEqF8AqMTEwWAvTwBi7Ml-pN9bzWsgsP'); |
| 80 | + const latestCapture = getLatestCapture(capturesAfter, startTime) |
| 81 | + expect(latestCapture?.functionName).toEqual('Input value "wrong" is wrong!') |
| 82 | + expect(capturesAfter.length).toBeGreaterThan(capturesBefore.length) |
| 83 | +}) |
| 84 | + |
| 85 | + |
| 86 | +test('should capture front-end bug in sveltekit example', async ({ page }) => { |
| 87 | + const startTime = new Date(); |
| 88 | + const capturesBefore = await fetchCaptures('sk_K8zYjKVWq6himuvhrPROShSjsWwAsseEY7K4Y9GZsYTwNxLt'); |
| 89 | + // Start from the index page (the baseURL is set via the webServer in the playwright.config.ts) |
| 90 | + await page.goto(getBaseUrl(SVELTE_PORT)); |
| 91 | + await page.reload() |
| 92 | + await waitMs(1000) |
| 93 | + |
| 94 | + // Find the input |
| 95 | + await page.locator('input').fill('wrong') |
| 96 | + await waitMs(1000) |
| 97 | + |
| 98 | + // Click the submit button |
| 99 | + await page.click('text=Submit') |
| 100 | + const consolePromise = await page.waitForEvent('console') |
| 101 | + const consoleArgs = await consolePromise.args(); |
| 102 | + |
| 103 | + expect(await consoleArgs[0]?.jsonValue()).toContain('[🐛 post] https://www.useflytrap.com/api/v1/captures') |
| 104 | + await waitMs(3_000); |
| 105 | + |
| 106 | + // Check the API for the capture |
| 107 | + const capturesAfter = await fetchCaptures('sk_K8zYjKVWq6himuvhrPROShSjsWwAsseEY7K4Y9GZsYTwNxLt'); |
| 108 | + const latestCapture = getLatestCapture(capturesAfter, startTime) |
| 109 | + expect(latestCapture?.functionName).toEqual('Input value "wrong" is wrong!') |
| 110 | + expect(capturesAfter.length).toBeGreaterThan(capturesBefore.length); |
| 111 | +}) |
| 112 | + |
| 113 | +test('should capture front-end bug in nuxt example', async ({ page }) => { |
| 114 | + const startTime = new Date(); |
| 115 | + const capturesBefore = await fetchCaptures('sk_TnBe2CSBNPGE-9jDtiQDvaoef8LOJ17T_xpIvKxVcBJuskGX'); |
| 116 | + // Start from the index page (the baseURL is set via the webServer in the playwright.config.ts) |
| 117 | + await page.goto(getBaseUrl(NUXT_PORT)); |
| 118 | + await page.reload() |
| 119 | + await waitMs(1000) |
| 120 | + |
| 121 | + // Find the input |
| 122 | + await page.locator('input').fill('wrong') |
| 123 | + await waitMs(1000) |
| 124 | + |
| 125 | + // Click the submit button |
| 126 | + await page.click('text=Submit') |
| 127 | + const consolePromise = await page.waitForEvent('console') |
| 128 | + const consoleArgs = await consolePromise.args(); |
| 129 | + |
| 130 | + expect(await consoleArgs[0]?.jsonValue()).toContain('[🐛 post] https://www.useflytrap.com/api/v1/captures') |
| 131 | + await waitMs(3_000); |
| 132 | + |
| 133 | + // Check the API for the capture |
| 134 | + const capturesAfter = await fetchCaptures('sk_TnBe2CSBNPGE-9jDtiQDvaoef8LOJ17T_xpIvKxVcBJuskGX'); |
| 135 | + const latestCapture = getLatestCapture(capturesAfter, startTime) |
| 136 | + expect(latestCapture?.functionName).toEqual('Input value "wrong" is wrong!') |
| 137 | + expect(capturesAfter.length).toBeGreaterThan(capturesBefore.length); |
| 138 | +}) |
| 139 | + |
| 140 | +test.skip('should navigate to the about page', async ({ page }) => { |
| 141 | + // Start from the index page (the baseURL is set via the webServer in the playwright.config.ts) |
| 142 | + await page.goto('/') |
| 143 | + // Find an element with the text 'About Page' and click on it |
| 144 | + await page.click('text=About Page') |
| 145 | + // The new url should be "/about" (baseURL is used there) |
| 146 | + await expect(page).toHaveURL('/about') |
| 147 | + // The new page should contain an h1 with "About Page" |
| 148 | + await expect(page.locator('h1')).toContainText('About Page') |
| 149 | +}) |
0 commit comments