1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
import { expect, test, type Page } from "@playwright/test";
import { layoutPages } from "./pages";
/** The widest screen that shows the header menu button instead of the links. */
const menuWidth = 980;
async function open(page: Page, target: string) {
const response = await page.goto(`http://${target}`);
expect(response?.status(), `status of ${target}`).toBeLessThan(400);
}
/**
* Elements that reach past either edge of the viewport, outside any box that
* scrolls or clips them. One such element is enough for a phone to scroll
* sideways or to cut text off.
*/
function elementsPastTheEdges(page: Page) {
return page.evaluate(() => {
const width = document.documentElement.clientWidth;
const clipped = (element: Element) => {
for (
let p = element.parentElement;
p && p !== document.body;
p = p.parentElement
) {
if (getComputedStyle(p).overflowX !== "visible") return true;
}
return false;
};
const found: string[] = [];
for (const element of document.body.querySelectorAll("*")) {
const box = element.getBoundingClientRect();
if (box.width === 0 || box.height === 0) continue;
if (box.right <= width + 1 && box.left >= -1) continue;
if (clipped(element)) continue;
found.push(
`${element.tagName.toLowerCase()}.${String(element.className)} ${Math.round(box.left)}..${Math.round(box.right)}`,
);
}
return found;
});
}
/**
* Controls smaller than 24 by 24 CSS pixels, the minimum target size of WCAG
* 2.2 (2.5.8). Links inside running text are exempt, as WCAG exempts them.
*/
function smallControls(page: Page) {
return page.evaluate(() => {
const controls = document.querySelectorAll(
"button, select, input:not([type=hidden]), .button, header a, .primary-nav a",
);
const found: string[] = [];
for (const control of controls) {
const box = control.getBoundingClientRect();
if (box.width === 0 || box.height === 0) continue;
if (box.width < 24 || box.height < 24) {
found.push(
`${control.tagName.toLowerCase()} "${control.textContent?.trim().slice(0, 24)}" ${Math.round(box.width)}x${Math.round(box.height)}`,
);
}
}
return found;
});
}
for (const target of layoutPages) {
test(`${target} fits the screen`, async ({ page }) => {
await open(page, target);
const overflow = await page.evaluate(
() =>
document.documentElement.scrollWidth -
document.documentElement.clientWidth,
);
expect(overflow, "horizontal scroll").toBe(0);
expect(await elementsPastTheEdges(page)).toEqual([]);
expect(await smallControls(page)).toEqual([]);
});
test(`${target} keeps its header on one line`, async ({ page }) => {
await open(page, target);
const header = page.locator("header.site-header").first();
test.skip((await header.count()) === 0, "the page has no site header");
const box = await header.boundingBox();
expect(box?.height ?? 0).toBeLessThanOrEqual(80);
// The brand and the controls beside it must not cover each other.
const overlaps = await header.evaluate((element) => {
// The words of the brand are measured themselves: a squeezed brand box
// can be narrower than the text that spills out of it.
const parts = [
...element.querySelectorAll(
".brand-name, .brand-product, .viget-brand-lockup > span, .menu-toggle, .primary-nav > *",
),
]
.map((part) => ({ part, box: part.getBoundingClientRect() }))
.filter(({ box }) => box.width > 0 && box.height > 0);
const found: string[] = [];
for (const [i, a] of parts.entries()) {
for (const b of parts.slice(i + 1)) {
if (a.part.contains(b.part) || b.part.contains(a.part)) continue;
const x =
Math.min(a.box.right, b.box.right) -
Math.max(a.box.left, b.box.left);
const y =
Math.min(a.box.bottom, b.box.bottom) -
Math.max(a.box.top, b.box.top);
if (x > 1 && y > 1)
found.push(`${a.part.className} / ${b.part.className}`);
}
}
return found;
});
expect(overlaps).toEqual([]);
});
}
test("the header menu opens and closes on a phone", async ({ page }) => {
const width = page.viewportSize()?.width ?? 0;
test.skip(width > menuWidth, "wide screens show the links in the header");
await open(page, "progmasoft.com/");
const toggle = page.locator(".menu-toggle");
const links = page.locator(".primary-nav a").first();
await expect(toggle).toBeVisible();
await expect(links).toBeHidden();
await toggle.click();
await expect(toggle).toHaveAttribute("aria-expanded", "true");
await expect(links).toBeVisible();
expect(await elementsPastTheEdges(page)).toEqual([]);
await page.keyboard.press("Escape");
await expect(toggle).toHaveAttribute("aria-expanded", "false");
await expect(links).toBeHidden();
});
test("wide screens show the links without a menu button", async ({ page }) => {
const width = page.viewportSize()?.width ?? 0;
test.skip(width <= menuWidth, "phones and tablets use the menu");
await open(page, "progmasoft.com/");
await expect(page.locator(".menu-toggle")).toBeHidden();
await expect(page.locator(".primary-nav a").first()).toBeVisible();
});
|