Skip to content

Commit

Permalink
test: add e2e tests
Browse files Browse the repository at this point in the history
- Add and config playwright
- Add fixtures to use chromium with the extension
- Add simple test to check popup and content script on github files changed
- Add github workflow
  • Loading branch information
tiagofilipenunes committed Sep 29, 2024
1 parent 00d8eef commit c317faa
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 3 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Playwright Tests
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- name: Install dependencies
run: npm install -g pnpm && pnpm install
- name: Install Playwright Browsers
run: pnpm exec playwright install --with-deps
- name: Run Playwright E2E tests
run: pnpm e2e
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-report
path: playwright-report/
retention-days: 30
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ auto-imports.d.ts

# Produced when building or packing
manifest.json
web-ext-artifacts
web-ext-artifacts

# Test
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
24 changes: 24 additions & 0 deletions e2e/diff.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { test, expect } from "./fixtures";

test("Goto github PR files changed and check diff", async ({ page }) => {
await page.goto(
"https://github.com/bancorprotocol/carbon-app/pull/1464/files"
);
const firstIframe = page.frameLocator("iframe").first();
const iframeBody = firstIframe.locator("body");

await expect(iframeBody).toContainText("Difference");
await expect(iframeBody).toContainText("Overlay");
});

test("Check settings page contains right header and labels", async ({
page,
extensionId,
}) => {
await page.goto(`chrome-extension://${extensionId}/src/popup/index.html`);
await expect(page.locator("h1")).toContainText("Settings");
expect(page.locator("form > label")).toHaveText([
"Diff Color",
"Default Algo",
]);
});
34 changes: 34 additions & 0 deletions e2e/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { test as base, chromium, type BrowserContext } from "@playwright/test";
import path from "path";
import { fileURLToPath } from "url";
import { resolve } from "node:path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const rootPath = resolve(__dirname, "..");

export const test = base.extend<{
context: BrowserContext;
extensionId: string;
}>({
context: async ({}, use) => {
const pathToExtension = path.join(rootPath, "dist");
const context = await chromium.launchPersistentContext("", {
headless: false,
args: [
`--disable-extensions-except=${pathToExtension}`,
`--load-extension=${pathToExtension}`,
],
});
await use(context);
await context.close();
},
extensionId: async ({ context }, use) => {
let [background] = context.serviceWorkers();
if (!background) background = await context.waitForEvent("serviceworker");

const extensionId = background.url().split("/")[2];
await use(extensionId);
},
});
export const expect = test.expect;
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
"dev:chromium": "concurrently \"pnpm build:chromium -- -- --watch\" \"pnpm start:chromium\"",
"pack:firefox": "web-ext build -s dist --overwrite-dest",
"lint": "web-ext lint -s dist",
"clear": "rimraf --glob dist/src dist/manifest.json dist.*"
"clear": "rimraf --glob dist/src dist/manifest.json dist.*",
"e2e": "pnpm build:chromium && pnpm playwright test"
},
"devDependencies": {
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@playwright/test": "^1.47.2",
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.12.7",
"@types/pixelmatch": "^5.2.6",
Expand Down
52 changes: 52 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @see {@link https://playwright.dev/docs/chrome-extensions Chrome extensions | Playwright}
*/

import { defineConfig, devices } from "@playwright/test";

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// import path from 'path';
// dotenv.config({ path: path.resolve(__dirname, '.env') });

/**
* 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"] },
},
],
// webServer: {
// command: "pnpm dev",
// // start e2e test after the Vite server is fully prepared
// url: "http://localhost:3303/popup/main.ts",
// reuseExistingServer: true,
// },
});
38 changes: 38 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
"include": ["src", "e2e"]
}

0 comments on commit c317faa

Please sign in to comment.