generated from fastify/skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add e2e tests with playwright (#99)
* feat: add e2e tests with playwright * test: enhance and define port in one place * test: adapt playwright workflow * Update .github/workflows/playwright.yml Co-authored-by: Manuel Spigolon <[email protected]> * Update .github/workflows/playwright.yml Co-authored-by: Manuel Spigolon <[email protected]> * test: run playwright tests in ci workflow * test: add playwright job permissions --------- Co-authored-by: Manuel Spigolon <[email protected]>
- Loading branch information
1 parent
1bb92b1
commit aee66c4
Showing
8 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Playwright Tests | ||
|
||
on: | ||
workflow_dispatch: | ||
workflow_call: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
|
||
timeout-minutes: 60 | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: lts/* | ||
- name: Install dependencies | ||
run: npm i | ||
- name: Install Playwright Browsers | ||
run: npx playwright@1 install chromium --with-deps | ||
- name: Run Playwright tests | ||
run: npx playwright@1 test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,3 +150,6 @@ yarn.lock | |
# editor files | ||
.vscode | ||
.idea | ||
/test-results/ | ||
/playwright-report/ | ||
/playwright/.cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict' | ||
|
||
const { test, expect } = require('@playwright/test') | ||
|
||
const URL_DOCUMENTATION = '/documentation' | ||
const URL_FAVICON = '/documentation/static/theme/favicon.svg' | ||
|
||
test.describe('Check customizations', () => { | ||
test('Check JS injection', async ({ page }) => { | ||
await page.goto(URL_DOCUMENTATION) | ||
await page.waitForLoadState('networkidle') | ||
|
||
page.on('dialog', async dialog => { | ||
expect(dialog.type() === 'beforeunload').toBeTruthy() | ||
expect(dialog.message() === 'unloaded test-theme').toBeTruthy() | ||
await dialog.dismiss() | ||
}) | ||
await page.close({ runBeforeUnload: true }) | ||
}) | ||
|
||
test('Check CSS injection', async ({ page }) => { | ||
await page.goto(URL_DOCUMENTATION) | ||
await page.waitForLoadState('networkidle') | ||
|
||
const element = await page.waitForSelector('button.download-url-button') | ||
const color = await element.evaluate(el => window.getComputedStyle(el).getPropertyValue('background-color')) | ||
expect(color).toBe('rgb(255, 0, 0)') | ||
}) | ||
|
||
test('Check custom favicon', async ({ page }) => { | ||
await page.goto(URL_FAVICON) | ||
|
||
const faviconId = await (await page.waitForSelector('svg')).getAttribute('id') | ||
expect(faviconId).toBe('example-logo') // it is included in the svg file | ||
}) | ||
|
||
test('Check custom logo', async ({ page }) => { | ||
await page.goto(URL_DOCUMENTATION) | ||
await page.waitForLoadState('networkidle') | ||
|
||
const logoSrc = await page.locator('img').first().getAttribute('src') | ||
await page.goto(logoSrc) | ||
|
||
const logoId = await (await page.waitForSelector('svg')).getAttribute('id') | ||
expect(logoId).toBe('example-logo') // it is included in the svg file | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const fastify = require('fastify')({ logger: true }) | ||
const readFileSync = require('node:fs').readFileSync | ||
const resolve = require('node:path').resolve | ||
|
||
const exampleLogo = readFileSync( | ||
resolve(__dirname, '..', 'examples/static', 'example-logo.svg'), | ||
'utf8' | ||
) | ||
|
||
fastify.register(require('@fastify/swagger'), { | ||
mode: 'static', | ||
specification: { | ||
path: './examples/example-static-specification.json' | ||
} | ||
}) | ||
|
||
fastify.register(require('../index'), { | ||
theme: { | ||
js: [ | ||
{ filename: 'unloaded.js', content: 'window.onbeforeunload = function(){alert("unloaded test-theme")}' } | ||
], | ||
css: [ | ||
{ filename: 'theme.css', content: '.download-url-button {background: red !important;}' } | ||
], | ||
favicon: [ | ||
{ | ||
filename: 'favicon.svg', | ||
rel: 'icon', | ||
sizes: '16x16', | ||
type: 'image/svg+xml', | ||
content: exampleLogo | ||
} | ||
] | ||
}, | ||
logo: { | ||
type: 'image/svg+xml', | ||
content: exampleLogo | ||
} | ||
}) | ||
|
||
fastify.listen({ port: process.env.PORT }, (err) => { | ||
if (err) throw err | ||
fastify.log.info(`visit the documentation at http://127.0.0.1:${process.env.PORT}/documentation/`) | ||
}) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
const { defineConfig, devices } = require('@playwright/test') | ||
|
||
const PORT = 3000 | ||
|
||
/** | ||
* @see https://playwright.dev/docs/test-configuration | ||
*/ | ||
module.exports = defineConfig({ | ||
testDir: './e2e', | ||
fullyParallel: true, | ||
forbidOnly: !!process.env.CI, | ||
retries: process.env.CI ? 2 : 0, | ||
workers: process.env.CI ? 1 : undefined, | ||
reporter: 'html', | ||
use: { | ||
baseURL: `http://127.0.0.1:${PORT}/documentation`, | ||
trace: 'on-first-retry' | ||
}, | ||
projects: [ | ||
{ | ||
name: 'chromium', | ||
use: { ...devices['Desktop Chrome'] } | ||
} | ||
], | ||
webServer: { | ||
command: `PORT=${PORT} npm run test:e2e:command`, | ||
url: `http://127.0.0.1:${PORT}/documentation`, | ||
reuseExistingServer: !process.env.CI | ||
} | ||
}) |