@webship-js/vue-steps 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +415 -2
- package/demo/app/app.vue +19 -0
- package/demo/app/pages/about.vue +10 -0
- package/demo/app/pages/client-only.vue +12 -0
- package/demo/app/pages/contact.vue +44 -0
- package/demo/app/pages/index.vue +15 -0
- package/demo/app/pages/posts/[id].vue +16 -0
- package/demo/app/pages/posts/index.vue +18 -0
- package/demo/app/pages/posts/latest.vue +11 -0
- package/demo/nuxt.config.ts +9 -0
- package/demo/server/api/contact.post.ts +19 -0
- package/demo/server/api/posts/[id].get.ts +8 -0
- package/demo/server/api/posts.get.ts +8 -0
- package/package.json +67 -4
- package/tests/features/errors.feature +14 -0
- package/tests/features/forms.feature +38 -0
- package/tests/features/page.feature +32 -0
- package/tests/features/views.feature +57 -0
- package/tests/step-definitions/vue-errors.steps.js +51 -0
- package/tests/step-definitions/vue-forms.steps.js +171 -0
- package/tests/step-definitions/vue-helpers.js +294 -0
- package/tests/step-definitions/vue-page.steps.js +109 -0
- package/tests/step-definitions/vue-probe.js +305 -0
- package/tests/step-definitions/vue-views.steps.js +211 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The page side of the pack: put into every page before the page's own scripts (vue-helpers.js does
|
|
4
|
+
// it), it installs `window.__webshipVue`.
|
|
5
|
+
//
|
|
6
|
+
// It keeps to the page side and to the app's own signals: an id for each document (so a step tells a
|
|
7
|
+
// navigation inside the app from a whole page load without counting on requests), the frames the page
|
|
8
|
+
// asks for, the requests it has in flight (fetch and XMLHttpRequest: a form sent in place, the data of
|
|
9
|
+
// a view), the input events, the changes of the DOM, whether the server's HTML held the page or only an
|
|
10
|
+
// empty shell, and, once the app has wired the page up, the navigations of its router: Nuxt's own
|
|
11
|
+
// page:loading:start and page:loading:end, or Vue Router's guards in an app without Nuxt.
|
|
12
|
+
//
|
|
13
|
+
// Not a step file: it registers no step definitions. `appProbe` is a plain function, serialised into
|
|
14
|
+
// the page.
|
|
15
|
+
|
|
16
|
+
function appProbe() {
|
|
17
|
+
if (window.__webshipVue) return;
|
|
18
|
+
const now = () => performance.now();
|
|
19
|
+
const nativeRaf = window.requestAnimationFrame.bind(window);
|
|
20
|
+
const nativeCancel = window.cancelAnimationFrame.bind(window);
|
|
21
|
+
const nativeFetch = window.fetch ? window.fetch.bind(window) : null;
|
|
22
|
+
const state = {
|
|
23
|
+
doc: `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`,
|
|
24
|
+
frames: 0,
|
|
25
|
+
pending: new Set(),
|
|
26
|
+
lastFrame: 0,
|
|
27
|
+
lastInput: 0,
|
|
28
|
+
lastMutation: 0,
|
|
29
|
+
inFlight: 0,
|
|
30
|
+
seq: 0,
|
|
31
|
+
requests: [],
|
|
32
|
+
navPending: 0,
|
|
33
|
+
navStarts: 0,
|
|
34
|
+
navEnds: 0,
|
|
35
|
+
lastNavEnd: 0,
|
|
36
|
+
hooked: false,
|
|
37
|
+
shellOnly: null,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Frames: every callback the page asks for, until it runs or is cancelled. The probe's own waits use
|
|
41
|
+
// the original function, so they are not counted.
|
|
42
|
+
window.requestAnimationFrame = function requestAnimationFrame(callback) {
|
|
43
|
+
let id = 0;
|
|
44
|
+
id = nativeRaf((time) => {
|
|
45
|
+
state.pending.delete(id);
|
|
46
|
+
state.frames += 1;
|
|
47
|
+
state.lastFrame = now();
|
|
48
|
+
return callback(time);
|
|
49
|
+
});
|
|
50
|
+
state.pending.add(id);
|
|
51
|
+
return id;
|
|
52
|
+
};
|
|
53
|
+
window.cancelAnimationFrame = function cancelAnimationFrame(id) {
|
|
54
|
+
state.pending.delete(id);
|
|
55
|
+
return nativeCancel(id);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// Requests the page starts, by fetch (Nuxt's $fetch and useFetch) or XMLHttpRequest (a library of the app).
|
|
59
|
+
const track = (url, method) => {
|
|
60
|
+
const entry = { seq: (state.seq += 1), url, method, status: 0, start: now(), end: 0, body: '' };
|
|
61
|
+
state.requests.push(entry);
|
|
62
|
+
if (state.requests.length > 200) state.requests.shift();
|
|
63
|
+
state.inFlight += 1;
|
|
64
|
+
return (status, body) => {
|
|
65
|
+
if (entry.end) return;
|
|
66
|
+
entry.status = status;
|
|
67
|
+
entry.end = now();
|
|
68
|
+
if (typeof body === 'string') entry.body = body.slice(0, 2000);
|
|
69
|
+
state.inFlight -= 1;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
if (nativeFetch) {
|
|
73
|
+
window.fetch = function fetch(input, init) {
|
|
74
|
+
let url = '';
|
|
75
|
+
let method = 'GET';
|
|
76
|
+
try {
|
|
77
|
+
url = new URL(typeof input === 'string' || input instanceof URL ? String(input) : input.url, location.href).href;
|
|
78
|
+
method = String((init && init.method) || (input && input.method) || 'GET').toUpperCase();
|
|
79
|
+
} catch (e) { /* an address the page itself will refuse */ }
|
|
80
|
+
const done = track(url, method);
|
|
81
|
+
return nativeFetch(input, init).then((response) => {
|
|
82
|
+
// The answer's text for the form steps, read from a copy, never from the page's own body.
|
|
83
|
+
if (method !== 'GET') response.clone().text().then((text) => done(response.status, text), () => done(response.status));
|
|
84
|
+
else done(response.status);
|
|
85
|
+
return response;
|
|
86
|
+
}, (error) => { done(-1); throw error; });
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
if (window.XMLHttpRequest) {
|
|
90
|
+
const open = XMLHttpRequest.prototype.open;
|
|
91
|
+
const send = XMLHttpRequest.prototype.send;
|
|
92
|
+
XMLHttpRequest.prototype.open = function (method, url) {
|
|
93
|
+
this.__webshipVue = { method: String(method || 'GET').toUpperCase(), url: String(url) };
|
|
94
|
+
return open.apply(this, arguments);
|
|
95
|
+
};
|
|
96
|
+
XMLHttpRequest.prototype.send = function () {
|
|
97
|
+
const meta = this.__webshipVue || { method: 'GET', url: '' };
|
|
98
|
+
let url = meta.url;
|
|
99
|
+
try { url = new URL(meta.url, location.href).href; } catch (e) { /* kept as given */ }
|
|
100
|
+
const done = track(url, meta.method);
|
|
101
|
+
this.addEventListener('loadend', () => done(this.status, typeof this.responseText === 'string' ? this.responseText : ''));
|
|
102
|
+
return send.apply(this, arguments);
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const markInput = () => { state.lastInput = now(); };
|
|
107
|
+
for (const type of ['pointerdown', 'mousedown', 'keydown', 'click', 'submit', 'input']) {
|
|
108
|
+
window.addEventListener(type, markInput, true);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
new MutationObserver(() => { state.lastMutation = now(); })
|
|
112
|
+
.observe(document, { childList: true, subtree: true, attributes: true, characterData: true });
|
|
113
|
+
|
|
114
|
+
// The server's HTML, before the app's scripts run (they run after parsing, before DOMContentLoaded): did
|
|
115
|
+
// it hold the page, or only an empty element for the app to draw into?
|
|
116
|
+
const mountPoint = () => document.getElementById('__nuxt') || document.getElementById('app');
|
|
117
|
+
document.addEventListener('readystatechange', () => {
|
|
118
|
+
if (document.readyState !== 'interactive' || state.shellOnly !== null) return;
|
|
119
|
+
const el = mountPoint();
|
|
120
|
+
const data = document.getElementById('__NUXT_DATA__');
|
|
121
|
+
state.shellOnly = data && data.dataset ? data.dataset.ssr === 'false' : (el ? el.children.length === 0 : null);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// Nuxt's own app, from the page it rendered on the server or drew in the browser.
|
|
125
|
+
const nuxtMarked = () => !!(document.getElementById('__NUXT_DATA__') || window.__NUXT__ || document.getElementById('__nuxt'));
|
|
126
|
+
const nuxtApp = () => {
|
|
127
|
+
try { return typeof window.useNuxtApp === 'function' ? window.useNuxtApp() : null; } catch (e) { return null; }
|
|
128
|
+
};
|
|
129
|
+
// A Vue app mounted in the browser marks its element data-v-app; one rendered on the server and hydrated does not,
|
|
130
|
+
// and only its element's own app (__vue_app__) says it is there.
|
|
131
|
+
const vueRoot = () => {
|
|
132
|
+
const marked = document.querySelector('[data-v-app]');
|
|
133
|
+
if (marked) return marked;
|
|
134
|
+
const el = document.getElementById('app');
|
|
135
|
+
return el && el.__vue_app__ ? el : null;
|
|
136
|
+
};
|
|
137
|
+
// A Vue app that has not mounted yet: create-vue's page, an empty #app and the app's module script.
|
|
138
|
+
const vueShell = () => !!(document.getElementById('app') && document.querySelector('script[type="module"]'));
|
|
139
|
+
const vueRouter = () => {
|
|
140
|
+
const root = vueRoot();
|
|
141
|
+
try { return root && root.__vue_app__ ? root.__vue_app__.config.globalProperties.$router || null : null; } catch (e) { return null; }
|
|
142
|
+
};
|
|
143
|
+
const navStart = () => { state.navPending += 1; state.navStarts += 1; };
|
|
144
|
+
const navEnd = () => { state.navPending = Math.max(0, state.navPending - 1); state.navEnds += 1; state.lastNavEnd = now(); };
|
|
145
|
+
|
|
146
|
+
// Wired up: Nuxt's app no longer hydrating, or a Vue app mounted. Once it is, the probe listens to the
|
|
147
|
+
// app's navigations: Nuxt's page:loading hooks (an error that ends a navigation ends it too), or Vue
|
|
148
|
+
// Router's guards in an app without Nuxt.
|
|
149
|
+
//
|
|
150
|
+
// A navigation that a guard redirects runs the guards again for the new address: the router marks that hop
|
|
151
|
+
// with the address it came from (redirectedFrom). Nuxt calls page:loading:start for each hop and
|
|
152
|
+
// page:loading:end once, when the last address has loaded; so a redirected hop folds the count back to the
|
|
153
|
+
// one navigation it belongs to, and with Vue Router alone it is not counted again.
|
|
154
|
+
function wiredUp() {
|
|
155
|
+
const app = nuxtApp();
|
|
156
|
+
if (app) {
|
|
157
|
+
if (app.isHydrating !== false) return false;
|
|
158
|
+
if (!state.hooked) {
|
|
159
|
+
state.hooked = true;
|
|
160
|
+
try {
|
|
161
|
+
app.hook('page:loading:start', navStart);
|
|
162
|
+
app.hook('page:loading:end', navEnd);
|
|
163
|
+
app.hook('app:error', () => { if (state.navPending) { state.navPending = 0; state.navEnds += 1; state.lastNavEnd = now(); } });
|
|
164
|
+
app.hook('vue:error', () => { if (state.navPending) { state.navPending = 0; state.navEnds += 1; state.lastNavEnd = now(); } });
|
|
165
|
+
const nuxtRouter = app.$router;
|
|
166
|
+
if (nuxtRouter && typeof nuxtRouter.beforeEach === 'function') {
|
|
167
|
+
nuxtRouter.beforeEach((to) => {
|
|
168
|
+
if (to && to.redirectedFrom && state.navPending > 1) { state.navStarts -= state.navPending - 1; state.navPending = 1; }
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
} catch (e) { /* an app without hooks: the address and the quiet still tell */ }
|
|
172
|
+
}
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
if (nuxtMarked()) return false;
|
|
176
|
+
if (!vueRoot()) return false;
|
|
177
|
+
const router = vueRouter();
|
|
178
|
+
if (router && !state.hooked) {
|
|
179
|
+
state.hooked = true;
|
|
180
|
+
try {
|
|
181
|
+
router.beforeEach((to) => { if (!(to && to.redirectedFrom && state.navPending > 0)) navStart(); });
|
|
182
|
+
router.afterEach(() => { navEnd(); });
|
|
183
|
+
router.onError(() => { if (state.navPending) navEnd(); });
|
|
184
|
+
} catch (e) { /* the quiet still tells */ }
|
|
185
|
+
}
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Does this page run its own scripts? A browser without JavaScript (a lane of the suite) never lets the
|
|
190
|
+
// app wire the page up, and the steps do not wait for it there. Asked once per document.
|
|
191
|
+
let scriptsRun = null;
|
|
192
|
+
function jsOn() {
|
|
193
|
+
if (scriptsRun !== null) return scriptsRun;
|
|
194
|
+
try {
|
|
195
|
+
const s = document.createElement('script');
|
|
196
|
+
s.textContent = 'window.__webshipVueScripts = 1;';
|
|
197
|
+
(document.head || document.documentElement).appendChild(s);
|
|
198
|
+
s.remove();
|
|
199
|
+
scriptsRun = window.__webshipVueScripts === 1;
|
|
200
|
+
} catch (e) {
|
|
201
|
+
scriptsRun = false;
|
|
202
|
+
}
|
|
203
|
+
return scriptsRun;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function runningAnimations() {
|
|
207
|
+
if (!document.getAnimations) return [];
|
|
208
|
+
return document.getAnimations().filter((a) => {
|
|
209
|
+
if (a.playState !== 'running') return false;
|
|
210
|
+
try {
|
|
211
|
+
const end = a.effect && a.effect.getComputedTiming ? a.effect.getComputedTiming().endTime : 0;
|
|
212
|
+
return Number.isFinite(end);
|
|
213
|
+
} catch (e) {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
}).map((a) => a.animationName || a.transitionProperty || 'an animation');
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const announcerEl = () => document.querySelector('.nuxt-route-announcer [aria-live], .nuxt-route-announcer [role="status"]');
|
|
220
|
+
const clean = (text) => String(text || '').replace(/\s+/g, ' ').trim();
|
|
221
|
+
|
|
222
|
+
const lib = {
|
|
223
|
+
doc: () => state.doc,
|
|
224
|
+
jsOn,
|
|
225
|
+
seq: () => state.seq,
|
|
226
|
+
requestsSince: (seq) => state.requests.filter((r) => r.seq > seq).map((r) => ({ ...r })),
|
|
227
|
+
navCounts: () => ({ starts: state.navStarts, ends: state.navEnds, pending: state.navPending }),
|
|
228
|
+
route() {
|
|
229
|
+
const app = nuxtApp();
|
|
230
|
+
try { if (app && app._route) return app._route.fullPath; } catch (e) { /* below */ }
|
|
231
|
+
const router = vueRouter();
|
|
232
|
+
try { if (router) return router.currentRoute.value.fullPath; } catch (e) { /* below */ }
|
|
233
|
+
return `${location.pathname}${location.search}${location.hash}`;
|
|
234
|
+
},
|
|
235
|
+
announcer: () => { const el = announcerEl(); return el ? clean(el.textContent) : null; },
|
|
236
|
+
shellOnly: () => state.shellOnly,
|
|
237
|
+
nuxtError() {
|
|
238
|
+
const app = nuxtApp();
|
|
239
|
+
try {
|
|
240
|
+
const e = app && app.payload ? app.payload.error : null;
|
|
241
|
+
return e ? { status: Number(e.statusCode || e.status) || 0, message: String(e.statusMessage || e.message || '') } : null;
|
|
242
|
+
} catch (x) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
// The page's state for a wait. Still: the document loaded, no request in flight, no navigation of the
|
|
247
|
+
// app in progress, no frame asked for, no finite animation or transition running, the page wired up
|
|
248
|
+
// when it can be, and 40 ms of quiet (no frame, no change of the DOM), with a frame after the last
|
|
249
|
+
// input or 100 ms of nothing.
|
|
250
|
+
state() {
|
|
251
|
+
const t = now();
|
|
252
|
+
const js = jsOn();
|
|
253
|
+
const app = js && (nuxtMarked() || !!vueRoot() || vueShell());
|
|
254
|
+
const wired = js ? wiredUp() : false;
|
|
255
|
+
const animations = runningAnimations();
|
|
256
|
+
const sinceFrame = state.lastFrame ? t - state.lastFrame : t;
|
|
257
|
+
const sinceMutation = state.lastMutation ? t - state.lastMutation : t;
|
|
258
|
+
const sinceInput = state.lastInput ? t - state.lastInput : t;
|
|
259
|
+
const frameAfterInput = state.lastFrame > state.lastInput;
|
|
260
|
+
const busy = [];
|
|
261
|
+
if (document.readyState !== 'complete') busy.push(`the document is ${document.readyState}`);
|
|
262
|
+
if (app && !wired) busy.push('the app has not wired the page up yet');
|
|
263
|
+
if (state.navPending) busy.push('a navigation of the app is in progress');
|
|
264
|
+
if (state.inFlight) busy.push(`${state.inFlight} request${state.inFlight > 1 ? 's' : ''} in flight`);
|
|
265
|
+
if (state.pending.size) busy.push(`${state.pending.size} frame${state.pending.size > 1 ? 's' : ''} asked for`);
|
|
266
|
+
if (animations.length) busy.push(`running: ${animations.slice(0, 3).join(', ')}`);
|
|
267
|
+
if (sinceMutation < 40) busy.push('the page changed in the last 40 ms');
|
|
268
|
+
if (sinceFrame < 40) busy.push('a frame ran in the last 40 ms');
|
|
269
|
+
if (!frameAfterInput && sinceInput < 100) busy.push('no frame since the last input');
|
|
270
|
+
return {
|
|
271
|
+
now: t,
|
|
272
|
+
doc: state.doc,
|
|
273
|
+
url: location.href,
|
|
274
|
+
route: lib.route(),
|
|
275
|
+
title: document.title,
|
|
276
|
+
readyState: document.readyState,
|
|
277
|
+
js,
|
|
278
|
+
app,
|
|
279
|
+
nuxt: nuxtMarked(),
|
|
280
|
+
wired,
|
|
281
|
+
inFlight: state.inFlight,
|
|
282
|
+
nav: { starts: state.navStarts, ends: state.navEnds, pending: state.navPending },
|
|
283
|
+
frames: state.frames,
|
|
284
|
+
animations,
|
|
285
|
+
announcer: lib.announcer(),
|
|
286
|
+
busy,
|
|
287
|
+
still: busy.length === 0,
|
|
288
|
+
};
|
|
289
|
+
},
|
|
290
|
+
firstHeading() {
|
|
291
|
+
const h = document.querySelector('h1');
|
|
292
|
+
return h ? clean(h.textContent) : null;
|
|
293
|
+
},
|
|
294
|
+
alerts() {
|
|
295
|
+
return [...document.querySelectorAll('[role="alert"]')].filter((e) => !e.closest('.nuxt-route-announcer')).map((e) => clean(e.textContent)).filter(Boolean);
|
|
296
|
+
},
|
|
297
|
+
pageText() {
|
|
298
|
+
return clean(document.body && document.body.innerText);
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
window.__webshipVue = { lib };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
module.exports = { appProbe };
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The app's views: a link used and the browser's back, each to its end (a change of view inside the
|
|
4
|
+
// app, or a whole page load, told apart by the document, never by a request: the app fetches a view's
|
|
5
|
+
// code ahead for the links on the screen, and its data during the navigation), the route announcer,
|
|
6
|
+
// the link of the current view, the view's address, and its data.
|
|
7
|
+
|
|
8
|
+
const { When, Then } = require('@cucumber/cucumber');
|
|
9
|
+
const h = require('./vue-helpers');
|
|
10
|
+
|
|
11
|
+
const withoutHash = (url) => String(url || '').split('#')[0];
|
|
12
|
+
|
|
13
|
+
async function linkByName(world, name) {
|
|
14
|
+
const found = await h.byRoleAndName(world, 'link', name);
|
|
15
|
+
if (!found.locator) {
|
|
16
|
+
throw await h.laneFailure(world, `Expected a link named "${name}" on the page, but there is none.`, {
|
|
17
|
+
found: `the links here: ${h.listed(found.names, 20)}`,
|
|
18
|
+
hint: 'a link is named by its text, or its label; the name is matched from its start.',
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return found.locator;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** How a step's navigation ended, for a failure message. */
|
|
25
|
+
function stillBusy(s) {
|
|
26
|
+
return `the address is ${s.url || '(unknown)'}; route announcer: "${s.announcer || ''}"; still busy: ${(s.busy || []).join('; ') || 'nothing'}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Use a link of the app, by its name, and wait for the navigation to end: a change of view inside the app
|
|
31
|
+
* (the app's router started and ended its navigation, the route announcer holds the new title, the page
|
|
32
|
+
* is still), or a whole page load (the new page loaded, and wired up when it can be). The step first
|
|
33
|
+
* waits for the app to wire the page up, as a click before then loads a whole page.
|
|
34
|
+
*
|
|
35
|
+
* Example #1: When I use the "Posts" link
|
|
36
|
+
* Example #2: When I use the "About" link
|
|
37
|
+
* Example #3: And I use the "Contact" link
|
|
38
|
+
* Example #4: When we use the "Home" link
|
|
39
|
+
* Example #5: When I use the "Invoices" link
|
|
40
|
+
*/
|
|
41
|
+
When(/^(?:I |we )?use the "([^"]*)" link$/, async function (name) {
|
|
42
|
+
await h.beforeActing(this);
|
|
43
|
+
const link = await linkByName(this, name);
|
|
44
|
+
const before = await h.pageState(this);
|
|
45
|
+
const href = await link.evaluate((a) => a.href || '').catch(() => '');
|
|
46
|
+
const sameAddress = !!href && withoutHash(href) === withoutHash(before && before.url);
|
|
47
|
+
try {
|
|
48
|
+
await link.click({ timeout: h.budget(this) });
|
|
49
|
+
} catch (e) {
|
|
50
|
+
throw h.friendly({ action: 'click', target: `the link "${name}"`, cause: e, hint: 'it may be covered by another part of the page, or not on the screen.' });
|
|
51
|
+
}
|
|
52
|
+
const nav = await h.followNavigation(this, before, sameAddress);
|
|
53
|
+
if (nav.kind === 'none') {
|
|
54
|
+
throw await h.laneFailure(this, `Using the link "${name}" did not end a navigation in ${Math.round(h.budget(this) / 1000)} s.`, {
|
|
55
|
+
found: `${stillBusy(nav.state || {})}${href ? ` (the link goes to ${href})` : ''}`,
|
|
56
|
+
hint: 'the app may have stopped the navigation (a guard of its router), or the view it goes to failed to load.',
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Go back with the browser, and wait for the navigation to end, a change of view inside the app or a
|
|
63
|
+
* whole page load, as using a link does.
|
|
64
|
+
*
|
|
65
|
+
* Example #1: When I return to the previous view
|
|
66
|
+
* Example #2: And I return to the previous view
|
|
67
|
+
* Example #3: When we return to the previous view
|
|
68
|
+
* Example #4: Then I return to the previous view
|
|
69
|
+
* Example #5: Given I return to the previous view
|
|
70
|
+
*/
|
|
71
|
+
When(/^(?:I |we )?return to the previous view$/, async function () {
|
|
72
|
+
await h.beforeActing(this);
|
|
73
|
+
const before = await h.pageState(this);
|
|
74
|
+
try {
|
|
75
|
+
await this.page.goBack({ waitUntil: 'commit', timeout: h.budget(this) });
|
|
76
|
+
} catch (e) {
|
|
77
|
+
throw h.friendly({ action: 'go back', target: 'the previous view', cause: e, hint: 'the browser may have no page before this one in the scenario.' });
|
|
78
|
+
}
|
|
79
|
+
const nav = await h.followNavigation(this, before, false);
|
|
80
|
+
if (nav.kind === 'none') {
|
|
81
|
+
throw await h.laneFailure(this, 'Going back did not end a navigation.', {
|
|
82
|
+
found: stillBusy(nav.state || {}),
|
|
83
|
+
hint: 'this may be the first page the scenario opened: there is no page before it.',
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* How the last navigation of the pack's steps ended: a change of view in place (the same document: the
|
|
90
|
+
* page did not load again, and what it held in the browser stays), or a new page loaded.
|
|
91
|
+
*
|
|
92
|
+
* Example #1: Then the view should have changed in place
|
|
93
|
+
* Example #2: Then a new page should have loaded
|
|
94
|
+
* Example #3: And the view should have changed in place
|
|
95
|
+
* Example #4: But a new page should have loaded
|
|
96
|
+
* Example #5: Then the view should have changed in place
|
|
97
|
+
*/
|
|
98
|
+
Then(/^(the view should have changed in place|a new page should have loaded)$/, async function (how) {
|
|
99
|
+
const nav = this.vueNavigation;
|
|
100
|
+
const wanted = how === 'the view should have changed in place' ? 'app' : 'whole';
|
|
101
|
+
if (!nav) {
|
|
102
|
+
throw await h.laneFailure(this, 'Expected a navigation of the scenario, but none of its steps made one.', {
|
|
103
|
+
hint: 'navigate with "I use the "..." link" or "I return to the previous view" first.',
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (nav.kind === wanted) return;
|
|
107
|
+
const said = { app: 'changed the view in place', whole: 'loaded a new page', none: 'did not end' };
|
|
108
|
+
throw await h.laneFailure(this, `Expected the last navigation to have ${wanted === 'app' ? 'changed the view in place' : 'loaded a new page'}, but it ${said[nav.kind] || nav.kind}.`, {
|
|
109
|
+
found: `from ${nav.from} to ${nav.to}`,
|
|
110
|
+
hint: wanted === 'app' ? 'a link to another site, a page the app has not wired up, or a browser without JavaScript loads a new page.' : 'a page the app has wired up changes its view in place.',
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The route announcer, the live region the app keeps for screen readers, says the title of the view a
|
|
116
|
+
* navigation came to (matched exactly, then without regard to case, then from its start). Nuxt's
|
|
117
|
+
* announcer says the page's title each time the page's head is rendered: the step reads it once the
|
|
118
|
+
* navigation has ended, and waits for it.
|
|
119
|
+
*
|
|
120
|
+
* Example #1: Then the new view should be announced as "Posts"
|
|
121
|
+
* Example #2: Then the new view should be announced as "About"
|
|
122
|
+
* Example #3: And the new view should be announced as "Moving between pages"
|
|
123
|
+
* Example #4: Then the new view should be announced as "Home"
|
|
124
|
+
* Example #5: Then the new view should be announced as "Invoices"
|
|
125
|
+
*/
|
|
126
|
+
Then(/^the new view should be announced as "([^"]*)"$/, async function (title) {
|
|
127
|
+
const want = h.norm(title).toLowerCase();
|
|
128
|
+
const result = await h.poll(this, (s) => {
|
|
129
|
+
if (!s || s.announcer === null || s.announcer === undefined) return { done: false, state: s };
|
|
130
|
+
const said = h.norm(s.announcer).toLowerCase();
|
|
131
|
+
return { done: !s.nav.pending && (said === want || (want.length >= 3 && said.startsWith(want))), state: s };
|
|
132
|
+
}, h.budget(this));
|
|
133
|
+
if (result.done) return;
|
|
134
|
+
const s = result.state || {};
|
|
135
|
+
throw await h.laneFailure(this, s.announcer === null || s.announcer === undefined
|
|
136
|
+
? `Expected the new view to be announced as "${title}", but the page has no route announcer.`
|
|
137
|
+
: `Expected the new view to be announced as "${title}", but the announcer says "${h.excerpt(s.announcer, 80)}".`, {
|
|
138
|
+
found: `the page's title is "${s.title || ''}"`,
|
|
139
|
+
hint: s.announcer === null || s.announcer === undefined ? 'the announcer is the app\'s own (Nuxt\'s <NuxtRouteAnnouncer />); it is there once the app has wired the page up, and an error page has none.' : 'the announcer says the title of the view each time the page\'s head is rendered.',
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A link marks the view a person is on (`aria-current="page"`, on the link or on the list item of the
|
|
145
|
+
* navigation that holds it): the link of the current view in a navigation. The step waits for it.
|
|
146
|
+
*
|
|
147
|
+
* Example #1: Then the "Posts" link should be marked current
|
|
148
|
+
* Example #2: Then the "Home" link should be marked current
|
|
149
|
+
* Example #3: And the "About" link should be marked current
|
|
150
|
+
* Example #4: Then the "Invoices" link should be marked current
|
|
151
|
+
* Example #5: Then the "Settings" link should be marked current
|
|
152
|
+
*/
|
|
153
|
+
Then(/^the "([^"]*)" link should be marked current$/, async function (name) {
|
|
154
|
+
const deadline = Date.now() + h.budget(this);
|
|
155
|
+
for (;;) {
|
|
156
|
+
const link = (await h.byRoleAndName(this, 'link', name)).locator;
|
|
157
|
+
const marked = link && await link.evaluate((a) => {
|
|
158
|
+
const holder = a.closest('[aria-current]');
|
|
159
|
+
return !!holder && holder.getAttribute('aria-current') === 'page' && (holder === a || (holder.tagName === 'LI' && holder.contains(a)));
|
|
160
|
+
}).catch(() => false);
|
|
161
|
+
if (marked) return;
|
|
162
|
+
if (Date.now() + 50 >= deadline) break;
|
|
163
|
+
await h.sleep(50);
|
|
164
|
+
}
|
|
165
|
+
const current = await this.page.locator('[aria-current]').evaluateAll((els) => els.map((e) => `${(e.textContent || e.getAttribute('aria-label') || '').replace(/\s+/g, ' ').trim()} (${e.getAttribute('aria-current')})`)).catch(() => []);
|
|
166
|
+
throw await h.laneFailure(this, `Expected the link "${name}" to be marked current, but it is not.`, {
|
|
167
|
+
found: `the links marked current: ${h.listed(current)}`,
|
|
168
|
+
hint: 'the link of the current view carries aria-current="page" (a router link does), or the list item that holds it does.',
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The address of the view the app shows, as its router has it (the path and query), once no navigation
|
|
174
|
+
* is in progress: "/posts", "/posts/2", "/contact?sent=Ada". The step waits for it.
|
|
175
|
+
*
|
|
176
|
+
* Example #1: Then the view's address should be "/posts"
|
|
177
|
+
* Example #2: Then the view's address should be "/posts/2"
|
|
178
|
+
* Example #3: And the view's address should be "/"
|
|
179
|
+
* Example #4: Then the view's address should be "/contact?sent=Ada"
|
|
180
|
+
* Example #5: Then the view's address should be "/invoices"
|
|
181
|
+
*/
|
|
182
|
+
Then(/^the view's address should be "([^"]*)"$/, async function (address) {
|
|
183
|
+
const want = String(address).trim();
|
|
184
|
+
const result = await h.poll(this, (s) => ({ done: !!s && !s.nav.pending && decodeURIComponent(s.route) === decodeURIComponent(want), state: s }), h.budget(this));
|
|
185
|
+
if (result.done) return;
|
|
186
|
+
const s = result.state || {};
|
|
187
|
+
throw await h.laneFailure(this, `Expected the view's address to be "${want}", but it is "${s.route || '(unknown)'}".`, {
|
|
188
|
+
found: `the page's address is ${s.url || '(unknown)'}`,
|
|
189
|
+
hint: 'the address is the route the app shows: its path and query, without the site\'s own address.',
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Wait for the view's data to arrive: the requests of the page answered (the data a view fetches as it
|
|
195
|
+
* opens, or loads again), no navigation in progress, and the page still. Nothing about a fixed time.
|
|
196
|
+
*
|
|
197
|
+
* Example #1: When I wait for the view's data to arrive
|
|
198
|
+
* Example #2: And I wait for the view's data to arrive
|
|
199
|
+
* Example #3: When we wait for the view's data to arrive
|
|
200
|
+
* Example #4: Then I wait for the view's data to arrive
|
|
201
|
+
* Example #5: Given I wait for the view's data to arrive
|
|
202
|
+
*/
|
|
203
|
+
When(/^(?:I |we )?wait for the view's data to arrive$/, async function () {
|
|
204
|
+
const result = await h.poll(this, (s) => ({ done: !!s && !s.inFlight && !s.nav.pending && s.still, state: s }), h.budget(this));
|
|
205
|
+
if (result.done) return;
|
|
206
|
+
const s = result.state || {};
|
|
207
|
+
throw await h.laneFailure(this, `Expected the view's data to arrive, but the page was still waiting after ${Math.round(h.budget(this) / 1000)} s.`, {
|
|
208
|
+
found: (s.busy || []).join('; ') || 'the page was between two documents',
|
|
209
|
+
hint: 'a request that never answers keeps a view waiting: look at the network of the run, or the server\'s log.',
|
|
210
|
+
});
|
|
211
|
+
});
|