Skip to content

Commit

Permalink
WIP: Add Playwright end-to-end tests
Browse files Browse the repository at this point in the history
  • Loading branch information
msmolens committed Jan 2, 2024
1 parent dafd31b commit ca9cbc3
Show file tree
Hide file tree
Showing 9 changed files with 774 additions and 8 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Playwright Tests
on:
push:
branches:
- main
- playwright
pull_request:
branches:
- main
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 7
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
/src/treetop/generated/*.css
/web-ext-artifacts/
/.vscode/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
35 changes: 35 additions & 0 deletions e2e/bookmarks.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { test, expect } from './fixtures';

test.beforeEach(async ({ page, extensionId }, _testInfo) => {
await page.goto(`chrome-extension://${extensionId}/treetop.html`);

await page.evaluate(async () => {
await chrome.bookmarks.create({
parentId: '1',
url: 'https://github.com',
title: 'GitHub',
});
await chrome.bookmarks.create({
parentId: '2',
url: 'https://gitlab.com',
title: 'GitLab',
});
});
});

test('existing bookmarks', async ({ page }) => {
const rootFolder = page.locator('.folder').first();
const bookmarksBarContents = rootFolder.locator('.folder > .contents').nth(0);
await expect(bookmarksBarContents).toHaveCount(1);
expect(
bookmarksBarContents.getByRole('link', { name: 'GitHub' }),
).toBeVisible();

const otherBookmarksContents = rootFolder
.locator('.folder > .contents')
.nth(1);
await expect(otherBookmarksContents).toHaveCount(1);
expect(
otherBookmarksContents.getByRole('link', { name: 'GitLab' }),
).toBeVisible();
});
35 changes: 35 additions & 0 deletions e2e/default.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { test, expect } from './fixtures';

test.beforeEach(async ({ page, extensionId }, _testInfo) => {
await page.goto(`chrome-extension://${extensionId}/treetop.html`);
});

test('page header', async ({ page }) => {
const banner = page.getByRole('banner');
await expect(banner.locator('.treetop')).toHaveText('Treetop');
await expect(banner.getByRole('textbox')).toBeVisible();
await expect(banner.getByRole('textbox')).toHaveText('');
await expect(banner.getByRole('button')).toHaveText(/settings/i);
});

test('default bookmarks', async ({ page }) => {
// Root folder
const rootFolder = page.locator('.folder').first();
await expect(rootFolder.locator('.heading').first()).toHaveText('Bookmarks');

// Bookmarks bar
const bookmarksBar = rootFolder.locator('.folder > .heading').nth(0);
const bookmarksBarContents = rootFolder.locator('.folder > .contents').nth(0);

await expect(bookmarksBar).toHaveText('Bookmarks bar');
await expect(bookmarksBarContents).toHaveText('Empty folder');

// Other bookmarks
const otherBookmarks = rootFolder.locator('.folder > .heading').nth(1);
const otherBookmarksContents = rootFolder
.locator('.folder > .contents')
.nth(1);

await expect(otherBookmarks).toHaveText('Other bookmarks');
await expect(otherBookmarksContents).toHaveText('Empty folder');
});
32 changes: 32 additions & 0 deletions e2e/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { test as base, chromium, type BrowserContext } from '@playwright/test';
import path from 'path';

export const test = base.extend<{
context: BrowserContext;
extensionId: string;
}>({
context: async ({}, use) => {
const pathToExtension = './dist/chrome';
const context = await chromium.launchPersistentContext('', {
headless: false,
args: [
// `--headless=new`,
`--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;
129 changes: 121 additions & 8 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@fontsource/inter": "~5.0.16",
"@fontsource/material-icons": "~5.0.11",
"@fontsource/news-cycle": "~5.0.18",
"@playwright/test": "~1.40.1",
"@smui/button": "~7.0.0-beta.16",
"@smui/dialog": "~7.0.0-beta.16",
"@smui/icon-button": "~7.0.0-beta.16",
Expand All @@ -47,6 +48,7 @@
"@tsconfig/svelte": "~5.0.2",
"@types/faker": "5.5.9",
"@types/lodash-es": "~4.17.12",
"@types/node": "~20.10.5",
"@typescript-eslint/eslint-plugin": "~6.16.0",
"@typescript-eslint/parser": "~6.16.0",
"chrome-types": "~0.1.246",
Expand Down
Loading

0 comments on commit ca9cbc3

Please sign in to comment.