Repository navigation
Expand file tree
/
Copy pathmeter.mjs
More file actions
185 lines (167 loc) · 7.78 KB
/
Copy pathmeter.mjs
File metadata and controls
185 lines (167 loc) · 7.78 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Counts what a person does in the browser: clicks, keystrokes and passkey prompts, attributed to the page they're on.
//
// The rules, so runs compare:
// - click: one mouse click. Choosing from a native <select> is two (open it, pick); a file chooser is two (the button,
// then picking the file in the system dialog). On a phone a click is a tap, counted the same way.
// - keys: every character typed, plus each key or shortcut pressed (Enter, Tab, Ctrl+K count one each). Pasting counts
// one (the shortcut). Typing into a field that doesn't already have the focus adds the click that focuses it.
// - prompts: a passkey prompt (a touch or a PIN on the device), counted apart from clicks.
// - swipes (phone only): a control off the screen, down the page or along a strip that scrolls sideways, takes a swipe
// to bring into view before the tap. On a computer scrolling isn't counted.
/** A page's address with the parts that differ per run (ids, codes, query values) taken out. */
export function pageKey(url) {
if (!/^https?:/.test(url)) return "(outside the browser)";
const u = new URL(url);
const port = u.port === "3990" ? "installer" : "";
const path = u.pathname
.split("/")
.map((s) => (/^\d+$|^[0-9a-f-]{8,}$|^[A-Za-z0-9_-]{20,}$/.test(s) ? ":id" : s))
.join("/");
return (port ? `${port}:` : "") + (path || "/");
}
/** Typing goes into `el` without a click: it has the focus, or (rich text)
8000
the caret is in it. */
function hasFocus(el) {
const active = document.activeElement;
if (el === active || el.contains(active)) return true;
const caret = getSelection()?.anchorNode;
return Boolean(el.isContentEditable && active?.contains(el) && caret && el.contains(caret));
}
/** The floating Ask button lies over the middle of `el`, where a click lands. */
function coveredByAsk(el) {
const r = el.getBoundingClientRect();
const top = document.elementFromPoint(r.left + r.width / 2, r.top + r.height / 2);
return Boolean(top?.closest("[data-ask-button]"));
}
export class Meter {
/** `touch`: a phone, where clicks are taps and bringing a control into view takes a swipe. */
constructor(page, { touch = false } = {}) {
this.page = page;
this.touch = touch;
this.journey = null;
this.journeys = [];
}
start(module, feature, note = "") {
this.journey = { module, feature, note, clicks: 0, keys: 0, prompts: 0, swipes: 0, steps: [], started: Date.now() };
}
end(ok = true, error = null) {
const j = this.journey;
if (!j) return null;
j.ms = Date.now() - j.started;
delete j.started;
j.ok = ok;
if (error) j.error = String(error).split("\n")[0].slice(0, 300);
this.journeys.push(j);
this.journey = null;
return j;
}
record(kind, what, clicks, keys, prompts = 0, swipes = 0) {
const j = this.journey;
if (!j) return;
j.clicks += clicks;
j.keys += keys;
j.prompts += prompts;
j.swipes += swipes;
const step = { page: pageKey(this.page.url()), kind, what, clicks, keys, prompts };
if (swipes) step.swipes = swipes;
j.steps.push(step);
}
/** On a phone, 1 when the control is off the screen (a swipe brings it into view); otherwise 0. */
async swipesTo(locator) {
if (!this.touch) return 0;
// Where it settles, not where an opening dialog or a late layout has it for a moment
let box = await locator.boundingBox();
for (let i = 0; i < 10; i++) {
await this.page.waitForTimeout(100);
const next = await locator.boundingBox();
const same = box && next && box.x === next.x && box.y === next.y && box.width === next.width;
box = next;
if (same) break;
}
const view = this.page.viewportSize();
if (!box || !view) return 0;
const off = box.y < 0 || box.y + box.height > view.height || box.x < 0 || box.x + box.width > view.width;
return off ? 1 : 0;
}
/** A click, or a tap on a phone. */
async tapOrClick(locator, options = {}) {
return this.touch ? locator.tap(options) : locator.click(options);
}
async click(locator, what) {
await locator.waitFor(); // on the page it belongs to, so the step is counted there
this.record(this.touch ? "tap" : "click", what, 1, 0, 0, await this.swipesTo(locator));
try {
await this.tapOrClick(locator, { trial: true, timeout: 3000 });
return await this.tapOrClick(locator);
} catch {
// Covered where it scrolled to: a person scrolls on to the end of the page (not counted) and tries again
await this.page.mouse.wheel(0, 10000).catch(() => {});
await this.page.waitForTimeout(300);
}
try {
await this.tapOrClick(locator, { trial: true, timeout: 2000 });
} catch {
// The floating Ask button rises above controls (or steps aside), so it covering one is a failure. Anything else
// lying over it (a toast) is noted in the step; the click still counts one, done here with the keyboard so it
// reaches the control rather than what covers it.
await locator.waitFor({ state: "visible" });
if (await locator.evaluate(coveredByAsk)) throw new Error(`${what} is covered by the floating Ask button`);
const step = this.journey?.steps.at(-1);
if (step) step.what += " (covered by something else on the page)";
await locator.focus();
await this.page.keyboard.press("Enter");
return;
}
await this.tapOrClick(locator);
}
/** Types `text` into a field, adding the click that focuses it when it isn't focused already. */
async type(locator, text, what) {
await locator.waitFor();
const focused = await locator.evaluate(hasFocus);
this.record("type", what, focused ? 0 : 1, text.length, 0, focused ? 0 : await this.swipesTo(locator));
if (!focused) await this.tapOrClick(locator);
await locator.pressSequentially(text);
}
/** Pastes `text` into a field: the focusing click when needed, and the paste shortcut. */
async paste(locator, text, what) {
await locator.waitFor();
const focused = await locator.evaluate(hasFocus);
this.record("paste", what, focused ? 0 : 1, 1, 0, focused ? 0 : await this.swipesTo(locator));
await locator.fill(text);
}
/** Replaces what a field holds: the focusing click when needed, Ctrl+A unless it's all selected, the new text. */
async replace(locator, text, what) {
await locator.waitFor();
const { focused, selected } = await locator.evaluate((el) => ({
focused: el === document.activeElement,
selected: el === document.activeElement && el.value != null && el.selectionStart === 0 && el.selectionEnd === el.value.length,
}));
this.record("replace", what, focused ? 0 : 1, (selected ? 0 : 1) + text.length, 0, focused ? 0 : await this.swipesTo(locator));
if (!focused) await this.tapOrClick(locator);
if (!selected) await this.page.keyboard.press("ControlOrMeta+a");
await locator.pressSequentially(text);
}
async press(key, what = key) {
this.record("key", what, 0, 1);
await this.page.keyboard.press(key);
}
/** Picks an option from a native select: open it, choose. */
async select(locator, value, what) {
await locator.waitFor();
this.record("select", what, 2, 0, 0, await this.swipesTo(locator));
await locator.selectOption(value);
}
/** Opens the system file chooser from `button` and picks `files`. */
async upload(button, files, what) {
this.record("upload", what, 2, 0, 0, await this.swipesTo(button));
const [chooser] = await Promise.all([this.page.waitForEvent("filechooser"), this.tapOrClick(button)]);
await chooser.setFiles(files);
}
/** A passkey prompt the browser shows (the virtual authenticator answers it). */
passkey(what = "passkey prompt") {
this.record("passkey", what, 0, 0, 1);
}
/** Arriving from a link the app or the installer printed, as a person pasting or clicking it would. */
openLink(what) {
this.record("link", what, 1, 0);
}
}