@webship-js/nextjs-steps 0.0.0-stage → 2.0.1
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 +575 -2
- package/package.json +64 -4
- package/tests/features/README.md +28 -0
- package/tests/features/build.feature +24 -0
- package/tests/features/default-app.feature +33 -0
- package/tests/features/development.feature +24 -0
- package/tests/features/errors.feature +38 -0
- package/tests/features/forms.feature +56 -0
- package/tests/features/images.feature +38 -0
- package/tests/features/loading.feature +43 -0
- package/tests/features/navigation.feature +46 -0
- package/tests/features/pages-router.feature +35 -0
- package/tests/features/search-and-pagination.feature +37 -0
- package/tests/features/session.feature +59 -0
- package/tests/step-definitions/README.md +30 -0
- package/tests/step-definitions/nextjs-errors.steps.js +250 -0
- package/tests/step-definitions/nextjs-forms.steps.js +266 -0
- package/tests/step-definitions/nextjs-helpers.js +1036 -0
- package/tests/step-definitions/nextjs-images.steps.js +200 -0
- package/tests/step-definitions/nextjs-loading.steps.js +170 -0
- package/tests/step-definitions/nextjs-navigation.steps.js +194 -0
- package/tests/step-definitions/nextjs-pages.steps.js +204 -0
- package/tests/step-definitions/nextjs-session.steps.js +254 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The pages an app shows when something is missing or fails: the not-found
|
|
4
|
+
// page (the app's own or the framework's default one), the error page an
|
|
5
|
+
// error boundary shows, the details of an error a production build keeps to
|
|
6
|
+
// itself, and the error overlay and tools of the development server.
|
|
7
|
+
|
|
8
|
+
const { Then } = require('@cucumber/cucumber');
|
|
9
|
+
const {
|
|
10
|
+
budget,
|
|
11
|
+
failure,
|
|
12
|
+
shortUrl,
|
|
13
|
+
requests,
|
|
14
|
+
lastDocumentStatus,
|
|
15
|
+
serverRenderErrors,
|
|
16
|
+
consoleOf,
|
|
17
|
+
appInfo,
|
|
18
|
+
pageReady,
|
|
19
|
+
devOverlay,
|
|
20
|
+
eventually,
|
|
21
|
+
} = require('./nextjs-helpers');
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* What the page says about being a not-found page: the status of its
|
|
25
|
+
* document (or of the payload of the client-side navigation that showed it),
|
|
26
|
+
* the robots "noindex" the framework adds to it, and the framework's default
|
|
27
|
+
* page (its "404" heading and "This page could not be found.").
|
|
28
|
+
*/
|
|
29
|
+
async function notFoundState(world) {
|
|
30
|
+
const state = requests(world.page);
|
|
31
|
+
const here = world.page.url();
|
|
32
|
+
const answer = [...state.answers].reverse().find((a) => a.kind === 'navigation' || a.kind === 'data');
|
|
33
|
+
const navigationStatus = answer && answer.at > ((state.documents[state.documents.length - 1] || {}).at || 0) ? answer.status : 0;
|
|
34
|
+
const page = await world.page.evaluate(() => {
|
|
35
|
+
const robots = [...document.querySelectorAll('meta[name="robots"]')].map((m) => m.getAttribute('content') || '').join(' ');
|
|
36
|
+
const h1 = document.querySelector('.next-error-h1');
|
|
37
|
+
return {
|
|
38
|
+
noindex: /noindex/i.test(robots),
|
|
39
|
+
defaultPage: !!h1 && h1.textContent.trim() === '404' && /This page could not be found/i.test(document.body.textContent || ''),
|
|
40
|
+
};
|
|
41
|
+
}).catch(() => ({ noindex: false, defaultPage: false }));
|
|
42
|
+
return { status: lastDocumentStatus(world.page), navigationStatus, here, ...page };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The page is a not-found page: its document answered 404 (or, after a
|
|
47
|
+
* client-side navigation, the payload of the new page did), or the page
|
|
48
|
+
* carries the "noindex" and the default heading the framework gives a page it
|
|
49
|
+
* cannot find. Error pages carry @js-off in a feature: the browser logs
|
|
50
|
+
* "Failed to load resource" for the 404.
|
|
51
|
+
*
|
|
52
|
+
* Example #1: Then I should be on a 404 page
|
|
53
|
+
* Example #2: And we should be on a 404 page
|
|
54
|
+
* Example #3: Then I should not be on a 404 page
|
|
55
|
+
* Example #4: And I should be on a 404 page
|
|
56
|
+
* Example #5: Then we should not be on a 404 page
|
|
57
|
+
*/
|
|
58
|
+
Then(/^(I |we )*should( not)* be on a 404 page$/, async function (pronounCase, notCase) {
|
|
59
|
+
await pageReady(this);
|
|
60
|
+
const s = await notFoundState(this);
|
|
61
|
+
const notFound = s.status === 404 || s.navigationStatus === 404 || (s.noindex && s.defaultPage);
|
|
62
|
+
if (!notCase && !notFound) {
|
|
63
|
+
throw failure(this, `Expected a 404 page, but ${shortUrl(s.here)} answered ${s.status || 'with no status the browser kept'}${s.navigationStatus ? ` (its payload ${s.navigationStatus})` : ''} and is not marked "noindex".`, 'An address the app has no route for, or a page that calls notFound(), answers 404 with its not-found page.');
|
|
64
|
+
}
|
|
65
|
+
if (notCase && notFound) {
|
|
66
|
+
throw failure(this, `Expected a page that exists, but ${shortUrl(s.here)} is a 404 page (status ${s.status || s.navigationStatus}).`, 'Check the address, and that the record the page shows exists.');
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The page is the framework's own not-found page (the app has no
|
|
72
|
+
* not-found page of its own for this address): the heading "404" and "This
|
|
73
|
+
* page could not be found.".
|
|
74
|
+
*
|
|
75
|
+
* Example #1: Then I should be on the default 404 page
|
|
76
|
+
* Example #2: And we should be on the default 404 page
|
|
77
|
+
* Example #3: Then I should not be on the default 404 page
|
|
78
|
+
* Example #4: And I should be on the default 404 page
|
|
79
|
+
* Example #5: Then we should not be on the default 404 page
|
|
80
|
+
*/
|
|
81
|
+
Then(/^(I |we )*should( not)* be on the default 404 page$/, async function (pronounCase, notCase) {
|
|
82
|
+
await pageReady(this);
|
|
83
|
+
const s = await notFoundState(this);
|
|
84
|
+
if (!notCase && !s.defaultPage) {
|
|
85
|
+
throw failure(this, `Expected the framework's default 404 page, but ${shortUrl(s.here)} shows ${s.status === 404 || s.noindex ? 'a not-found page of the app' : 'a page that is not a not-found page'}.`, 'The default page is the heading "404" (.next-error-h1) and "This page could not be found."; an app with its own not-found.js (or 404.js) shows that instead.');
|
|
86
|
+
}
|
|
87
|
+
if (notCase && s.defaultPage) {
|
|
88
|
+
throw failure(this, `Expected a not-found page of the app's own, but ${shortUrl(s.here)} shows the framework's default 404 page.`, 'A not-found.js in the app folder (or pages/404.js) replaces the default page.');
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The page shows the error page of an error boundary with the given words,
|
|
94
|
+
* and an error did happen: the document answered with a server error, the
|
|
95
|
+
* console reported the production error of a failed server render, a request
|
|
96
|
+
* of the page failed on the server, or the page raised an error. With "not",
|
|
97
|
+
* the words are not on the page. Error pages carry @js-off in a feature.
|
|
98
|
+
*
|
|
99
|
+
* Example #1: Then the error page should show "Something went wrong!"
|
|
100
|
+
* Example #2: And the error page should show "Try again"
|
|
101
|
+
* Example #3: Then the error page should not show "Something went wrong!"
|
|
102
|
+
* Example #4: Then error page should show "Something went wrong!"
|
|
103
|
+
* Example #5: And the error page should not show "Application error"
|
|
104
|
+
*/
|
|
105
|
+
Then(/^(?:the )?error page should( not)* show "([^"]*)?"$/, async function (notCase, text) {
|
|
106
|
+
await pageReady(this);
|
|
107
|
+
const visible = () => this.page.getByText(text, { exact: false }).filter({ visible: true }).count().then((n) => n > 0).catch(() => false);
|
|
108
|
+
if (notCase) {
|
|
109
|
+
if (await visible()) {
|
|
110
|
+
throw failure(this, `Expected no error page with "${text}", but the page shows it (${shortUrl(this.page.url())}).`, 'An error boundary shows its page when a part of the page fails to render.');
|
|
111
|
+
}
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
await eventually(this, visible, (ok) => ok, () => ({
|
|
115
|
+
message: `Expected the error page to show "${text}", but it is not on the page (${shortUrl(this.page.url())}).`,
|
|
116
|
+
hint: 'The words are those of the app\'s error.js (or the framework\'s default "Application error" page).',
|
|
117
|
+
}));
|
|
118
|
+
const status = lastDocumentStatus(this.page);
|
|
119
|
+
const state = requests(this.page);
|
|
120
|
+
const failedAnswers = state.answers.filter((a) => a.status >= 500);
|
|
121
|
+
const rendered = serverRenderErrors(this.page);
|
|
122
|
+
const raised = consoleOf(this.page).filter((e) => e.type === 'pageerror');
|
|
123
|
+
if (status < 500 && !failedAnswers.length && !rendered.length && !raised.length) {
|
|
124
|
+
throw failure(this, `The page shows "${text}", but nothing on it failed: the document answered ${status || 'with no status'}, no request failed on the server and the console reported no error.`, 'The words are on the page without an error behind them: the step expects the error page of an error that happened.');
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Whether the page shows the details of an error to the visitor: a stack
|
|
130
|
+
* trace, or the error overlay of the development server. A production build
|
|
131
|
+
* keeps them to itself (the page and the console get a digest instead).
|
|
132
|
+
*
|
|
133
|
+
* Example #1: Then the page should not show the details of the error
|
|
134
|
+
* Example #2: And the page should not show the details of the error
|
|
135
|
+
* Example #3: Then the page should show the details of the error
|
|
136
|
+
* Example #4: Then page should not show the details of the error
|
|
137
|
+
* Example #5: And the page should show the details of the error
|
|
138
|
+
*/
|
|
139
|
+
Then(/^(?:the )?page should( not)* show the details of the error$/, async function (notCase) {
|
|
140
|
+
await pageReady(this);
|
|
141
|
+
const read = async () => {
|
|
142
|
+
const overlay = await devOverlay(this);
|
|
143
|
+
const stack = await this.page.evaluate(() => {
|
|
144
|
+
const text = document.body ? document.body.innerText || '' : '';
|
|
145
|
+
const m = text.match(/^\s+at .+\(?.+:\d+:\d+\)?$/m) || text.match(/\b[\w./-]+\.(?:js|ts|tsx|jsx):\d+:\d+\b/);
|
|
146
|
+
return m ? m[0].trim().slice(0, 120) : '';
|
|
147
|
+
}).catch(() => '');
|
|
148
|
+
return { overlay: overlay.open ? `the development error overlay ("${overlay.label}: ${overlay.message.slice(0, 80)}")` : '', stack };
|
|
149
|
+
};
|
|
150
|
+
if (notCase) {
|
|
151
|
+
const seen = await read();
|
|
152
|
+
if (seen.overlay || seen.stack) {
|
|
153
|
+
throw failure(this, `Expected the page to keep the details of the error to itself, but it shows ${seen.overlay || `a stack trace ("${seen.stack}")`}.`, 'A production build shows the error page and keeps the message and the stack on the server; the development server shows them on purpose.');
|
|
154
|
+
}
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
await eventually(this, read, (seen) => !!(seen.overlay || seen.stack), () => ({
|
|
158
|
+
message: `Expected the page to show the details of the error, but it shows neither a stack trace nor the development error overlay (${shortUrl(this.page.url())}).`,
|
|
159
|
+
hint: 'Only the development server shows the details of an error.',
|
|
160
|
+
}));
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Whether the development server's error overlay is shown over the page (a
|
|
165
|
+
* dialog of the development tools). A production build never shows it.
|
|
166
|
+
*
|
|
167
|
+
* Example #1: Then the development error overlay should be shown
|
|
168
|
+
* Example #2: Then the development error overlay should not be shown
|
|
169
|
+
* Example #3: And the development error overlay should be shown
|
|
170
|
+
* Example #4: And development error overlay should not be shown
|
|
171
|
+
* Example #5: Then the development error overlay should not be shown
|
|
172
|
+
*/
|
|
173
|
+
Then(/^(?:the )?development error overlay should( not)* be shown$/, async function (notCase) {
|
|
174
|
+
if (notCase) {
|
|
175
|
+
await pageReady(this);
|
|
176
|
+
const overlay = await devOverlay(this);
|
|
177
|
+
if (overlay.open) {
|
|
178
|
+
throw failure(this, `Expected no development error overlay, but it shows "${overlay.label}: ${overlay.message.slice(0, 120)}" (${overlay.count} error(s)).`, 'The overlay shows an error of the page or of its server render: read it, or look at the console.');
|
|
179
|
+
}
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const info = await appInfo(this);
|
|
183
|
+
await eventually(this, () => devOverlay(this), (o) => o.open, (o) => ({
|
|
184
|
+
message: `Expected the development error overlay, but ${o.present ? 'the development tools show no error' : 'the page has no development tools'} (${shortUrl(this.page.url())}).`,
|
|
185
|
+
hint: info.development || o.present ? 'The overlay opens for an error of the page or of its server render.' : 'Only the development server (next dev) shows it: a production build has no development tools.',
|
|
186
|
+
}), budget(this) * 2);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The words of the development error overlay: its title (the kind of error)
|
|
191
|
+
* and its message contain them.
|
|
192
|
+
*
|
|
193
|
+
* Example #1: Then the development error overlay should show "invalid input syntax for type uuid"
|
|
194
|
+
* Example #2: And the development error overlay should show "PostgresError"
|
|
195
|
+
* Example #3: Then development error overlay should show "Failed to fetch invoice."
|
|
196
|
+
* Example #4: And the development error overlay should show "Server"
|
|
197
|
+
* Example #5: Then the development error overlay should show "invalid input syntax"
|
|
198
|
+
*/
|
|
199
|
+
Then(/^(?:the )?development error overlay should show "([^"]*)?"$/, async function (text) {
|
|
200
|
+
await eventually(this, () => devOverlay(this), (o) => o.open && o.text.toLowerCase().includes(String(text).toLowerCase()), (o) => ({
|
|
201
|
+
message: o.open
|
|
202
|
+
? `Expected the development error overlay to show "${text}", but it shows "${o.label}: ${o.message.slice(0, 160)}".`
|
|
203
|
+
: `Expected the development error overlay to show "${text}", but no overlay is shown (${shortUrl(this.page.url())}).`,
|
|
204
|
+
hint: 'The overlay shows one error at a time; its header counts them.',
|
|
205
|
+
}), budget(this) * 2);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* How many errors the development error overlay lists (the count in its
|
|
210
|
+
* header).
|
|
211
|
+
*
|
|
212
|
+
* Example #1: Then the development error overlay should list 2 errors
|
|
213
|
+
* Example #2: And the development error overlay should list 1 error
|
|
214
|
+
* Example #3: Then development error overlay should list 3 errors
|
|
215
|
+
* Example #4: And the development error overlay should list 2 errors
|
|
216
|
+
* Example #5: Then the development error overlay should list 1 error
|
|
217
|
+
*/
|
|
218
|
+
Then(/^(?:the )?development error overlay should list (\d+) errors?$/, async function (count) {
|
|
219
|
+
const wanted = Number(count);
|
|
220
|
+
await eventually(this, () => devOverlay(this), (o) => o.open && o.count === wanted, (o) => ({
|
|
221
|
+
message: o.open ? `Expected the development error overlay to list ${wanted} error(s), but it lists ${o.count}.` : `Expected the development error overlay to list ${wanted} error(s), but no overlay is shown.`,
|
|
222
|
+
hint: 'The overlay counts the errors of the page and of its server render; one failure can log more than one.',
|
|
223
|
+
}), budget(this) * 2);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Whether the development tools of the development server are on the page
|
|
228
|
+
* (their indicator in a corner). A production build has none.
|
|
229
|
+
*
|
|
230
|
+
* Example #1: Then the development tools should be on the page
|
|
231
|
+
* Example #2: Then the development tools should not be on the page
|
|
232
|
+
* Example #3: And the development tools should not be on the page
|
|
233
|
+
* Example #4: And development tools should be on the page
|
|
234
|
+
* Example #5: Then the development tools should not be on the page
|
|
235
|
+
*/
|
|
236
|
+
Then(/^(?:the )?development tools should( not)* be on the page$/, async function (notCase) {
|
|
237
|
+
await pageReady(this);
|
|
238
|
+
if (notCase) {
|
|
239
|
+
const o = await devOverlay(this);
|
|
240
|
+
if (o.present) {
|
|
241
|
+
throw failure(this, `Expected no development tools on the page, but they are there (${shortUrl(this.page.url())}).`, 'The development server (next dev) puts them on every page; a production build has none.');
|
|
242
|
+
}
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
await eventually(this, () => devOverlay(this), (o) => o.present && o.indicator, () => ({
|
|
246
|
+
message: `Expected the development tools on the page, but there are none (${shortUrl(this.page.url())}).`,
|
|
247
|
+
hint: 'Only the development server (next dev) puts them on the page.',
|
|
248
|
+
}));
|
|
249
|
+
});
|
|
250
|
+
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Forms and their actions: a form sent with its button (the step waits for
|
|
4
|
+
// the form's action on the server and the page that follows), the errors the
|
|
5
|
+
// server sends back next to each field and for the whole form, a button that
|
|
6
|
+
// says the form is on its way, and a form handled in the page (no full page
|
|
7
|
+
// reload).
|
|
8
|
+
|
|
9
|
+
const { Then, When } = require('@cucumber/cucumber');
|
|
10
|
+
const {
|
|
11
|
+
budget,
|
|
12
|
+
same,
|
|
13
|
+
listed,
|
|
14
|
+
failure,
|
|
15
|
+
shortUrl,
|
|
16
|
+
requests,
|
|
17
|
+
documentId,
|
|
18
|
+
pageReady,
|
|
19
|
+
afterAction,
|
|
20
|
+
personClick,
|
|
21
|
+
firstPresent,
|
|
22
|
+
formMessages,
|
|
23
|
+
eventually,
|
|
24
|
+
} = require('./nextjs-helpers');
|
|
25
|
+
|
|
26
|
+
/** A button by its name (exact, then containing the words), the visible one first. */
|
|
27
|
+
async function buttonByName(world, name, scope) {
|
|
28
|
+
const root = scope || world.page;
|
|
29
|
+
return firstPresent([
|
|
30
|
+
root.getByRole('button', { name, exact: true }).filter({ visible: true }),
|
|
31
|
+
root.getByRole('button', { name }).filter({ visible: true }),
|
|
32
|
+
root.locator(`input[type="submit"][value="${String(name).replace(/"/g, '\\"')}"]`).filter({ visible: true }),
|
|
33
|
+
root.getByRole('button', { name, exact: true }),
|
|
34
|
+
]);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The visible row of a table (or of a grid) that holds the text. */
|
|
38
|
+
async function rowWith(world, text) {
|
|
39
|
+
return firstPresent([
|
|
40
|
+
world.page.locator('tr, [role="row"]').filter({ hasText: text }).filter({ visible: true }),
|
|
41
|
+
world.page.locator('tr, [role="row"]').filter({ hasText: text }),
|
|
42
|
+
]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The names of the visible buttons of the page, for a failure message. */
|
|
46
|
+
async function buttonNames(world) {
|
|
47
|
+
return world.page.evaluate(() => [...document.querySelectorAll('button, input[type="submit"], [role="button"]')]
|
|
48
|
+
.filter((b) => b.offsetWidth || b.offsetHeight)
|
|
49
|
+
.map((b) => (b.getAttribute('aria-label') || b.textContent || b.value || '').replace(/\s+/g, ' ').trim())
|
|
50
|
+
.filter(Boolean)
|
|
51
|
+
.slice(0, 15)).catch(() => []);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The fields of a form the browser holds back (its own validation), with their messages. */
|
|
55
|
+
async function invalidFields(form) {
|
|
56
|
+
return form.evaluate((f) => [...f.querySelectorAll('input, select, textarea')]
|
|
57
|
+
.filter((el) => el.willValidate && !el.checkValidity())
|
|
58
|
+
.map((el) => {
|
|
59
|
+
const label = (el.labels && el.labels[0] && el.labels[0].textContent) || el.getAttribute('aria-label') || el.name || el.id;
|
|
60
|
+
return `"${String(label).replace(/\s+/g, ' ').trim()}" (${el.validationMessage})`;
|
|
61
|
+
})).catch(() => []);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Send a form with its button, by the button's name: the step waits for the
|
|
66
|
+
* button to be hydrated, clicks it, and waits for the form's action on the
|
|
67
|
+
* server and for the page that follows (the same page with its messages, or
|
|
68
|
+
* the page the action sends the browser to). It fails when the browser holds
|
|
69
|
+
* the form back (a required field left empty, an address that is not one).
|
|
70
|
+
*
|
|
71
|
+
* With "in the "…" row", the button is the one of the table row that holds
|
|
72
|
+
* that text (a "Delete" button on each row of a list).
|
|
73
|
+
*
|
|
74
|
+
* Example #1: When I send the form with "Create Invoice"
|
|
75
|
+
* Example #2: And we send the form with "Edit Invoice"
|
|
76
|
+
* Example #3: When I send the form with "Log in"
|
|
77
|
+
* Example #4: When I send the form with "Delete" in the "$42.17" row
|
|
78
|
+
* Example #5: And I send the form with "Delete" in the "Oak Example" row
|
|
79
|
+
*/
|
|
80
|
+
When(/^(I |we )*send the form with "([^"]*)?"(?: in the "([^"]*)?" row)?$/, { timeout: 60000 }, async function (pronounCase, name, rowText) {
|
|
81
|
+
await pageReady(this);
|
|
82
|
+
let scope = null;
|
|
83
|
+
if (rowText) {
|
|
84
|
+
scope = await rowWith(this, rowText);
|
|
85
|
+
if (!(await scope.count())) {
|
|
86
|
+
throw failure(this, `Expected a row with "${rowText}" on ${this.page.url()}, but no row of a table holds it.`, 'The row is a table row (tr) or a row of a grid (role="row") whose text holds the words.');
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const button = await buttonByName(this, name, scope);
|
|
90
|
+
if (!(await button.count())) {
|
|
91
|
+
throw failure(this, `Expected a button "${name}"${rowText ? ` in the "${rowText}" row` : ''} on ${this.page.url()}, but there is none. Buttons on the page: ${listed(await buttonNames(this))}.`, 'A button is found by the name a person reads: its text, its value or its accessible name.');
|
|
92
|
+
}
|
|
93
|
+
const form = button.locator('xpath=ancestor::form[1]');
|
|
94
|
+
const state = requests(this.page);
|
|
95
|
+
const actionsBefore = state.started.action;
|
|
96
|
+
const navigationsBefore = state.started.navigation + state.started.data;
|
|
97
|
+
const documentsBefore = state.documents.length;
|
|
98
|
+
const documentBefore = await documentId(this);
|
|
99
|
+
const urlBefore = this.page.url();
|
|
100
|
+
await afterAction(this, () => personClick(this, button, `the button "${name}"`));
|
|
101
|
+
const sent = state.started.action > actionsBefore || state.documents.length > documentsBefore || state.started.navigation + state.started.data > navigationsBefore || this.page.url() !== urlBefore;
|
|
102
|
+
if (!sent && (await form.count())) {
|
|
103
|
+
const invalid = await invalidFields(form);
|
|
104
|
+
if (invalid.length) {
|
|
105
|
+
throw failure(this, `The form of "${name}" was not sent: the browser held it back. ${invalid.join('; ')}.`, 'Fill in the fields the browser asks for, or test the app\'s own messages with a value the browser accepts.');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
this.__lastForm = {
|
|
109
|
+
name,
|
|
110
|
+
sentByAction: state.started.action > actionsBefore,
|
|
111
|
+
newDocument: state.documents.length > documentsBefore || (await documentId(this)) !== documentBefore,
|
|
112
|
+
url: this.page.url(),
|
|
113
|
+
answers: state.answers.filter((a) => a.kind === 'action').slice(-3),
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The last form sent was handled in the page: its action ran on the server
|
|
119
|
+
* through the app's own request, and the browser kept its document (no full
|
|
120
|
+
* page reload, as a plain HTML form would do).
|
|
121
|
+
*
|
|
122
|
+
* Example #1: Then the form should have been sent without a full page reload
|
|
123
|
+
* Example #2: And the form should have been sent without a full page reload
|
|
124
|
+
* Example #3: Then form should have been sent without a full page reload
|
|
125
|
+
* Example #4: And form should have been sent without a full page reload
|
|
126
|
+
* Example #5: Then the form should have been sent without a full page reload
|
|
127
|
+
*/
|
|
128
|
+
Then(/^(?:the )?form should have been sent without a full page reload$/, async function () {
|
|
129
|
+
const last = this.__lastForm;
|
|
130
|
+
if (!last) {
|
|
131
|
+
throw failure(this, 'Expected a form sent in this scenario, but none was: "When I send the form with "…"" comes first.', 'This step reads what the last "send the form" step saw.');
|
|
132
|
+
}
|
|
133
|
+
if (last.newDocument) {
|
|
134
|
+
throw failure(this, `Expected the form of "${last.name}" to be sent without a full page reload, but the browser loaded a new document (${shortUrl(last.url)}).`, 'A form the app had not hydrated yet posts as plain HTML (and still works, by design): wait for the page to be ready before sending it, and look at the console for an error.');
|
|
135
|
+
}
|
|
136
|
+
if (!last.sentByAction) {
|
|
137
|
+
throw failure(this, `Expected the form of "${last.name}" to run its action on the server, but no action request was sent.`, 'A form with a server action sends a request with the header next-action; a form handled only in the browser sends none.');
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The control a person names by its label (or a group of radio buttons or
|
|
143
|
+
* check boxes by its legend), and the texts that describe it: the elements its
|
|
144
|
+
* aria-describedby and aria-errormessage name, and the live regions of its
|
|
145
|
+
* group.
|
|
146
|
+
*/
|
|
147
|
+
async function fieldAndErrors(world, label) {
|
|
148
|
+
const page = world.page;
|
|
149
|
+
const control = await firstPresent([
|
|
150
|
+
page.getByLabel(label, { exact: true }).filter({ visible: true }),
|
|
151
|
+
page.getByRole('group', { name: label, exact: true }),
|
|
152
|
+
page.getByLabel(label).filter({ visible: true }),
|
|
153
|
+
page.getByRole('group', { name: label }),
|
|
154
|
+
]);
|
|
155
|
+
if (!(await control.count())) return null;
|
|
156
|
+
const texts = await control.evaluate((el) => {
|
|
157
|
+
const read = (ids) => String(ids || '').split(/\s+/).filter(Boolean)
|
|
158
|
+
.map((id) => document.getElementById(id))
|
|
159
|
+
.filter(Boolean)
|
|
160
|
+
.map((n) => (n.textContent || '').replace(/\s+/g, ' ').trim());
|
|
161
|
+
const out = [...read(el.getAttribute('aria-describedby')), ...read(el.getAttribute('aria-errormessage'))];
|
|
162
|
+
const group = el.matches('fieldset, [role="group"], [role="radiogroup"]') ? el : null;
|
|
163
|
+
if (group) {
|
|
164
|
+
for (const n of group.querySelectorAll('[aria-live], [role="alert"]')) out.push((n.textContent || '').replace(/\s+/g, ' ').trim());
|
|
165
|
+
for (const input of group.querySelectorAll('input, select, textarea')) out.push(...read(input.getAttribute('aria-describedby')), ...read(input.getAttribute('aria-errormessage')));
|
|
166
|
+
}
|
|
167
|
+
return out.filter(Boolean);
|
|
168
|
+
});
|
|
169
|
+
return { control, texts };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The error a field shows: the text of the element its aria-describedby (or
|
|
174
|
+
* aria-errormessage) names, as a screen reader reads it with the field; for a
|
|
175
|
+
* group of radio buttons named by its legend, the messages of the group. The
|
|
176
|
+
* step waits for the error to appear ("should not" for it to go).
|
|
177
|
+
*
|
|
178
|
+
* Example #1: Then the field "Choose customer" should show the error "Please select a customer."
|
|
179
|
+
* Example #2: And the field "Choose an amount" should show the error "Please enter an amount greater than $0."
|
|
180
|
+
* Example #3: And the field "Set the invoice status" should show the error "Please select an invoice status."
|
|
181
|
+
* Example #4: Then the field "Choose customer" should not show the error "Please select a customer."
|
|
182
|
+
* Example #5: Then field "Choose an amount" should not show the error "Please enter an amount greater than $0."
|
|
183
|
+
*/
|
|
184
|
+
Then(/^(?:the )?field "([^"]*)?" should( not)* show the error "([^"]*)?"$/, async function (label, notCase, error) {
|
|
185
|
+
await pageReady(this);
|
|
186
|
+
const read = () => fieldAndErrors(this, label);
|
|
187
|
+
const has = (found) => !!found && found.texts.some((t) => same(t, error) || t.toLowerCase().includes(String(error).toLowerCase()));
|
|
188
|
+
const first = await read();
|
|
189
|
+
if (!first) {
|
|
190
|
+
throw failure(this, `Expected a field "${label}" on ${this.page.url()}, but there is none.`, 'A field is found by its label (a <label for>, aria-label or aria-labelledby); a group of radio buttons by its <legend>.');
|
|
191
|
+
}
|
|
192
|
+
if (notCase) {
|
|
193
|
+
if (has(first)) {
|
|
194
|
+
throw failure(this, `Expected the field "${label}" not to show the error "${error}", but it does.`, 'The error is the text its aria-describedby names.');
|
|
195
|
+
}
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
await eventually(this, read, has, (found) => ({
|
|
199
|
+
message: `Expected the field "${label}" to show the error "${error}", but it shows ${listed(found ? found.texts : [])}.`,
|
|
200
|
+
hint: 'The step reads the texts the field\'s aria-describedby (or aria-errormessage) names, as a screen reader does. An error shown next to the field but not tied to it is not announced with the field: tie it with aria-describedby.',
|
|
201
|
+
}));
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* A message of the form as a whole, in one of its live regions (aria-live, an
|
|
206
|
+
* alert, a status): what the form's action sent back. The step waits for it
|
|
207
|
+
* to appear ("should not" for it to go).
|
|
208
|
+
*
|
|
209
|
+
* Example #1: Then the form should show the message "Missing Fields. Failed to Create Invoice."
|
|
210
|
+
* Example #2: And the form should show the message "Invalid credentials."
|
|
211
|
+
* Example #3: Then the form should not show the message "Invalid credentials."
|
|
212
|
+
* Example #4: Then form should show the message "Missing Fields. Failed to Update Invoice."
|
|
213
|
+
* Example #5: And the form should not show the message "Missing Fields. Failed to Create Invoice."
|
|
214
|
+
*/
|
|
215
|
+
Then(/^(?:the )?form should( not)* show the message "([^"]*)?"$/, async function (notCase, message) {
|
|
216
|
+
await pageReady(this);
|
|
217
|
+
const read = async () => {
|
|
218
|
+
const forms = this.page.locator('form').filter({ visible: true });
|
|
219
|
+
const count = await forms.count();
|
|
220
|
+
const all = [];
|
|
221
|
+
for (let i = 0; i < count; i += 1) all.push(...(await formMessages(this, forms.nth(i))));
|
|
222
|
+
return all;
|
|
223
|
+
};
|
|
224
|
+
const has = (messages) => messages.some((m) => same(m, message) || m.toLowerCase().includes(String(message).toLowerCase()));
|
|
225
|
+
if (notCase) {
|
|
226
|
+
const messages = await read();
|
|
227
|
+
if (has(messages)) {
|
|
228
|
+
throw failure(this, `Expected no message "${message}" in a form, but a form shows it.`, 'The message is in a live region of the form (aria-live, role="alert" or role="status").');
|
|
229
|
+
}
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
await eventually(this, read, has, (messages) => ({
|
|
233
|
+
message: `Expected a form to show the message "${message}", but its messages are ${listed(messages)}.`,
|
|
234
|
+
hint: 'The step reads the live regions of the forms of the page (aria-live, role="alert", role="status"): the places a screen reader announces. A message outside them is not announced.',
|
|
235
|
+
}));
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Whether a button says that its form is on its way: disabled, aria-disabled
|
|
240
|
+
* or aria-busy while the form's action runs (the pending state of the form).
|
|
241
|
+
* "should be busy" waits for it; "should not be busy" waits for it to end.
|
|
242
|
+
*
|
|
243
|
+
* Example #1: Then the "Log in" button should be busy
|
|
244
|
+
* Example #2: And the "Log in" button should not be busy
|
|
245
|
+
* Example #3: Then the "Create Invoice" button should not be busy
|
|
246
|
+
* Example #4: Then "Log in" button should be busy
|
|
247
|
+
* Example #5: And the "Edit Invoice" button should not be busy
|
|
248
|
+
*/
|
|
249
|
+
Then(/^(?:the )?"([^"]*)?" button should( not)* be busy$/, async function (name, notCase) {
|
|
250
|
+
const button = await buttonByName(this, name);
|
|
251
|
+
if (!(await button.count())) {
|
|
252
|
+
throw failure(this, `Expected a button "${name}" on ${this.page.url()}, but there is none. Buttons on the page: ${listed(await buttonNames(this))}.`, 'A button is found by the name a person reads.');
|
|
253
|
+
}
|
|
254
|
+
const read = () => button.evaluate((b) => ({
|
|
255
|
+
disabled: !!b.disabled,
|
|
256
|
+
ariaDisabled: b.getAttribute('aria-disabled') || '',
|
|
257
|
+
ariaBusy: b.getAttribute('aria-busy') || '',
|
|
258
|
+
})).catch(() => null);
|
|
259
|
+
const busy = (s) => !!s && (s.disabled || s.ariaDisabled === 'true' || s.ariaBusy === 'true');
|
|
260
|
+
await eventually(this, read, (s) => (notCase ? !busy(s) : busy(s)), (s) => ({
|
|
261
|
+
message: `Expected the "${name}" button ${notCase ? 'not to be busy' : 'to be busy'}, but it is ${s ? `disabled: ${s.disabled}, aria-disabled: "${s.ariaDisabled}", aria-busy: "${s.ariaBusy}"` : 'gone'}.`,
|
|
262
|
+
hint: notCase
|
|
263
|
+
? 'The button stays busy while the form\'s action runs: hold-back answers come through with "When the server\'s answers come through".'
|
|
264
|
+
: 'A form says it is busy only while its action runs, and only if the app marks its button (disabled, aria-disabled or aria-busy, from the form\'s pending state). Hold the answers back first so the step can see it.',
|
|
265
|
+
}), budget(this));
|
|
266
|
+
});
|