Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

playwright integration #1825

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 yarn && yarn
- name: Install Playwright Browsers
run: yarn playwright install --with-deps
- name: Run Playwright tests
run: yarn playwright test
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ out

# Autogenerated static files
public/sitemap.xml
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
34 changes: 34 additions & 0 deletions e2e/baseFixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as fs from 'fs';
import * as path from 'path';

// eslint-disable-next-line import/no-extraneous-dependencies
import { test as baseTest } from '@playwright/test';

const istanbulCLIOutput = path.join(process.cwd(), 'coverage');

export const test = baseTest.extend({
context: async ({ context }, use) => {
await context.addInitScript(() =>
window.addEventListener('beforeunload', () =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-underscore-dangle
(window as any).collectIstanbulCoverage(JSON.stringify((window as any).__coverage__)),
),
);
await fs.promises.mkdir(istanbulCLIOutput, { recursive: true });
await context.exposeFunction('collectIstanbulCoverage', (coverageJSON: string) => {
if (coverageJSON)
fs.writeFileSync(path.join(istanbulCLIOutput, `playwright_coverage.json`), coverageJSON);
});
await use(context);
// eslint-disable-next-line no-restricted-syntax
for (const page of context.pages()) {
// eslint-disable-next-line no-await-in-loop
await page.evaluate(() =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-underscore-dangle
(window as any).collectIstanbulCoverage(JSON.stringify((window as any).__coverage__)),
);
}
},
});

export const { expect } = test;
18 changes: 18 additions & 0 deletions e2e/example.spec.ts
Original file line number Diff line number Diff line change
@@ -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 page to have a heading with the name of Installation.
await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible();
});
13 changes: 13 additions & 0 deletions e2e/homePageExampleTest.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { test, expect } from '@playwright/test';

test('HomePage Example Test', async ({ page }) => {
await page.goto('https://operationcode.org/');
await expect(page).toHaveTitle('Operation Code | Home');
});

test('learn more link', async ({ page }) => {
await page.goto('https://operationcode.org/');
await page.getByRole('link', { name: 'learn more' }).click();
await expect(page.getByRole('heading', { name: 'about us' })).toBeVisible();
});
12 changes: 11 additions & 1 deletion nyc.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
module.exports = {
all: true,
include: [
'pages/**/*.{js,ts,tsx}',
'common/**/*.{js,ts,tsx}',
'components/**/*.{js,ts,tsx}',
'decorators/**/*.{js,ts,tsx}',
],
exclude: ['pages/api/__coverage__.{js,ts}'],
exclude: [
'pages/api/__coverage__.{js,ts}',
'**/*.config.ts',
'**/*.config.js',
'**/*.d.ts',
'**/pages/api/**/*.*',
'__tests__',
'tests',
],
reporter: ['html'],
};
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
"test:changes:watch": "yarn test:changes --watch",
"test:watch": "yarn test --watch",
"test:update-snaps": "yarn test -u",
"test:ui": "vitest --ui"
"test:ui": "vitest --ui",
"test-e2e": "playwright test",
"create-report": "nyc report --reporter html --reporter text --reporter json -t coverage --report-dir coverage/summary",
"show-report": "yarn playwright show-report",
"coverage": "yarn test-e2e && yarn create-report"
},
"nyc": {
"extends": "./nyc.config.js",
Expand All @@ -43,6 +47,7 @@
"@innocuous/components": "^2.1.1",
"@innocuous/functions": "^2.1.1",
"@next/bundle-analyzer": "12.3.1",
"@playwright/test": "^1.44.1",
"@radix-ui/react-dialog": "1.0.0",
"@radix-ui/react-tabs": "0.1.1",
"@sentry/nextjs": "^7.77.0",
Expand All @@ -66,6 +71,7 @@
"next-cookies": "^2.0.3",
"next-sitemap": "^4.2.3",
"nuka-carousel": "^5.2.0",
"nyc": "^15.1.0",
"object-hash": "^3.0.0",
"path": "^0.12.7",
"prop-types": "^15.8.1",
Expand All @@ -81,6 +87,8 @@
"react-select": "^4.0.2",
"react-youtube": "9.0.3",
"tailwind-merge": "^2.1.0",
"ts-jest": "^29.1.4",
"ts-node": "^10.9.2",
"yup": "^0.32.11"
},
"devDependencies": {
Expand Down Expand Up @@ -108,6 +116,7 @@
"@types/jest": "^29.5.12",
"@types/lodash": "^4.14.202",
"@types/logrocket-react": "^3.0.3",
"@types/node": "^20.14.2",
"@types/object-hash": "^3.0.0",
"@types/prop-types": "^15.7.11",
"@types/react": "^18.2.55",
Expand Down
78 changes: 78 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// eslint-disable-next-line import/no-extraneous-dependencies
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,
// },
});
Loading