Skip to content

Commit 7e42a52

Browse files
committed
add playwright and port one test
1 parent b311e55 commit 7e42a52

8 files changed

Lines changed: 163 additions & 80 deletions

File tree

.github/workflows/playwright.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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-latest
11+
steps:
12+
- uses: actions/checkout@v5
13+
- uses: actions/setup-node@v6
14+
with:
15+
node-version-file: ".nvmrc"
16+
cache: "yarn"
17+
- name: Install dependencies
18+
run: yarn install --frozen-lockfile
19+
- name: Install Playwright Browsers
20+
run: yarn playwright install --with-deps
21+
- name: Run Playwright tests
22+
run: yarn playwright test
23+
- uses: actions/upload-artifact@v4
24+
if: always()
25+
with:
26+
name: playwright-report
27+
path: playwright-report/
28+
retention-days: 30

components/Heading/Heading.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,16 @@ function Heading({
5151
},
5252
className,
5353
)}
54+
id={anchorId}
5455
>
5556
{hasHashLink ? (
5657
<div className="relative" data-testid={`Heading Content ${anchorId}`}>
5758
<a
58-
id={anchorId}
5959
href={`#${anchorId}`}
6060
data-testid="Hash Link"
6161
className={cx(
62-
'hidden absolute top-2 -left-8',
63-
'transition-all duration-200 ease-linear',
64-
'sm:inline sm:opacity-0 sm:group-hover:opacity-100',
62+
'absolute top-2 -left-8',
63+
'invisible group-hover:visible focus-visible:visible',
6564
)}
6665
>
6766
<ScreenReaderOnly>Scroll Link for {text}</ScreenReaderOnly>

cypress/e2e/hashlink.spec.js

Lines changed: 0 additions & 73 deletions
This file was deleted.

e2e/hashlink.spec.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import type { Page } from '@playwright/test';
2+
import { test, expect } from '@playwright/test';
3+
4+
const HERO_BANNER_H1 = 'HERO_BANNER_H1';
5+
6+
const someRandomPagesWithHashLinks = [
7+
{ title: 'Home', path: '/' },
8+
{ title: 'About', path: '/about' },
9+
{ title: 'Corporate Sponsorship', path: '/sponsorship' },
10+
];
11+
12+
const makeHashLinkVisible = async ({ page, hashId }: { page: Page; hashId: string }) => {
13+
// Locate the heading element relevant to the hashlink
14+
const heading = page.locator(`#${hashId}`);
15+
16+
// Scroll heading into view
17+
await heading.scrollIntoViewIfNeeded();
18+
19+
// Hover over the heading to make the hash link visible
20+
await heading.hover();
21+
};
22+
23+
test.describe('Hash Links', () => {
24+
for (const { title, path } of someRandomPagesWithHashLinks) {
25+
test(`on ${title} page, will be invisible until hovered and change route when clicked`, async ({
26+
page,
27+
browserName,
28+
}) => {
29+
await page.goto(path);
30+
31+
// Wait for navigation to complete
32+
await expect(
33+
page.getByTestId(browserName === 'chromium' ? 'Desktop Nav' : 'Mobile Nav Container'),
34+
).toBeVisible();
35+
await expect(page).toHaveURL(new RegExp(path));
36+
37+
// Verify hero banner is visible
38+
await expect(page.getByTestId(HERO_BANNER_H1).first()).toBeVisible();
39+
40+
// Get all hash links on the page
41+
const hashLinks = await page.getByTestId('Hash Link').all();
42+
43+
for (const link of hashLinks) {
44+
const hash = await link.getAttribute('href');
45+
46+
// This is to satiate TS
47+
// eslint-disable-next-line playwright/no-conditional-in-test
48+
if (!hash) continue;
49+
50+
const hashId = hash.replace(/^.*#/, '');
51+
52+
await expect(link).toBeHidden();
53+
54+
await makeHashLinkVisible({ page, hashId });
55+
56+
await expect(link).toBeVisible();
57+
58+
// On mobile safari, clicking elements positioned outside viewport can fail
59+
// Use evaluate to navigate directly by clicking via JavaScript
60+
await link.evaluate((el: HTMLAnchorElement) => el.click());
61+
62+
// Verify URL includes the hash
63+
await expect(page).toHaveURL(new RegExp(`${path}#${hashId}`));
64+
}
65+
});
66+
}
67+
});

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"start": "next start -p 3000",
2020
"storybook": "storybook dev -p 9001 -c .storybook",
2121
"storybook:build": "storybook build -c .storybook -o .storybook-dist",
22-
"test:e2e": "playwright test",
23-
"test:e2e:headless": "playwright test --headed",
22+
"test:e2e": "playwright test --headed",
23+
"test:e2e:headless": "playwright test",
2424
"test:e2e:update-snaps": "playwright test --update-snapshots",
2525
"test:e2e:ci": "playwright test --ci",
2626
"test": "vitest --run",
@@ -134,6 +134,7 @@
134134
"chromatic": "^6.8.0",
135135
"cross-env": "^7.0.3",
136136
"css-loader": "^6.7.1",
137+
"dotenv": "^17.2.3",
137138
"eslint": "^8.56.0",
138139
"eslint-config-prettier": "^9.1.0",
139140
"eslint-plugin-import": "^2.32.0",

pages/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function Home() {
4141
<Content
4242
theme="gray"
4343
columns={[
44-
<div className="flex flex-col">
44+
<div className="flex flex-col" key="our-mission">
4545
<Heading text="Our Mission" hasTitleUnderline />
4646
<p>
4747
We serve our veterans, service members, and their families, work alongside their

playwright.config.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import path from 'path';
2+
import { defineConfig, devices } from '@playwright/test';
3+
import dotenv from 'dotenv';
4+
5+
dotenv.config({ path: path.resolve(__dirname, '.env') });
6+
7+
/**
8+
* See https://playwright.dev/docs/test-configuration.
9+
*/
10+
export default defineConfig({
11+
testDir: './e2e',
12+
13+
/* Run tests in files in parallel */
14+
fullyParallel: true,
15+
16+
/* Fail the build on CI if you accidentally left test.only in the source code. */
17+
forbidOnly: !!process.env.CI,
18+
19+
/* Retry on CI only */
20+
retries: process.env.CI ? 2 : 0,
21+
22+
/* Opt out of parallel tests on CI. */
23+
// workers: process.env.CI ? 1 : undefined,
24+
25+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
26+
reporter: process.env.CI ? 'github' : 'html',
27+
28+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
29+
use: {
30+
/* Base URL to use in actions like `await page.goto('')`. */
31+
baseURL: 'http://localhost:3000',
32+
33+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
34+
trace: 'on-first-retry',
35+
},
36+
37+
/* Configure projects for major browsers */
38+
projects: [
39+
{
40+
name: 'chromium',
41+
use: { ...devices['Desktop Chrome'] },
42+
},
43+
{
44+
name: 'Mobile Safari',
45+
use: { ...devices['iPhone 14 Pro'] },
46+
},
47+
],
48+
49+
/* Run your local dev server before starting the tests */
50+
webServer: {
51+
command: 'yarn dev',
52+
url: 'http://localhost:3000',
53+
reuseExistingServer: !process.env.CI,
54+
timeout: 60_000,
55+
},
56+
});

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7084,6 +7084,11 @@ dotenv@^16.0.0:
70847084
resolved "https://registry.npmjs.org/dotenv/-/dotenv-16.3.0.tgz"
70857085
integrity sha512-tHB+hmf8MRCkT3VVivGiG8kq9HiGTmQ3FzOKgztfpJQH1IWuZTOvKSJmHNnQPowecAmkCJhLrxdPhOr06LLqIQ==
70867086

7087+
dotenv@^17.2.3:
7088+
version "17.2.3"
7089+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-17.2.3.tgz#ad995d6997f639b11065f419a22fabf567cdb9a2"
7090+
integrity sha512-JVUnt+DUIzu87TABbhPmNfVdBDt18BLOWjMUFJMSi/Qqg7NTYtabbvSNJGOJ7afbRuv9D/lngizHtP7QyLQ+9w==
7091+
70877092
dunder-proto@^1.0.0, dunder-proto@^1.0.1:
70887093
version "1.0.1"
70897094
resolved "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz"

0 commit comments

Comments
 (0)