Skip to content

Commit 9561a4b

Browse files
committed
implement 11.9 Simple end-to-end tests
1 parent 6b267bd commit 9561a4b

File tree

6 files changed

+215
-7
lines changed

6 files changed

+215
-7
lines changed

.github/workflows/playwright.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Playwright Tests
2+
on:
3+
push:
4+
branches: [ main, master ]
5+
pull_request:
6+
branches: [ main, master ]
7+
jobs:
8+
test:
9+
timeout-minutes: 60
10+
runs-on: ubuntu-20.04
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-node@v4
14+
with:
15+
node-version: '20'
16+
- name: Install dependencies
17+
run: npm ci
18+
- name: Install Playwright Browsers
19+
run: npx playwright install --with-deps
20+
- name: Run Playwright tests
21+
run: npx playwright test
22+
- uses: actions/upload-artifact@v4
23+
if: always()
24+
with:
25+
name: playwright-report
26+
path: playwright-report/
27+
retention-days: 30

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
dist/
2-
node_modules/
2+
node_modules/
3+
/test-results/
4+
/playwright-report/
5+
/blob-report/
6+
/playwright/.cache/

e2e-tests/pokemon-page.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { test, describe, expect } = require('@playwright/test')
2+
3+
describe('Pokedex', () => {
4+
test('front page can be opened', async ({ page }) => {
5+
await page.goto('')
6+
await expect(page.getByText('ivysaur')).toBeVisible()
7+
await expect(page.getByText('Pokémon and Pokémon character names are trademarks of Nintendo.')).toBeVisible()
8+
})
9+
10+
test('pokemon page can be navigated to', async ({ page }) => {
11+
await page.goto('')
12+
const ivysaur = page.getByText('ivysaur')
13+
await expect(ivysaur).toBeVisible()
14+
await ivysaur.click()
15+
await expect(page.getByText('chlorophyll')).toBeVisible()
16+
})
17+
})

package-lock.json

+74-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"start-prod": "node app.js",
99
"test": "jest",
1010
"eslint": "eslint './**/*.{js,jsx}'",
11-
"build": "webpack --mode production"
11+
"build": "webpack --mode production",
12+
"test:e2e": "playwright test"
1213
},
1314
"repository": {
1415
"type": "git",
@@ -28,8 +29,10 @@
2829
"@babel/plugin-transform-runtime": "^7.23.7",
2930
"@babel/preset-env": "^7.23.7",
3031
"@babel/preset-react": "^7.23.3",
32+
"@playwright/test": "^1.47.0",
3133
"@testing-library/jest-dom": "^6.2.0",
3234
"@testing-library/react": "^14.1.2",
35+
"@types/node": "^22.5.4",
3336
"babel-jest": "^29.7.0",
3437
"babel-loader": "^9.1.3",
3538
"css-loader": "^6.8.1",
@@ -54,6 +57,9 @@
5457
"react-router-dom": "^6.21.1"
5558
},
5659
"jest": {
57-
"testEnvironment": "jsdom"
60+
"testEnvironment": "jsdom",
61+
"testPathIgnorePatterns": [
62+
"e2e-tests"
63+
]
5864
}
5965
}

playwright.config.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// @ts-check
2+
const { defineConfig, devices } = require('@playwright/test')
3+
4+
/**
5+
* Read environment variables from file.
6+
* https://github.com/motdotla/dotenv
7+
*/
8+
// require('dotenv').config({ path: path.resolve(__dirname, '.env') });
9+
10+
/**
11+
* @see https://playwright.dev/docs/test-configuration
12+
*/
13+
module.exports = defineConfig({
14+
testDir: './e2e-tests',
15+
/* Run tests in files in parallel */
16+
fullyParallel: false,
17+
/* Fail the build on CI if you accidentally left test.only in the source code. */
18+
forbidOnly: !!process.env.CI,
19+
/* Retry on CI only */
20+
retries: process.env.CI ? 2 : 0,
21+
/* Opt out of parallel tests on CI. */
22+
workers: process.env.CI ? 1 : undefined,
23+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
24+
reporter: 'html',
25+
webServer: {
26+
command: 'npm run start',
27+
url: 'http://localhost:8080',
28+
timeout: 120 * 1000,
29+
reuseExistingServer: !process.env.CI,
30+
},
31+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
32+
use: {
33+
/* Base URL to use in actions like `await page.goto('/')`. */
34+
baseURL: 'http://localhost:8080',
35+
36+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
37+
trace: 'on-first-retry',
38+
},
39+
40+
/* Configure projects for major browsers */
41+
projects: [
42+
{
43+
name: 'chromium',
44+
use: { ...devices['Desktop Chrome'] },
45+
},
46+
47+
{
48+
name: 'firefox',
49+
use: { ...devices['Desktop Firefox'] },
50+
},
51+
52+
{
53+
name: 'webkit',
54+
use: { ...devices['Desktop Safari'] },
55+
},
56+
57+
/* Test against mobile viewports. */
58+
// {
59+
// name: 'Mobile Chrome',
60+
// use: { ...devices['Pixel 5'] },
61+
// },
62+
// {
63+
// name: 'Mobile Safari',
64+
// use: { ...devices['iPhone 12'] },
65+
// },
66+
67+
/* Test against branded browsers. */
68+
// {
69+
// name: 'Microsoft Edge',
70+
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
71+
// },
72+
// {
73+
// name: 'Google Chrome',
74+
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
75+
// },
76+
],
77+
78+
/* Run your local dev server before starting the tests */
79+
// webServer: {
80+
// command: 'npm run start',
81+
// url: 'http://127.0.0.1:3000',
82+
// reuseExistingServer: !process.env.CI,
83+
// },
84+
})

0 commit comments

Comments
 (0)