Add Playwright support for Task List
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/stevekinney/testing-javascript#readme",
|
||||
"devDependencies": {
|
||||
"@playwright/test": "^1.47.2",
|
||||
"@testing-library/dom": "^10.4.0",
|
||||
"@testing-library/jest-dom": "^6.5.0",
|
||||
"@testing-library/react": "^16.0.1",
|
||||
@@ -39,7 +40,8 @@
|
||||
"tailwind-merge": "^2.5.2",
|
||||
"uuid": "^10.0.0",
|
||||
"vite": "^5.4.6",
|
||||
"vitest": "^2.1.1"
|
||||
"vitest": "^2.1.1",
|
||||
"wait-port": "^1.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"body-parser": "^1.20.3",
|
||||
|
||||
78
examples/task-list/playwright.config.js
Normal file
78
examples/task-list/playwright.config.js
Normal file
@@ -0,0 +1,78 @@
|
||||
// @ts-check
|
||||
import { defineConfig, devices } from '@playwright/test';
|
||||
|
||||
/**
|
||||
* Read environment variables from file.
|
||||
* https://github.com/motdotla/dotenv
|
||||
*/
|
||||
// require('dotenv').config({ path: path.resolve(__dirname, '.env') });
|
||||
|
||||
/**
|
||||
* @see https://playwright.dev/docs/test-configuration
|
||||
*/
|
||||
export default defineConfig({
|
||||
testDir: './tests',
|
||||
/* 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,
|
||||
// },
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import * as url from 'node:url';
|
||||
import express from 'express';
|
||||
import bodyParser from 'body-parser';
|
||||
import chalk from 'chalk';
|
||||
@@ -59,7 +60,16 @@ app.delete('/api/tasks/:id', (req, res) => {
|
||||
res.sendStatus(204); // No content to send back
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
app.listen(PORT, () => {
|
||||
console.log(chalk.magenta(`Server is running on port ${chalk.green(PORT)}…`));
|
||||
});
|
||||
if (import.meta.url.startsWith('file:')) {
|
||||
const modulePath = url.fileURLToPath(import.meta.url);
|
||||
if (process.argv[1] === modulePath) {
|
||||
const PORT = process.env.PORT || 3000;
|
||||
app.listen(PORT, () => {
|
||||
console.log(
|
||||
chalk.magenta(`Server is running on port ${chalk.green(PORT)}…`),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default app;
|
||||
|
||||
22
examples/task-list/tests/task-list.spec.js
Normal file
22
examples/task-list/tests/task-list.spec.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
|
||||
/** @type {import('../start-server').DevelopmentServer} */
|
||||
test.beforeEach(async ({ page }) => {
|
||||
await page.goto('http://localhost:5173');
|
||||
});
|
||||
|
||||
test('it should load the page', async ({ page }) => {
|
||||
await expect(page).toHaveTitle('Task List');
|
||||
});
|
||||
|
||||
test('it should add a task', async ({ page }) => {
|
||||
const input = page.getByLabel('Create Task');
|
||||
const submit = page.getByRole('button', { name: 'Create Task' });
|
||||
|
||||
await input.fill('Learn Playwright');
|
||||
await submit.click();
|
||||
|
||||
const heading = await page.getByRole('heading', { name: 'Learn Playwright' });
|
||||
|
||||
await expect(heading).toBeVisible();
|
||||
});
|
||||
Reference in New Issue
Block a user