diff --git a/.gitignore b/.gitignore index 5c93b61..3cec5f6 100644 --- a/.gitignore +++ b/.gitignore @@ -38,4 +38,7 @@ next-env.d.ts /data # tests -/tests \ No newline at end of file +/tests +/test-results/ +/playwright-report/ +/playwright/.cache/ diff --git a/e2e/example.spec.ts b/e2e/example.spec.ts new file mode 100644 index 0000000..c511525 --- /dev/null +++ b/e2e/example.spec.ts @@ -0,0 +1,18 @@ +import { test, expect } from '@playwright/test'; + +test('has title', async ({ page }) => { + await page.goto('https://playwright.dev/'); + + // Expect a title "to contain" a substring. + await expect(page).toHaveTitle(/Playwright/); +}); + +test('get started link', async ({ page }) => { + await page.goto('https://playwright.dev/'); + + // Click the get started link. + await page.getByRole('link', { name: 'Get started' }).click(); + + // Expects the URL to contain intro. + await expect(page).toHaveURL(/.*intro/); +}); diff --git a/lib/api/archive.ts b/lib/api/archive.ts index 60050f7..7d5d0d5 100644 --- a/lib/api/archive.ts +++ b/lib/api/archive.ts @@ -1,8 +1,8 @@ -import { Page } from "puppeteer"; +import { Page, chromium, devices } from "playwright"; import { prisma } from "@/lib/api/db"; -import puppeteer from "puppeteer-extra"; -import AdblockerPlugin from "puppeteer-extra-plugin-adblocker"; -import StealthPlugin from "puppeteer-extra-plugin-stealth"; +// import puppeteer from "puppeteer-extra"; +// import AdblockerPlugin from "puppeteer-extra-plugin-adblocker"; +// import StealthPlugin from "puppeteer-extra-plugin-stealth"; import createFile from "@/lib/api/storage/createFile"; export default async function archive( @@ -10,16 +10,18 @@ export default async function archive( collectionId: number, linkId: number ) { - const browser = await puppeteer.launch(); + const browser = await chromium.launch(); + const context = await browser.newContext(devices["Desktop Chrome"]); + const page = await context.newPage(); try { - puppeteer.use(AdblockerPlugin()).use(StealthPlugin()); + // puppeteer.use(AdblockerPlugin()).use(StealthPlugin()); - const page = await browser.newPage(); + // const page = await browser.newPage(); await page.goto(url, { waitUntil: "domcontentloaded", timeout: 300000 }); - await page.setViewport({ width: 1080, height: 1024 }); + // await page.setViewport({ width: 1080, height: 1024 }); await autoScroll(page); @@ -51,10 +53,10 @@ export default async function archive( }); } - await browser.close(); + // await browser.close(); } catch (err) { console.log(err); - await browser.close(); + // await browser.close(); } } diff --git a/package.json b/package.json index 179cb2a..fdc74a6 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "next": "13.1.6", "next-auth": "^4.22.1", "nodemailer": "^6.9.3", + "playwright": "^1.35.1", "puppeteer": "^19.8.0", "puppeteer-extra": "^3.3.6", "puppeteer-extra-plugin-adblocker": "^2.13.6", @@ -49,6 +50,7 @@ "zustand": "^4.3.3" }, "devDependencies": { + "@playwright/test": "^1.35.1", "@types/bcrypt": "^5.0.0", "@types/multer": "^1.4.7", "autoprefixer": "^10.4.14", diff --git a/playwright.config.ts b/playwright.config.ts new file mode 100644 index 0000000..bfe3e83 --- /dev/null +++ b/playwright.config.ts @@ -0,0 +1,77 @@ +import { defineConfig, devices } from '@playwright/test'; + +/** + * Read environment variables from file. + * https://github.com/motdotla/dotenv + */ +// require('dotenv').config(); + +/** + * See https://playwright.dev/docs/test-configuration. + */ +export default defineConfig({ + testDir: './e2e', + /* Run tests in files in parallel */ + fullyParallel: true, + /* Fail the build on CI if you accidentally left test.only in the source code. */ + forbidOnly: !!process.env.CI, + /* Retry on CI only */ + retries: process.env.CI ? 2 : 0, + /* Opt out of parallel tests on CI. */ + workers: process.env.CI ? 1 : undefined, + /* Reporter to use. See https://playwright.dev/docs/test-reporters */ + reporter: 'html', + /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ + use: { + /* Base URL to use in actions like `await page.goto('/')`. */ + // baseURL: 'http://127.0.0.1:3000', + + /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ + trace: 'on-first-retry', + }, + + /* Configure projects for major browsers */ + projects: [ + { + name: 'chromium', + use: { ...devices['Desktop Chrome'] }, + }, + + { + name: 'firefox', + use: { ...devices['Desktop Firefox'] }, + }, + + { + name: 'webkit', + use: { ...devices['Desktop Safari'] }, + }, + + /* Test against mobile viewports. */ + // { + // name: 'Mobile Chrome', + // use: { ...devices['Pixel 5'] }, + // }, + // { + // name: 'Mobile Safari', + // use: { ...devices['iPhone 12'] }, + // }, + + /* Test against branded browsers. */ + // { + // name: 'Microsoft Edge', + // use: { ...devices['Desktop Edge'], channel: 'msedge' }, + // }, + // { + // name: 'Google Chrome', + // use: { ...devices['Desktop Chrome'], channel: 'chrome' }, + // }, + ], + + /* Run your local dev server before starting the tests */ + // webServer: { + // command: 'npm run start', + // url: 'http://127.0.0.1:3000', + // reuseExistingServer: !process.env.CI, + // }, +}); diff --git a/yarn.lock b/yarn.lock index 759d1d7..40c0956 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1062,6 +1062,16 @@ tiny-glob "^0.2.9" tslib "^2.4.0" +"@playwright/test@^1.35.1": + version "1.35.1" + resolved "https://registry.yarnpkg.com/@playwright/test/-/test-1.35.1.tgz#a596b61e15b980716696f149cc7a2002f003580c" + integrity sha512-b5YoFe6J9exsMYg0pQAobNDR85T1nLumUYgUTtKm4d21iX2L7WqKq9dW8NGJ+2vX0etZd+Y7UeuqsxDXm9+5ZA== + dependencies: + "@types/node" "*" + playwright-core "1.35.1" + optionalDependencies: + fsevents "2.3.2" + "@prisma/client@^4.9.0": version "4.9.0" resolved "https://registry.yarnpkg.com/@prisma/client/-/client-4.9.0.tgz#4a4068f3540732ea5723c008d49ed684d20f9340" @@ -2987,7 +2997,7 @@ fs.realpath@^1.0.0: resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw== -fsevents@~2.3.2: +fsevents@2.3.2, fsevents@~2.3.2: version "2.3.2" resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a" integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA== @@ -4260,6 +4270,18 @@ pirates@^4.0.1: resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.5.tgz#feec352ea5c3268fb23a37c702ab1699f35a5f3b" integrity sha512-8V9+HQPupnaXMA23c5hvl69zXvTwTzyAYasnkb0Tts4XvO4CliqONMOnvlq26rkhLC3nWDFBJf73LU1e1VZLaQ== +playwright-core@1.35.1: + version "1.35.1" + resolved "https://registry.yarnpkg.com/playwright-core/-/playwright-core-1.35.1.tgz#52c1e6ffaa6a8c29de1a5bdf8cce0ce290ffb81d" + integrity sha512-pNXb6CQ7OqmGDRspEjlxE49w+4YtR6a3X6mT1hZXeJHWmsEz7SunmvZeiG/+y1yyMZdHnnn73WKYdtV1er0Xyg== + +playwright@^1.35.1: + version "1.35.1" + resolved "https://registry.yarnpkg.com/playwright/-/playwright-1.35.1.tgz#f991d0c76ae517d4a0023d9428b09d19d5e87128" + integrity sha512-NbwBeGJLu5m7VGM0+xtlmLAH9VUfWwYOhUi/lSEDyGg46r1CA9RWlvoc5yywxR9AzQb0mOCm7bWtOXV7/w43ZA== + dependencies: + playwright-core "1.35.1" + pngjs@^3.3.3: version "3.4.0" resolved "https://registry.yarnpkg.com/pngjs/-/pngjs-3.4.0.tgz#99ca7d725965fb655814eaf65f38f12bbdbf555f"