@webship-js/django-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.
@@ -0,0 +1,688 @@
1
+ 'use strict';
2
+
3
+ // The change list: its rows and columns, the search, the filters of the
4
+ // sidebar (and their counts), the date drill-down, sorting by a column,
5
+ // pagination, the selection of rows (on a page, across all pages) and the
6
+ // actions run on them.
7
+
8
+ const { When, Then } = require('@cucumber/cucumber');
9
+ const { friendly } = require('webship-js/tests/step-definitions/webship');
10
+ const {
11
+ clean,
12
+ normal,
13
+ listed,
14
+ escapeRegExp,
15
+ hintFor,
16
+ afterNavigation,
17
+ pageSummary,
18
+ eventually,
19
+ changeListOrFail,
20
+ listRows,
21
+ listColumns,
22
+ listCell,
23
+ listCounts,
24
+ listPages,
25
+ filterBlock,
26
+ filterTitles,
27
+ } = require('./django-helpers');
28
+
29
+ /** The object texts of the rows on screen. */
30
+ async function shownObjects(page) {
31
+ return (await listRows(page)).map((r) => r.object);
32
+ }
33
+
34
+ /**
35
+ * Search the change list with its search box (the model's search fields), as
36
+ * a person does: type, then "Search".
37
+ *
38
+ * Example #1: When I search the list for "train"
39
+ * Example #2: When we search the list for "Which"
40
+ * Example #3: And I search the list for "editor"
41
+ * Example #4: When I search the list for ""
42
+ * Example #5: And we search the list for "no such question at all"
43
+ */
44
+ When(/^(I |we )*search the list for "([^"]*)"$/, async function (pronounCase, text) {
45
+ await changeListOrFail(this);
46
+ const page = this.page;
47
+ const box = page.locator('#searchbar');
48
+ if (!(await box.count())) {
49
+ throw friendly({ message: 'The list has no search box.', hint: 'The admin draws the search box when the model\'s admin has search_fields.' });
50
+ }
51
+ await box.fill(text || '');
52
+ await afterNavigation(this, () => page.locator('#changelist-search [type="submit"]').first().click());
53
+ });
54
+
55
+ /**
56
+ * Assert what the last search found: the count the list writes beside the
57
+ * search box ("2 results (24 total)"), with the total when the step gives it.
58
+ *
59
+ * Example #1: Then the search should find 2 results
60
+ * Example #2: Then the search should find 2 results of 24
61
+ * Example #3: And the search should find 1 result
62
+ * Example #4: Then the search should find 0 results of 24
63
+ * Example #5: And the search should find 12 results
64
+ */
65
+ Then(/^the search should find (\d+) results?(?: of (\d+))?$/, async function (found, total) {
66
+ await changeListOrFail(this);
67
+ const counts = await listCounts(this.page);
68
+ if (counts.results !== parseInt(found, 10) || (total && counts.total !== parseInt(total, 10))) {
69
+ throw friendly({
70
+ message: counts.results === null
71
+ ? `The list shows no search count: ${await pageSummary(this)}.`
72
+ : `The search found ${counts.results} result${counts.results === 1 ? '' : 's'} of ${counts.total} ("${counts.searchText}"), not ${found}${total ? ` of ${total}` : ''}.`,
73
+ hint: 'Search first: When I search the list for "train"',
74
+ });
75
+ }
76
+ });
77
+
78
+ /**
79
+ * Assert that the change list shows (or does not show) a row, by the object's
80
+ * text in its first column (the link to its page).
81
+ *
82
+ * Example #1: Then the list should show "Which river is the longest?"
83
+ * Example #2: Then the list should not show "What's up?"
84
+ * Example #3: And the list should show "editor"
85
+ * Example #4: Then the list should show "Do you travel by train or by plane?"
86
+ * Example #5: And the list should not show "Which train journey is the most beautiful?"
87
+ */
88
+ Then(/^the list should( not)* show "([^"]*)"$/, async function (notCase, object) {
89
+ await changeListOrFail(this);
90
+ const shown = await shownObjects(this.page);
91
+ const has = shown.some((o) => normal(o) === normal(object));
92
+ if (has === !!notCase) {
93
+ throw friendly({
94
+ message: notCase ? `The list shows "${object}".` : `The list does not show "${object}" on this page. It shows: ${listed(shown)}.`,
95
+ hint: 'A row is named by the text of its first column. The list shows one page at a time: search for the row, or go to its page.',
96
+ });
97
+ }
98
+ });
99
+
100
+ /**
101
+ * Assert how many rows the change list shows on its current page.
102
+ *
103
+ * Example #1: Then the list should have 10 rows
104
+ * Example #2: Then the list should have 2 rows
105
+ * Example #3: And the list should have 1 row
106
+ * Example #4: Then the list should have 4 rows
107
+ * Example #5: And the list should have 0 rows
108
+ */
109
+ Then(/^the list should have (\d+) rows?$/, async function (expected) {
110
+ await changeListOrFail(this);
111
+ const shown = await shownObjects(this.page);
112
+ if (shown.length !== parseInt(expected, 10)) {
113
+ throw friendly({ message: `The list has ${shown.length} row${shown.length === 1 ? '' : 's'} on this page, not ${expected}: ${listed(shown)}.`, hint: 'The list shows a page of rows at a time (list_per_page).' });
114
+ }
115
+ });
116
+
117
+ /**
118
+ * Assert the count the change list gives below its rows for all its pages
119
+ * ("24 questions"): the number, and the name the list counts in.
120
+ *
121
+ * Example #1: Then the list should count 24 questions
122
+ * Example #2: Then the list should count 1 question
123
+ * Example #3: And the list should count 3 users
124
+ * Example #4: Then the list should count 12 questions
125
+ * Example #5: And the list should count 0 questions
126
+ */
127
+ Then(/^the list should count (\d+) ([^"]+)$/, async function (expected, noun) {
128
+ await changeListOrFail(this);
129
+ const counts = await listCounts(this.page);
130
+ const said = normal(counts.paginator);
131
+ if (counts.count !== parseInt(expected, 10) || !said.includes(normal(`${expected} ${noun}`))) {
132
+ throw friendly({ message: `The list counts "${counts.paginator || '(nothing)'}", not "${expected} ${noun}".`, hint: 'The count is the line below the rows: the number and the model\'s name.' });
133
+ }
134
+ });
135
+
136
+ /**
137
+ * Assert the headings of the change list's columns, in order (the column of
138
+ * the selection boxes left out).
139
+ *
140
+ * Example #1: Then the list columns should be "Question text", "Date published", "Category", "Published recently?"
141
+ * Example #2: Then the list columns should be "Username", "Email address", "First name", "Last name", "Staff status"
142
+ * Example #3: And the list columns should be "Name"
143
+ * Example #4: Then the list columns should be "Category"
144
+ * Example #5: And the list columns should be "Tag"
145
+ */
146
+ Then(/^the list columns should be ("[^"]*"(?:,\s*"[^"]*")*)$/, async function (spec) {
147
+ await changeListOrFail(this);
148
+ const want = [...spec.matchAll(/"([^"]*)"/g)].map((m) => m[1]);
149
+ const columns = await listColumns(this.page);
150
+ if (columns.length !== want.length || !columns.every((c, i) => normal(c) === normal(want[i]))) {
151
+ throw friendly({ message: `The list columns are ${listed(columns)}, not ${listed(want)}.`, hint: 'The columns are the model admin\'s list_display, in its order.' });
152
+ }
153
+ });
154
+
155
+ /**
156
+ * Assert the text of one cell of the change list: the row of an object, the
157
+ * column of a heading. A yes/no column shows an icon: write "yes" or "no".
158
+ *
159
+ * Example #1: Then the "Which river is the longest?" row should show "Science" in the "Category" column
160
+ * Example #2: Then the "editor" row should show "editor@example.com" in the "Email address" column
161
+ * Example #3: And the "viewer" row should show "yes" in the "Staff status" column
162
+ * Example #4: Then the "What's up?" row should show "no" in the "Published recently?" column
163
+ * Example #5: And the "What's up?" row should show "Jan. 6, 2025, 9 a.m." in the "Date published" column
164
+ */
165
+ Then(/^the "([^"]*)" row should show "([^"]*)" in the "([^"]*)" column$/, async function (object, expected, column) {
166
+ await changeListOrFail(this);
167
+ const cell = await listCell(this.page, object, column);
168
+ if (cell.error === 'column') {
169
+ throw friendly({ message: `The list has no column "${column}". Its columns: ${listed(cell.columns)}.`, hint: 'Name the column by its heading.' });
170
+ }
171
+ if (cell.error === 'row') {
172
+ throw friendly({ message: `The list has no row "${object}" on this page. It shows: ${listed(cell.rows)}.`, hint: 'Search for the row, or go to its page, first.' });
173
+ }
174
+ const yesNo = { yes: 'true', no: 'false', true: 'true', false: 'false' };
175
+ const got = normal(cell.text);
176
+ const want = normal(expected);
177
+ const same = got === want || (yesNo[want] && got === yesNo[want]);
178
+ if (!same) {
179
+ throw friendly({ message: `The "${object}" row shows "${cell.text}" in the "${column}" column, not "${expected}".`, hint: 'A yes/no column shows an icon: write "yes" or "no" for it.' });
180
+ }
181
+ });
182
+
183
+ /**
184
+ * Assert which row comes first, or last, on the current page of the list.
185
+ *
186
+ * Example #1: Then the first row of the list should be "What's up?"
187
+ * Example #2: Then the last row of the list should be "Which breakfast keeps you going?"
188
+ * Example #3: And the first row of the list should be "administrator"
189
+ * Example #4: Then the first row of the list should be "Which breakfast keeps you going?"
190
+ * Example #5: And the last row of the list should be "viewer"
191
+ */
192
+ Then(/^the (first|last) row of the list should be "([^"]*)"$/, async function (which, object) {
193
+ await changeListOrFail(this);
194
+ const shown = await shownObjects(this.page);
195
+ const row = which === 'first' ? shown[0] : shown[shown.length - 1];
196
+ if (normal(row || '') !== normal(object)) {
197
+ throw friendly({ message: `The ${which} row of the list is "${row || '(no row)'}", not "${object}". The rows: ${listed(shown)}.`, hint: 'The order follows the sorting of the list.' });
198
+ }
199
+ });
200
+
201
+ /**
202
+ * Assert that one row comes before another on the current page of the list.
203
+ *
204
+ * Example #1: Then "What's up?" should come before "Which fruit do you eat most?" in the list
205
+ * Example #2: Then "administrator" should come before "viewer" in the list
206
+ * Example #3: And "Which river is the longest?" should come before "Which season do you like best?" in the list
207
+ * Example #4: Then "Which breakfast keeps you going?" should come before "What's up?" in the list
208
+ * Example #5: And "editor" should come before "viewer" in the list
209
+ */
210
+ Then(/^"([^"]*)" should come before "([^"]*)" in the list$/, async function (first, second) {
211
+ await changeListOrFail(this);
212
+ const shown = await shownObjects(this.page);
213
+ const a = shown.findIndex((o) => normal(o) === normal(first));
214
+ const b = shown.findIndex((o) => normal(o) === normal(second));
215
+ if (a < 0 || b < 0 || a >= b) {
216
+ throw friendly({
217
+ message: a < 0 || b < 0
218
+ ? `The list does not show ${a < 0 ? `"${first}"` : `"${second}"`} on this page. It shows: ${listed(shown)}.`
219
+ : `"${first}" comes after "${second}" in the list: ${listed(shown)}.`,
220
+ hint: 'Both rows must be on the page the list shows.',
221
+ });
222
+ }
223
+ });
224
+
225
+ /** The heading cell of a sortable column, by its text. */
226
+ function columnHeading(page, column) {
227
+ return page.locator('#result_list thead th').filter({ hasText: new RegExp('^\\s*' + escapeRegExp(clean(column)) + '\\s*$', 'i') });
228
+ }
229
+
230
+ /** The order a column sorts the list in: 'ascending', 'descending' or '' (not sorted by it). */
231
+ async function sortOrder(page, column) {
232
+ const th = columnHeading(page, column);
233
+ if (!(await th.count())) return null;
234
+ const cls = (await th.first().getAttribute('class')) || '';
235
+ if (!/\bsorted\b/.test(cls)) return '';
236
+ return /\bdescending\b/.test(cls) ? 'descending' : 'ascending';
237
+ }
238
+
239
+ /**
240
+ * Sort the change list by a column, with the column's heading as a person
241
+ * does: a first click sorts ascending, a second descending.
242
+ *
243
+ * Example #1: When I sort the list by "Date published"
244
+ * Example #2: When I sort the list by "Date published" in descending order
245
+ * Example #3: And we sort the list by "Question text" in ascending order
246
+ * Example #4: When I sort the list by "Username"
247
+ * Example #5: And I sort the list by "Category" in descending order
248
+ */
249
+ When(/^(I |we )*sort the list by "([^"]*)"(?: in (ascending|descending) order)?$/, async function (pronounCase, column, order) {
250
+ await changeListOrFail(this);
251
+ const page = this.page;
252
+ const want = order || 'ascending';
253
+ for (let i = 0; i < 3; i += 1) {
254
+ const now = await sortOrder(page, column);
255
+ if (now === null) {
256
+ throw friendly({ message: `The list has no column "${column}". Its columns: ${listed(await listColumns(page))}.`, hint: 'Name the column by its heading.' });
257
+ }
258
+ if (now === want) return;
259
+ const link = columnHeading(page, column).first().locator('.text a');
260
+ if (!(await link.count())) {
261
+ throw friendly({ message: `The column "${column}" cannot be sorted: its heading is no link.`, hint: 'A column sorts when its field (or its admin_order_field) can be ordered.' });
262
+ }
263
+ await afterNavigation(this, () => link.first().click());
264
+ }
265
+ throw friendly({ message: `The list did not sort by "${column}" in ${want} order: it is "${await sortOrder(page, column)}".`, hint: 'The heading of a sorted column carries the admin\'s sorted state.' });
266
+ });
267
+
268
+ /**
269
+ * Assert the order the change list is sorted in by a column, as the column's
270
+ * heading shows it.
271
+ *
272
+ * Example #1: Then the list should be sorted by "Date published" in ascending order
273
+ * Example #2: Then the list should be sorted by "Date published" in descending order
274
+ * Example #3: And the list should be sorted by "Question text" in ascending order
275
+ * Example #4: Then the list should be sorted by "Username" in descending order
276
+ * Example #5: And the list should be sorted by "Category" in ascending order
277
+ */
278
+ Then(/^the list should be sorted by "([^"]*)" in (ascending|descending) order$/, async function (column, order) {
279
+ await changeListOrFail(this);
280
+ const now = await sortOrder(this.page, column);
281
+ if (now !== order) {
282
+ throw friendly({
283
+ message: now === null ? `The list has no column "${column}". Its columns: ${listed(await listColumns(this.page))}.` : `The list is ${now ? `sorted by "${column}" in ${now} order` : `not sorted by "${column}"`}, not in ${order} order.`,
284
+ hint: `Sort it with: When I sort the list by "${column}" in ${order} order`,
285
+ });
286
+ }
287
+ });
288
+
289
+ /**
290
+ * Choose a value in a filter of the list's sidebar ("By category"), by the
291
+ * value's text as the filter shows it ("Science", "Any date", "All").
292
+ *
293
+ * Example #1: When I choose "Science" in the "By category" filter
294
+ * Example #2: When I choose "All" in the "By category" filter
295
+ * Example #3: And we choose "Yes" in the "By staff status" filter
296
+ * Example #4: When I choose "Editors" in the "By groups" filter
297
+ * Example #5: And I choose "Any date" in the "By date published" filter
298
+ */
299
+ When(/^(I |we )*choose "([^"]*)" in the "([^"]*)" filter$/, async function (pronounCase, value, title) {
300
+ await changeListOrFail(this);
301
+ const page = this.page;
302
+ const block = filterBlock(page, title);
303
+ if (!(await block.count())) {
304
+ throw friendly({ message: `The list has no filter "${title}". Its filters: ${listed(await filterTitles(page))}.`, hint: 'Name a filter as the sidebar shows it, such as "By category".' });
305
+ }
306
+ const link = block.first().locator('li a').filter({ hasText: new RegExp('^\\s*' + escapeRegExp(clean(value)) + '(\\s*\\(\\d+\\))?\\s*$') });
307
+ if (!(await link.count())) {
308
+ const offered = await block.first().locator('li a').allTextContents();
309
+ throw friendly({ message: `The "${title}" filter offers no "${value}". It offers: ${listed(offered)}.`, hint: 'Write the value as the filter shows it.' });
310
+ }
311
+ if (!(await block.first().evaluate((d) => d.tagName !== 'DETAILS' || d.open))) await block.first().locator('summary').click();
312
+ await afterNavigation(this, () => link.first().click());
313
+ });
314
+
315
+ /**
316
+ * Assert the value a filter of the sidebar is set to (the one it marks as
317
+ * chosen).
318
+ *
319
+ * Example #1: Then the "By category" filter should be set to "Science"
320
+ * Example #2: Then the "By category" filter should be set to "All"
321
+ * Example #3: And the "By staff status" filter should be set to "Yes"
322
+ * Example #4: Then the "By date published" filter should be set to "Any date"
323
+ * Example #5: And the "By groups" filter should be set to "Editors"
324
+ */
325
+ Then(/^the "([^"]*)" filter should be set to "([^"]*)"$/, async function (title, value) {
326
+ await changeListOrFail(this);
327
+ const page = this.page;
328
+ const block = filterBlock(page, title);
329
+ if (!(await block.count())) {
330
+ throw friendly({ message: `The list has no filter "${title}". Its filters: ${listed(await filterTitles(page))}.`, hint: 'Name a filter as the sidebar shows it, such as "By category".' });
331
+ }
332
+ const chosen = clean(await block.first().locator('li.selected').first().innerText().catch(() => '')).replace(/\s*\(\d+\)$/, '');
333
+ if (normal(chosen) !== normal(value)) {
334
+ throw friendly({ message: `The "${title}" filter is set to "${chosen || '(nothing)'}", not "${value}".`, hint: `Choose it with: When I choose "${value}" in the "${title}" filter` });
335
+ }
336
+ });
337
+
338
+ /**
339
+ * Clear every filter of the list with the sidebar's "Clear all filters" link
340
+ * (the list then shows all its rows again).
341
+ *
342
+ * Example #1: When I clear all filters
343
+ * Example #2: When we clear all filters
344
+ * Example #3: And I clear all filters
345
+ * Example #4: When I clear all the filters
346
+ * Example #5: And we clear all the filters
347
+ */
348
+ When(/^(I |we )*clear all (?:the )?filters$/, async function (pronounCase) {
349
+ await changeListOrFail(this);
350
+ const page = this.page;
351
+ const link = page.locator('#changelist-filter a').filter({ hasText: /Clear all filters/i });
352
+ if (!(await link.count())) {
353
+ throw friendly({ message: 'The filter sidebar has no "Clear all filters" link: no filter is set.', hint: 'The link shows once a filter is chosen.' });
354
+ }
355
+ await afterNavigation(this, () => link.first().click());
356
+ });
357
+
358
+ /**
359
+ * Show the counts of the filters' values ("Show counts" in the sidebar): each
360
+ * value then says how many rows it keeps, "Science (5)".
361
+ *
362
+ * Example #1: When I show the filter counts
363
+ * Example #2: When we show the filter counts
364
+ * Example #3: And I show the filter counts
365
+ * Example #4: Then I show the filter counts
366
+ * Example #5: Given I show the filter counts
367
+ */
368
+ When(/^(I |we )*show the filter counts$/, async function (pronounCase) {
369
+ await changeListOrFail(this);
370
+ const page = this.page;
371
+ const link = page.locator('#changelist-filter-extra-actions a, #changelist-filter a').filter({ hasText: /^\s*Show counts\s*$/i });
372
+ if (!(await link.count())) {
373
+ if (await page.locator('#changelist-filter a').filter({ hasText: /^\s*Hide counts\s*$/i }).count()) return;
374
+ throw friendly({ message: 'The filter sidebar has no "Show counts" link.', hint: 'The admin offers the counts when the model admin keeps show_facets on its default.' });
375
+ }
376
+ await afterNavigation(this, () => link.first().click());
377
+ });
378
+
379
+ /**
380
+ * Assert the count a filter's value shows once the counts are on (see "I show
381
+ * the filter counts"): "Science (5)".
382
+ *
383
+ * Example #1: Then the "By category" filter should count 5 for "Science"
384
+ * Example #2: Then the "By category" filter should count 4 for "Travel"
385
+ * Example #3: And the "By category" filter should count 2 for "-"
386
+ * Example #4: Then the "By staff status" filter should count 3 for "Yes"
387
+ * Example #5: And the "By groups" filter should count 1 for "Viewers"
388
+ */
389
+ Then(/^the "([^"]*)" filter should count (\d+) for "([^"]*)"$/, async function (title, expected, value) {
390
+ await changeListOrFail(this);
391
+ const page = this.page;
392
+ const block = filterBlock(page, title);
393
+ if (!(await block.count())) {
394
+ throw friendly({ message: `The list has no filter "${title}". Its filters: ${listed(await filterTitles(page))}.`, hint: 'Name a filter as the sidebar shows it.' });
395
+ }
396
+ const values = (await block.first().locator('li a').allTextContents()).map(clean);
397
+ const entry = values.find((v) => normal(v.replace(/\s*\(\d+\)$/, '')) === normal(value));
398
+ const count = entry && entry.match(/\((\d+)\)$/);
399
+ if (!count || count[1] !== String(expected)) {
400
+ throw friendly({
401
+ message: !entry ? `The "${title}" filter offers no "${value}". It offers: ${listed(values)}.` : (count ? `The "${title}" filter counts ${count[1]} for "${value}", not ${expected}.` : `The "${title}" filter shows no counts ("${entry}").`),
402
+ hint: 'Show the counts first: When I show the filter counts',
403
+ });
404
+ }
405
+ });
406
+
407
+ /**
408
+ * Choose a date in the list's date drill-down above the rows: a year, then a
409
+ * month ("January 2025"), then a day; "All dates" goes back to every date.
410
+ *
411
+ * Example #1: When I choose "2025" in the date drill-down
412
+ * Example #2: When I choose "January 2025" in the date drill-down
413
+ * Example #3: And we choose "All dates" in the date drill-down
414
+ * Example #4: When I choose "2026" in the date drill-down
415
+ * Example #5: And I choose "June 2026" in the date drill-down
416
+ */
417
+ When(/^(I |we )*choose "([^"]*)" in the date drill-down$/, async function (pronounCase, value) {
418
+ await changeListOrFail(this);
419
+ const page = this.page;
420
+ const links = page.locator('nav.toplinks a, .xfull a');
421
+ if (!(await links.count())) {
422
+ throw friendly({ message: 'The list has no date drill-down.', hint: 'The admin draws it when the model admin has date_hierarchy.' });
423
+ }
424
+ const link = links.filter({ hasText: new RegExp('^\\s*(‹\\s*)?' + escapeRegExp(clean(value)) + '\\s*$', 'i') });
425
+ if (!(await link.count())) {
426
+ throw friendly({ message: `The date drill-down offers no "${value}". It offers: ${listed(await links.allTextContents())}.`, hint: 'The drill-down offers the dates one level down (the years, the months of a year, the days of a month), and the level up as "‹ …": "‹ All dates" from a year, "‹ 2025" from a month.' });
427
+ }
428
+ await afterNavigation(this, () => link.first().click());
429
+ });
430
+
431
+ /**
432
+ * Assert that the date drill-down offers (or does not offer) a date.
433
+ *
434
+ * Example #1: Then the date drill-down should offer "2025"
435
+ * Example #2: Then the date drill-down should offer "January 2025"
436
+ * Example #3: And the date drill-down should not offer "2024"
437
+ * Example #4: Then the date drill-down should offer "All dates"
438
+ * Example #5: And the date drill-down should not offer "July 2026"
439
+ */
440
+ Then(/^the date drill-down should( not)* offer "([^"]*)"$/, async function (notCase, value) {
441
+ await changeListOrFail(this);
442
+ const offered = (await this.page.locator('nav.toplinks a, .xfull a').allTextContents().catch(() => [])).map((t) => clean(t).replace(/^‹\s*/, ''));
443
+ const has = offered.some((t) => normal(t) === normal(value));
444
+ if (has === !!notCase) {
445
+ throw friendly({ message: notCase ? `The date drill-down offers "${value}".` : `The date drill-down does not offer "${value}". It offers: ${listed(offered)}.`, hint: 'It offers the years, months or days that have rows, one level at a time.' });
446
+ }
447
+ });
448
+
449
+ /**
450
+ * Go to a page of the list with its paginator.
451
+ *
452
+ * Example #1: When I go to page 2 of the list
453
+ * Example #2: When we go to page 3 of the list
454
+ * Example #3: And I go to page 1 of the list
455
+ * Example #4: When I go to page 2 of the list
456
+ * Example #5: And we go to page 1 of the list
457
+ */
458
+ When(/^(I |we )*go to page (\d+) of the list$/, async function (pronounCase, number) {
459
+ await changeListOrFail(this);
460
+ const page = this.page;
461
+ const pages = await listPages(page);
462
+ if (pages.current === parseInt(number, 10)) return;
463
+ const link = page.locator('.paginator a').filter({ hasText: new RegExp(`^\\s*${number}\\s*$`) });
464
+ if (!(await link.count())) {
465
+ throw friendly({ message: `The list has no page ${number}: it has ${pages.last} page${pages.last === 1 ? '' : 's'}.`, hint: 'The paginator links the pages near the current one.' });
466
+ }
467
+ await afterNavigation(this, () => link.first().click());
468
+ });
469
+
470
+ /**
471
+ * Assert the page of the list on screen.
472
+ *
473
+ * Example #1: Then the list should be on page 2
474
+ * Example #2: Then the list should be on page 1
475
+ * Example #3: And the list should be on page 3
476
+ * Example #4: Then the list should be on page 1
477
+ * Example #5: And the list should be on page 2
478
+ */
479
+ Then(/^the list should be on page (\d+)$/, async function (number) {
480
+ await changeListOrFail(this);
481
+ const pages = await listPages(this.page);
482
+ if (pages.current !== parseInt(number, 10)) {
483
+ throw friendly({ message: `The list is on page ${pages.current} of ${pages.last}, not page ${number}.`, hint: `Go there with: When I go to page ${number} of the list` });
484
+ }
485
+ });
486
+
487
+ /**
488
+ * Assert how many pages the list has.
489
+ *
490
+ * Example #1: Then the list should have 3 pages
491
+ * Example #2: Then the list should have 1 page
492
+ * Example #3: And the list should have 2 pages
493
+ * Example #4: Then the list should have 3 pages
494
+ * Example #5: And the list should have 1 page
495
+ */
496
+ Then(/^the list should have (\d+) pages?$/, async function (number) {
497
+ await changeListOrFail(this);
498
+ const pages = await listPages(this.page);
499
+ if (pages.last !== parseInt(number, 10)) {
500
+ throw friendly({ message: `The list has ${pages.last} page${pages.last === 1 ? '' : 's'}, not ${number}.`, hint: 'The list shows list_per_page rows a page (100 by default).' });
501
+ }
502
+ });
503
+
504
+ /**
505
+ * Show every row of the list on one page, with the paginator's "Show all"
506
+ * link (offered while the list is short enough: list_max_show_all).
507
+ *
508
+ * Example #1: When I show all rows of the list
509
+ * Example #2: When we show all rows of the list
510
+ * Example #3: And I show all rows of the list
511
+ * Example #4: When I show all the rows of the list
512
+ * Example #5: And we show all the rows of the list
513
+ */
514
+ When(/^(I |we )*show all (?:the )?rows of the list$/, async function (pronounCase) {
515
+ await changeListOrFail(this);
516
+ const page = this.page;
517
+ const link = page.locator('.paginator a.showall');
518
+ if (!(await link.count())) {
519
+ throw friendly({ message: 'The paginator has no "Show all" link.', hint: 'The admin offers it when the list has more than one page and no more rows than list_max_show_all.' });
520
+ }
521
+ await afterNavigation(this, () => link.first().click());
522
+ });
523
+
524
+ /** The selection box of a row, by the object's text. */
525
+ function rowBox(page, object) {
526
+ return page.locator('#result_list tbody tr').filter({
527
+ has: page.locator('th, td').filter({ hasText: new RegExp('^\\s*' + escapeRegExp(clean(object)) + '\\s*$') }),
528
+ }).locator('input.action-select');
529
+ }
530
+
531
+ /**
532
+ * Tick (or clear) the selection box of a row of the list, by the object's
533
+ * text, for an action.
534
+ *
535
+ * Example #1: When I select "Which river is the longest?" in the list
536
+ * Example #2: When I unselect "Which river is the longest?" in the list
537
+ * Example #3: And we select "Action test question one" in the list
538
+ * Example #4: When I select "What's up?" in the list
539
+ * Example #5: And I unselect "What's up?" in the list
540
+ */
541
+ When(/^(I |we )*(select|unselect) "([^"]*)" in the list$/, async function (pronounCase, verb, object) {
542
+ await changeListOrFail(this);
543
+ const box = rowBox(this.page, object);
544
+ if (!(await box.count())) {
545
+ const shown = await shownObjects(this.page);
546
+ throw friendly({
547
+ message: (await this.page.locator('input.action-select').count())
548
+ ? `The list has no row "${object}" on this page. It shows: ${listed(shown)}.`
549
+ : 'The list has no selection boxes: the account may run no action on it.',
550
+ hint: 'Search for the row, or go to its page, first.',
551
+ });
552
+ }
553
+ if (verb === 'select') await box.first().check();
554
+ else await box.first().uncheck();
555
+ });
556
+
557
+ /**
558
+ * Tick the box above the list that selects every row of the page.
559
+ *
560
+ * Example #1: When I select all rows on the page
561
+ * Example #2: When we select all rows on the page
562
+ * Example #3: And I select all the rows on the page
563
+ * Example #4: When I select all rows on the page
564
+ * Example #5: And we select all the rows on the page
565
+ */
566
+ When(/^(I |we )*select all (?:the )?rows on the page$/, async function (pronounCase) {
567
+ await changeListOrFail(this);
568
+ const toggle = this.page.locator('#action-toggle');
569
+ if (!(await toggle.count())) {
570
+ throw friendly({ message: 'The list has no box to select its rows: the account may run no action on it.', hint: 'The admin draws the selection boxes when the account may run an action.' });
571
+ }
572
+ await toggle.check();
573
+ });
574
+
575
+ /**
576
+ * Select every row of the list, on all its pages: the rows of the page, then
577
+ * the link "Select all 24 questions" the admin offers then.
578
+ *
579
+ * Example #1: When I select everything in the list
580
+ * Example #2: When we select everything in the list
581
+ * Example #3: And I select everything in the list
582
+ * Example #4: Then I select everything in the list
583
+ * Example #5: Given we select everything in the list
584
+ */
585
+ When(/^(I |we )*select everything in the list$/, async function (pronounCase) {
586
+ await changeListOrFail(this);
587
+ const page = this.page;
588
+ const toggle = page.locator('#action-toggle');
589
+ if (!(await toggle.count())) {
590
+ throw friendly({ message: 'The list has no box to select its rows: the account may run no action on it.', hint: 'The admin draws the selection boxes when the account may run an action.' });
591
+ }
592
+ await toggle.check();
593
+ const across = page.locator('.actions .question a');
594
+ if (await across.isVisible().catch(() => false)) {
595
+ await across.click();
596
+ await page.locator('.actions .all').waitFor({ state: 'visible', timeout: 5000 });
597
+ }
598
+ });
599
+
600
+ /**
601
+ * Clear the selection of the list with the admin's "Clear selection" link
602
+ * (after selecting across pages), or by clearing every box.
603
+ *
604
+ * Example #1: When I clear the selection
605
+ * Example #2: When we clear the selection
606
+ * Example #3: And I clear the selection
607
+ * Example #4: Then I clear the selection
608
+ * Example #5: Given we clear the selection
609
+ */
610
+ When(/^(I |we )*clear the selection$/, async function (pronounCase) {
611
+ await changeListOrFail(this);
612
+ const page = this.page;
613
+ const clear = page.locator('.actions .clear a');
614
+ if (await clear.isVisible().catch(() => false)) {
615
+ await clear.click();
616
+ } else if (await page.locator('#action-toggle').count()) {
617
+ await page.locator('#action-toggle').check();
618
+ await page.locator('#action-toggle').uncheck();
619
+ }
620
+ });
621
+
622
+ /**
623
+ * Assert what the action bar says about the selection: "2 of 10 selected", or
624
+ * "All 24 selected" after selecting across all pages.
625
+ *
626
+ * Example #1: Then the selection should read "2 of 10 selected"
627
+ * Example #2: Then the selection should read "All 24 selected"
628
+ * Example #3: And the selection should read "0 of 10 selected"
629
+ * Example #4: Then the selection should read "10 of 10 selected"
630
+ * Example #5: And the selection should read "1 of 3 selected"
631
+ */
632
+ Then(/^the selection should read "([^"]*)"$/, async function (expected) {
633
+ await changeListOrFail(this);
634
+ const page = this.page;
635
+ const read = () => page.evaluate(() => [...document.querySelectorAll('.actions .action-counter, .actions .all')]
636
+ .filter((e) => !e.classList.contains('hidden') && getComputedStyle(e).display !== 'none')
637
+ .map((e) => e.textContent.replace(/\s+/g, ' ').trim()).join(' ')).catch(() => '');
638
+ await eventually(this, read, (text) => normal(text) === normal(expected),
639
+ (text) => ({ message: `The action bar says "${text || '(nothing)'}", not "${expected}".`, hint: 'The counter follows the boxes of the rows; select across pages with: When I select everything in the list' }), 3000);
640
+ });
641
+
642
+ /**
643
+ * Assert that the action list of the change list offers (or does not offer)
644
+ * an action, by its text ("Delete selected questions").
645
+ *
646
+ * Example #1: Then the list should offer the action "Delete selected questions"
647
+ * Example #2: Then the list should offer the action "Reset the votes of the selected questions"
648
+ * Example #3: And the list should not offer the action "Delete selected questions"
649
+ * Example #4: Then the list should not offer the action "Reset the votes of the selected questions"
650
+ * Example #5: And the list should offer the action "Delete selected users"
651
+ */
652
+ Then(/^the list should( not)* offer the action "([^"]*)"$/, async function (notCase, action) {
653
+ await changeListOrFail(this);
654
+ const options = (await this.page.locator('.actions select[name="action"] option').allTextContents().catch(() => [])).map(clean).filter((o) => !/^-+$|^- .* -$/.test(o));
655
+ const has = options.some((o) => normal(o) === normal(action));
656
+ if (has === !!notCase) {
657
+ throw friendly({ message: notCase ? `The list offers the action "${action}".` : `The list does not offer the action "${action}". It offers: ${listed(options)}.`, hint: 'An account sees only the actions its permissions allow.' });
658
+ }
659
+ });
660
+
661
+ /**
662
+ * Run an action on the rows that are selected: choose it in the action list
663
+ * and press the button beside it ("Run", "Go" on older releases). An action
664
+ * that asks first (such as "Delete selected …") opens its confirmation.
665
+ *
666
+ * Example #1: When I run the action "Delete selected questions"
667
+ * Example #2: When I run the action "Reset the votes of the selected questions"
668
+ * Example #3: And we run the action "Delete selected questions"
669
+ * Example #4: When we run the action "Reset the votes of the selected questions"
670
+ * Example #5: And I run the action "Delete selected tags"
671
+ */
672
+ When(/^(I |we )*run the action "([^"]*)"$/, async function (pronounCase, action) {
673
+ await changeListOrFail(this);
674
+ const page = this.page;
675
+ const select = page.locator('.actions select[name="action"]');
676
+ if (!(await select.count())) {
677
+ throw friendly({ message: 'The list has no actions: the account may run none on it.', hint: 'The admin draws the action bar when the account may run an action.' });
678
+ }
679
+ const options = (await select.locator('option').allTextContents()).map(clean);
680
+ const exact = options.find((o) => normal(o) === normal(action));
681
+ if (!exact) {
682
+ throw friendly({ message: `The list offers no action "${action}". It offers: ${listed(options.filter((o) => !/^-+$|^- .* -$/.test(o)))}.`, hint: 'Write the action as the list shows it.' });
683
+ }
684
+ await select.selectOption({ label: exact });
685
+ await afterNavigation(this, () => page.locator('.actions button[name="index"], .actions [type="submit"]').first().click());
686
+ });
687
+
688
+ module.exports = { shownObjects, rowBox };