@webship-js/bbpress-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,577 @@
1
+ 'use strict';
2
+
3
+ // A topic and its posts: opening one, its posts by their number on the page
4
+ // (who wrote them, the author's forum role, what they say and how it is
5
+ // formatted, their edit log), its marks and tags; and the forms: a new topic,
6
+ // a reply, an edit, with the editor's toolbar, the tags, the topic's type and
7
+ // status, and the errors the forums answer with.
8
+ //
9
+ // The forms are plain forms: submitting one loads a page. A step that
10
+ // submits waits for that page, then for the forums' answer on it: the new
11
+ // topic's own page, the reply under its address (`#post-<id>`), or the error
12
+ // notice above the form.
13
+
14
+ const { When, Then } = require('@cucumber/cucumber');
15
+ const { friendly } = require('webship-js/tests/step-definitions/webship');
16
+ const {
17
+ FORUMS,
18
+ settle,
19
+ navigate,
20
+ supplied,
21
+ clean,
22
+ listed,
23
+ same,
24
+ holds,
25
+ eventually,
26
+ clickAndLoad,
27
+ forumPage,
28
+ forumNotices,
29
+ listTotal,
30
+ openForum,
31
+ openTopic,
32
+ readingTopic,
33
+ topicPosts,
34
+ postOnPage,
35
+ followPostLink,
36
+ } = require('./bbpress-helpers');
37
+
38
+ /** The form of the forums on the page (a new topic, a reply, an edit), or fail. */
39
+ async function postForm(world) {
40
+ const form = world.page.locator(`${FORUMS} form#new-post`).first();
41
+ if (!(await form.count())) {
42
+ const notices = (await forumNotices(world)).filter((n) => n.kind !== 'info').map((n) => n.text);
43
+ throw friendly({
44
+ message: `The page has no topic or reply form${notices.length ? `: the forums say ${listed(notices)}` : ''} (${world.page.url()}).`,
45
+ hint: 'Open a forum to start a topic, a topic to reply, or edit a post first. A visitor, a spectator, a closed topic and a closed forum get no form.',
46
+ });
47
+ }
48
+ return form;
49
+ }
50
+
51
+ /** The text area of the form on the page: the topic's or the reply's. */
52
+ async function editorArea(world) {
53
+ const form = await postForm(world);
54
+ return form.locator('textarea#bbp_topic_content, textarea#bbp_reply_content').first();
55
+ }
56
+
57
+ /** The errors the forums print above a form they refused. */
58
+ async function formErrors(world) {
59
+ return (await forumNotices(world)).filter((n) => n.kind === 'error').map((n) => n.text);
60
+ }
61
+
62
+ // ---------------------------------------------------------------------------
63
+ // Reading a topic
64
+ // ---------------------------------------------------------------------------
65
+
66
+ /**
67
+ * Open a topic by its title: in a forum (through the pages of its list), or
68
+ * without one from the list on the page, a topic a Given step of the scenario
69
+ * made, or the forum search.
70
+ *
71
+ * Example #1: When I open the topic "Welcome to the forums" in "General discussion"
72
+ * Example #2: And I open the topic "A long conversation" in "Help and support"
73
+ * Example #3: When we open the topic "Forum rules"
74
+ * Example #4: When I open the topic "Showcase entry 01" in "Showcase"
75
+ * Example #5: And I open the topic "The forums opened" in "Archive"
76
+ */
77
+ When(/^(I |we )*open the topic "([^"]*)"(?: in "([^"]*)")?$/, async function (pronounCase, title, forum) {
78
+ await openTopic(this, title, forum);
79
+ });
80
+
81
+ /**
82
+ * Assert the page is a topic, by its title.
83
+ *
84
+ * Example #1: Then I should be reading the topic "Welcome to the forums"
85
+ * Example #2: And I should be reading the topic "Forum rules"
86
+ * Example #3: Then we should be reading the topic "A long conversation"
87
+ * Example #4: Then I should be reading the topic "Showcase entry 01"
88
+ * Example #5: And we should be reading the topic "The forums opened"
89
+ */
90
+ Then(/^(I |we )*should be reading the topic "([^"]*)"$/, async function (pronounCase, title) {
91
+ await settle(this);
92
+ await readingTopic(this, title);
93
+ });
94
+
95
+ /**
96
+ * Assert how many posts the topic has over all its pages: its own post and
97
+ * its replies ("Viewing 15 posts - 1 through 15 (of 18 total)").
98
+ *
99
+ * Example #1: Then the topic should have 3 posts
100
+ * Example #2: And the topic should have 18 posts
101
+ * Example #3: Then the topic should have 1 post
102
+ * Example #4: Then the topic should have 2 posts
103
+ * Example #5: And the topic should have 4 posts
104
+ */
105
+ Then(/^the topic should have (\d+) posts?$/, async function (count) {
106
+ await settle(this);
107
+ await readingTopic(this);
108
+ const total = await listTotal(this);
109
+ const actual = total && total.total !== null ? total.total : (await topicPosts(this)).length;
110
+ if (actual !== Number(count)) {
111
+ throw friendly({
112
+ message: `Expected the topic to have ${count} post(s), but it has ${actual}${total ? ` ("${clean(total.text)}")` : ''}.`,
113
+ hint: 'The topic\'s own post counts as the first. A moderator also counts the pending, spammed and trashed replies.',
114
+ });
115
+ }
116
+ });
117
+
118
+ /**
119
+ * Assert who wrote a post of the page, by its number on the page (1 is the
120
+ * topic's own post on the first page) or the last one.
121
+ *
122
+ * Example #1: Then post 1 should be by "keymaster"
123
+ * Example #2: And post 2 should be by "participant"
124
+ * Example #3: Then the last post should be by "moderator"
125
+ * Example #4: Then post 3 should be by "moderator"
126
+ * Example #5: And the last post should be by "participant"
127
+ */
128
+ Then(/^(?:post (\d+)|the last post) should be by "([^"]*)"$/, async function (number, author) {
129
+ await settle(this);
130
+ const { post, label } = await postOnPage(this, number);
131
+ if (!same(post.author, author)) {
132
+ throw friendly({ message: `${label} is by "${post.author}", not by "${author}".`, hint: 'A post names the display name of its author.' });
133
+ }
134
+ });
135
+
136
+ /**
137
+ * Assert the forum role the forums print under the author of a post
138
+ * (Keymaster, Moderator, Participant, Spectator, Blocked).
139
+ *
140
+ * Example #1: Then the author of post 1 should be a "Keymaster"
141
+ * Example #2: And the author of post 2 should be a "Participant"
142
+ * Example #3: Then the author of the last post should be a "Moderator"
143
+ * Example #4: Then the author of post 3 should be a "Moderator"
144
+ * Example #5: And the author of the last post should be a "Participant"
145
+ */
146
+ Then(/^the author of (?:post (\d+)|the last post) should be a "([^"]*)"$/, async function (number, role) {
147
+ await settle(this);
148
+ const { post, label } = await postOnPage(this, number);
149
+ if (!same(post.role, role)) {
150
+ throw friendly({ message: `${label} is by "${post.author}", whose forum role reads "${post.role}", not "${role}".`, hint: 'The forum roles are Keymaster, Moderator, Participant, Spectator and Blocked.' });
151
+ }
152
+ });
153
+
154
+ /**
155
+ * Assert what a post says: its text holds the given text (the edit log under
156
+ * it aside).
157
+ *
158
+ * Example #1: Then post 1 should read "Say hello and tell us what you are working on."
159
+ * Example #2: And post 2 should read "Hello everyone"
160
+ * Example #3: Then the last post should read "Welcome aboard."
161
+ * Example #4: Then the last post should read "Webship test reply"
162
+ * Example #5: And post 3 should read "Ask in Help and support"
163
+ */
164
+ Then(/^(?:post (\d+)|the last post) should read "([^"]*)"$/, async function (number, text) {
165
+ await eventually(this, async () => (await postOnPage(this, number)), (found) => holds(found.post.text, text), (found) => ({
166
+ message: found ? `${found.label} does not read "${text}". It reads: "${clean(found.post.text).slice(0, 300)}".` : `The topic shows no such post (${this.page.url()}).`,
167
+ hint: 'The text is matched as a part of the post, letter case aside.',
168
+ }), 3000);
169
+ });
170
+
171
+ /**
172
+ * Assert a post shows a text with a format: in bold, in italics, as a quote,
173
+ * as code, as a link, or struck through.
174
+ *
175
+ * Example #1: Then the last post should show "bold words" in bold
176
+ * Example #2: And the last post should show "a quoted line" as a quote
177
+ * Example #3: Then post 2 should show "important" in italics
178
+ * Example #4: Then the last post should show "wp-config.php" as code
179
+ * Example #5: And the last post should show "the documentation" as a link
180
+ */
181
+ Then(/^(?:post (\d+)|the last post) should show "([^"]*)" (in bold|in italics|as a quote|as code|as a link|struck through)$/, async function (number, text, format) {
182
+ await settle(this);
183
+ const { post, label } = await postOnPage(this, number);
184
+ const tags = { 'in bold': 'strong, b', 'in italics': 'em, i', 'as a quote': 'blockquote', 'as code': 'code, pre', 'as a link': 'a', 'struck through': 'del, s' }[format];
185
+ const found = await this.page.evaluate(([html, selector]) => {
186
+ const box = document.createElement('div');
187
+ box.innerHTML = html;
188
+ return [...box.querySelectorAll(selector)].map((e) => e.textContent.replace(/\s+/g, ' ').trim());
189
+ }, [post.html, tags]);
190
+ if (!found.some((f) => holds(f, text))) {
191
+ throw friendly({
192
+ message: `${label} does not show "${text}" ${format}. ${found.length ? `It shows ${format}: ${listed(found)}` : `It shows nothing ${format}`}; the post reads: "${clean(post.text).slice(0, 200)}".`,
193
+ hint: 'The forums keep the formatting their editor\'s toolbar writes (b, i, b-quote, del, code, link) and drop what the account may not post.',
194
+ });
195
+ }
196
+ });
197
+
198
+ /**
199
+ * Assert the edit log under a post holds a text: who changed it and the
200
+ * reason given ("This topic was modified … by moderator. Reason: Clearer title").
201
+ *
202
+ * Example #1: Then the edit log of post 1 should show "Reason: Clearer title"
203
+ * Example #2: And the edit log of the last post should show "This reply was modified"
204
+ * Example #3: Then the edit log of post 1 should show "by moderator"
205
+ * Example #4: Then the edit log of the last post should show "Reason: Typo"
206
+ * Example #5: And the edit log of post 2 should show "by participant"
207
+ */
208
+ Then(/^the edit log of (?:post (\d+)|the last post) should show "([^"]*)"$/, async function (number, text) {
209
+ await settle(this);
210
+ const { post, label } = await postOnPage(this, number);
211
+ if (!post.log.some((line) => holds(line, text))) {
212
+ throw friendly({
213
+ message: `The edit log of ${label.toLowerCase()} does not show "${text}". ${post.log.length ? `It shows: ${listed(post.log)}` : 'The post has no edit log'}.`,
214
+ hint: 'A post gets a line in its log when it is edited with "Keep a log of this edit" checked, which the forums\' "Revisions" setting allows.',
215
+ });
216
+ }
217
+ });
218
+
219
+ /**
220
+ * Assert a mark of the topic that is open: open or closed, sticky, pending,
221
+ * spam, in the trash.
222
+ *
223
+ * Example #1: Then the topic should be closed
224
+ * Example #2: And the topic should be open
225
+ * Example #3: Then the topic should be sticky
226
+ * Example #4: Then the topic should not be sticky
227
+ * Example #5: And the topic should be pending
228
+ */
229
+ Then(/^the topic should( not)* be (open|closed|sticky|pending|spam|in the trash)$/, async function (notCase, mark) {
230
+ await settle(this);
231
+ await readingTopic(this);
232
+ const posts = await topicPosts(this);
233
+ const lead = posts.find((p) => p.lead);
234
+ let is;
235
+ let found;
236
+ if (mark === 'sticky') {
237
+ // The topic's own page says it through the link it offers a moderator, its body through a class of the lists.
238
+ const state = await this.page.evaluate((sel) => {
239
+ const link = document.querySelector(`${sel} .bbp-admin-links a.bbp-topic-sticky-link`);
240
+ return link ? link.textContent.trim() : null;
241
+ }, FORUMS);
242
+ if (state === null) {
243
+ throw friendly({ message: 'The topic\'s page does not say whether it is sticky for this account.', hint: 'A moderator reads it from the topic\'s links (Stick or Unstick); any account from the forum\'s list: Then the topic "…" should be sticky in the list.' });
244
+ }
245
+ is = /unstick/i.test(state);
246
+ found = is ? 'sticky' : 'not sticky';
247
+ } else {
248
+ if (!lead) {
249
+ throw friendly({ message: 'The page does not show the topic\'s own post, which carries its status.', hint: 'Go to the first page of the topic: When I go to page 1.' });
250
+ }
251
+ const status = { open: 'publish', closed: 'closed', pending: 'pending', spam: 'spam', 'in the trash': 'trash' }[mark];
252
+ is = lead.status === status;
253
+ found = { publish: 'open', closed: 'closed', pending: 'pending', spam: 'spam', trash: 'in the trash' }[lead.status] || lead.status;
254
+ }
255
+ if (is === Boolean(notCase)) {
256
+ throw friendly({
257
+ message: notCase ? `Expected the topic not to be ${mark}, but it is.` : `Expected the topic to be ${mark}, but it is ${found}.`,
258
+ hint: 'A topic is open, closed (no new replies), pending (waits for a moderator), spam or in the trash; sticky comes on top of that.',
259
+ });
260
+ }
261
+ });
262
+
263
+ /**
264
+ * Assert the topic carries (or does not carry) a tag, in the "Tagged:" line
265
+ * above its posts.
266
+ *
267
+ * Example #1: Then the topic should be tagged "welcome"
268
+ * Example #2: And the topic should be tagged "spring"
269
+ * Example #3: Then the topic should not be tagged "howto"
270
+ * Example #4: Then the topic should be tagged "garden"
271
+ * Example #5: And the topic should not be tagged "rules"
272
+ */
273
+ Then(/^the topic should( not)* be tagged "([^"]*)"$/, async function (notCase, tag) {
274
+ await settle(this);
275
+ await readingTopic(this);
276
+ const tags = await this.page.evaluate((sel) => [...document.querySelectorAll(`${sel} .bbp-topic-tags a`)].map((a) => a.textContent.trim()), FORUMS);
277
+ if (tags.some((t) => same(t, tag)) === Boolean(notCase)) {
278
+ throw friendly({
279
+ message: notCase ? `Expected the topic not to be tagged "${tag}", but it is.` : `Expected the topic to be tagged "${tag}". Its tags: ${listed(tags)}.`,
280
+ hint: 'A spammed topic loses its tags until it is marked as not spam.',
281
+ });
282
+ }
283
+ });
284
+
285
+ // ---------------------------------------------------------------------------
286
+ // The forms: a new topic, a reply, an edit
287
+ // ---------------------------------------------------------------------------
288
+
289
+ /**
290
+ * Open a forum and its form for a new topic.
291
+ *
292
+ * Example #1: When I start a new topic in "General discussion"
293
+ * Example #2: And I start a new topic in "Help and support"
294
+ * Example #3: When we start a new topic in "Showcase"
295
+ * Example #4: When I start a new topic in "Members lounge"
296
+ * Example #5: And we start a new topic in "Staff room"
297
+ */
298
+ When(/^(I |we )*start a new topic in "([^"]*)"$/, async function (pronounCase, forum) {
299
+ await openForum(this, forum);
300
+ const title = this.page.locator(`${FORUMS} form#new-post #bbp_topic_title`);
301
+ if (!(await title.count())) {
302
+ const notices = (await forumNotices(this)).filter((n) => n.kind !== 'info').map((n) => n.text);
303
+ throw friendly({
304
+ message: `The forum "${forum}" offers this account no form for a new topic${notices.length ? `: the forums say ${listed(notices)}` : ''}.`,
305
+ hint: 'A participant may start a topic in an open forum; a visitor, a spectator, a category and a closed forum get a notice instead.',
306
+ });
307
+ }
308
+ });
309
+
310
+ /**
311
+ * Write the title of the topic in the form (a new topic, or the edit of one).
312
+ *
313
+ * Example #1: When I write the topic title "Webship test topic"
314
+ * Example #2: And I write the topic title "Webship test topic, renamed"
315
+ * Example #3: When we write the topic title "Webship test question"
316
+ * Example #4: When I write the topic title ""
317
+ * Example #5: And we write the topic title "Webship test announcement"
318
+ */
319
+ When(/^(I |we )*write the topic title "([^"]*)"$/, async function (pronounCase, title) {
320
+ const form = await postForm(this);
321
+ const field = form.locator('#bbp_topic_title');
322
+ if (!(await field.count())) {
323
+ throw friendly({ message: 'The form on the page has no topic title: it is the form of a reply.', hint: 'Start a topic first (When I start a new topic in "…"), or edit the topic\'s own post (When I edit post 1).' });
324
+ }
325
+ await field.fill(title);
326
+ });
327
+
328
+ /**
329
+ * Write a text in the editor of the form (it replaces what the editor
330
+ * holds), or add one to what is there.
331
+ *
332
+ * Example #1: When I write "Webship test: the first post of the topic." in the editor
333
+ * Example #2: And I add " A second sentence." to the editor
334
+ * Example #3: When we write "Webship test reply." in the editor
335
+ * Example #4: When I write "" in the editor
336
+ * Example #5: And I write "A reply with <strong>bold</strong> words." in the editor
337
+ */
338
+ When(/^(I |we )*(write|add) "([^"]*)" (?:in|to) the editor$/, async function (pronounCase, how, text) {
339
+ const area = await editorArea(this);
340
+ if (how === 'add') {
341
+ await area.evaluate((el, more) => { el.value += more; el.dispatchEvent(new Event('input', { bubbles: true })); }, text);
342
+ } else {
343
+ await area.fill(text);
344
+ }
345
+ });
346
+
347
+ /**
348
+ * Add a text to the editor through a button of its toolbar: the button opens
349
+ * the format, the text is typed, the button closes it again. The buttons that
350
+ * wrap a text: "b", "i", "b-quote", "del", "code".
351
+ *
352
+ * Example #1: When I add "bold words" to the editor with the "b" button
353
+ * Example #2: And I add "a quoted line" to the editor with the "b-quote" button
354
+ * Example #3: When we add "important" to the editor with the "i" button
355
+ * Example #4: When I add "wp-config.php" to the editor with the "code" button
356
+ * Example #5: And I add "no longer true" to the editor with the "del" button
357
+ */
358
+ When(/^(I |we )*add "([^"]*)" to the editor with the "([^"]*)" button$/, async function (pronounCase, text, name) {
359
+ const wrapping = ['b', 'i', 'b-quote', 'del', 'code'];
360
+ if (!wrapping.includes(name)) {
361
+ throw friendly({ message: `"${name}" is not a button of the toolbar that wraps a text.`, hint: `The buttons that do: ${wrapping.join(', ')}. The link and image buttons ask for an address; write their HTML in the editor instead.` });
362
+ }
363
+ const area = await editorArea(this);
364
+ const toolbar = this.page.locator(`${FORUMS} form#new-post .quicktags-toolbar`);
365
+ const button = toolbar.locator(`input[type="button"][value="${name}"]`).first();
366
+ try {
367
+ await button.waitFor({ state: 'visible', timeout: 10000 });
368
+ } catch (err) {
369
+ const buttons = await toolbar.locator('input[type="button"]').evaluateAll((all) => all.map((b) => b.value)).catch(() => []);
370
+ throw friendly({ message: `The editor's toolbar has no "${name}" button. It has: ${listed(buttons)}.`, hint: 'The toolbar comes with the forums\' "Post Formatting" setting.' });
371
+ }
372
+ // The caret goes to the end of the text: the toolbar writes where the caret is.
373
+ await area.evaluate((el) => { el.focus(); el.setSelectionRange(el.value.length, el.value.length); });
374
+ await button.click();
375
+ await this.page.keyboard.type(text);
376
+ await toolbar.locator(`input[type="button"][value="/${name}"]`).first().click();
377
+ });
378
+
379
+ /**
380
+ * Assert what the editor of the form holds: the text of a post that is being
381
+ * edited, or what was written.
382
+ *
383
+ * Example #1: Then the editor should contain "Webship test: the first post of the topic."
384
+ * Example #2: And the editor should contain "<strong>bold words</strong>"
385
+ * Example #3: Then the editor should contain "Webship test reply."
386
+ * Example #4: Then the editor should contain "Say hello"
387
+ * Example #5: And the editor should contain "<blockquote>a quoted line</blockquote>"
388
+ */
389
+ Then(/^the editor should contain "([^"]*)"$/, async function (text) {
390
+ const area = await editorArea(this);
391
+ const value = await area.inputValue();
392
+ if (!clean(value).includes(clean(text))) {
393
+ throw friendly({ message: `The editor does not contain "${text}". It holds: "${clean(value).slice(0, 300)}".`, hint: 'The editor holds the HTML of the post, as the toolbar writes it.' });
394
+ }
395
+ });
396
+
397
+ /**
398
+ * Give the topic its tags in the form, separated by commas (it replaces the
399
+ * tags the field holds).
400
+ *
401
+ * Example #1: When I tag the topic with "webship, test"
402
+ * Example #2: And I tag the topic with "howto"
403
+ * Example #3: When we tag the topic with "spring, garden"
404
+ * Example #4: When I tag the topic with ""
405
+ * Example #5: And we tag the topic with "account"
406
+ */
407
+ When(/^(I |we )*tag the topic with "([^"]*)"$/, async function (pronounCase, tags) {
408
+ const form = await postForm(this);
409
+ const field = form.locator('#bbp_topic_tags');
410
+ if (!(await field.count())) {
411
+ throw friendly({ message: 'The form has no field for the topic\'s tags.', hint: 'The forums\' "Topic tags" setting allows them; a participant may tag a topic it starts or replies to.' });
412
+ }
413
+ await field.fill(tags);
414
+ });
415
+
416
+ /**
417
+ * Choose the type (Normal, Sticky, Super Sticky) or the status (Open,
418
+ * Closed, Spam, Trash, Pending) of the topic in the form, as a moderator.
419
+ *
420
+ * Example #1: When I choose the topic type "Sticky"
421
+ * Example #2: And I choose the topic status "Closed"
422
+ * Example #3: When we choose the topic type "Super Sticky"
423
+ * Example #4: When I choose the topic type "Normal"
424
+ * Example #5: And I choose the topic status "Open"
425
+ */
426
+ When(/^(I |we )*choose the topic (type|status) "([^"]*)"$/, async function (pronounCase, what, option) {
427
+ const form = await postForm(this);
428
+ const select = form.locator(what === 'type' ? '#bbp_stick_topic_select' : '#bbp_topic_status_select');
429
+ if (!(await select.count())) {
430
+ throw friendly({ message: `The form has no list for the topic's ${what}.`, hint: 'A moderator and a keymaster get it on the form of a topic; a participant does not.' });
431
+ }
432
+ const options = await select.locator('option').evaluateAll((all) => all.map((o) => o.textContent.trim()));
433
+ const match = options.find((o) => same(o, option));
434
+ if (!match) throw friendly({ message: `The topic's ${what} has no option "${option}". It has: ${listed(options)}.`, hint: 'Use the option as the list shows it.' });
435
+ await select.selectOption({ label: match });
436
+ });
437
+
438
+ /**
439
+ * Give the reason of an edit, in the edit form of a topic or a reply; the
440
+ * forums print it in the post's edit log.
441
+ *
442
+ * Example #1: When I give the edit reason "Clearer title"
443
+ * Example #2: And I give the edit reason "Typo"
444
+ * Example #3: When we give the edit reason "Moved to the right forum"
445
+ * Example #4: When I give the edit reason ""
446
+ * Example #5: And we give the edit reason "Added a link"
447
+ */
448
+ When(/^(I |we )*give the edit reason "([^"]*)"$/, async function (pronounCase, reason) {
449
+ const form = await postForm(this);
450
+ const field = form.locator('#bbp_topic_edit_reason, #bbp_reply_edit_reason').first();
451
+ if (!(await field.count())) {
452
+ throw friendly({ message: 'The form has no field for the reason of an edit.', hint: 'Edit a post first (When I edit post 1); the forums\' "Revisions" setting allows the log.' });
453
+ }
454
+ await field.fill(reason);
455
+ });
456
+
457
+ /**
458
+ * Submit the form of the forums: the new topic, the reply or the edit. The
459
+ * step waits for the page the forums answer with, and fails with their
460
+ * errors when they refuse the form; with "try to" it leaves the outcome to
461
+ * the next step.
462
+ *
463
+ * Example #1: When I submit the post
464
+ * Example #2: And I try to submit the post
465
+ * Example #3: When we submit the post
466
+ * Example #4: When we try to submit the post
467
+ * Example #5: And I submit the post
468
+ */
469
+ When(/^(I |we )*(try to )?submit the post$/, async function (pronounCase, trying) {
470
+ const form = await postForm(this);
471
+ const title = form.locator('#bbp_topic_title');
472
+ const written = (await title.count()) ? await title.inputValue() : '';
473
+ await clickAndLoad(this, form.locator('#bbp_topic_submit, #bbp_reply_submit').first(), 'submit the post');
474
+ if (trying) return;
475
+ const errors = await formErrors(this);
476
+ if (errors.length) {
477
+ throw friendly({
478
+ message: `The forums refused the post: ${listed(errors)}.`,
479
+ hint: 'A topic needs a title and a text, a reply a text; the forums refuse the same post twice and a post sent right after another when their flood protection is on.',
480
+ });
481
+ }
482
+ const page = await forumPage(this);
483
+ if (page.kind === 'topic' && written) this.forumTopics.push({ title: page.heading, url: this.page.url().split('#')[0].split('?')[0] });
484
+ });
485
+
486
+ /**
487
+ * Assert the forums refused the form with an error, by a part of its text.
488
+ *
489
+ * Example #1: Then the editor should show the error "Your topic needs a title."
490
+ * Example #2: And the editor should show the error "Your topic cannot be empty."
491
+ * Example #3: Then the editor should show the error "Your reply cannot be empty."
492
+ * Example #4: Then the editor should show the error "Duplicate reply detected"
493
+ * Example #5: And the editor should show the error "Duplicate topic detected"
494
+ */
495
+ Then(/^the editor should show the error "([^"]*)"$/, async function (text) {
496
+ await eventually(this, () => formErrors(this), (errors) => errors.some((e) => holds(e, text)), (errors) => ({
497
+ message: `Expected the error "${text}". ${(errors || []).length ? `The forums say: ${listed(errors)}` : 'The forums show no error'} (${this.page.url()}).`,
498
+ hint: 'The errors are the red notice above the form the forums refused.',
499
+ }), 3000);
500
+ });
501
+
502
+ /**
503
+ * Reply to the topic that is open: write the text in its reply form and
504
+ * submit it; the step waits for the reply on the page.
505
+ *
506
+ * Example #1: When I reply to the topic with "Webship test reply."
507
+ * Example #2: And I reply to the topic with "Webship test: a second reply."
508
+ * Example #3: When we reply to the topic with "Thank you, that worked."
509
+ * Example #4: When I reply to the topic with "Webship test reply of the moderator."
510
+ * Example #5: And we reply to the topic with "Webship test: see the forum rules."
511
+ */
512
+ When(/^(I |we )*reply to the topic with "([^"]*)"$/, async function (pronounCase, text) {
513
+ await readingTopic(this);
514
+ const area = this.page.locator(`${FORUMS} form#new-post #bbp_reply_content`);
515
+ if (!(await area.count())) {
516
+ const notices = (await forumNotices(this)).filter((n) => n.kind !== 'info').map((n) => n.text);
517
+ throw friendly({
518
+ message: `The topic offers this account no reply form${notices.length ? `: the forums say ${listed(notices)}` : ''}.`,
519
+ hint: 'A participant may reply to an open topic; a visitor and a spectator may not, and a closed topic takes replies from moderators only.',
520
+ });
521
+ }
522
+ await area.fill(text);
523
+ await clickAndLoad(this, this.page.locator(`${FORUMS} form#new-post #bbp_reply_submit`), 'submit the reply');
524
+ const errors = await formErrors(this);
525
+ if (errors.length) {
526
+ throw friendly({ message: `The forums refused the reply: ${listed(errors)}.`, hint: 'The forums refuse an empty reply, the same reply twice, and a reply sent right after another when their flood protection is on.' });
527
+ }
528
+ if (!/#post-\d+/.test(this.page.url())) {
529
+ throw friendly({ message: `The forums did not answer with the reply: the browser is at ${this.page.url()}.`, hint: 'A reply that was taken lands on the topic at the reply (…/#post-<number>).' });
530
+ }
531
+ });
532
+
533
+ /**
534
+ * Open the edit form of a post of the page, by its number or the last one.
535
+ *
536
+ * Example #1: When I edit post 1
537
+ * Example #2: And I edit the last post
538
+ * Example #3: When we edit post 2
539
+ * Example #4: When I edit post 3
540
+ * Example #5: And we edit the last post
541
+ */
542
+ When(/^(I |we )*edit (?:post (\d+)|the last post)$/, async function (pronounCase, number) {
543
+ const { post, label } = await postOnPage(this, number);
544
+ const link = post.links.find((l) => l.name === 'bbp-topic-edit-link' || l.name === 'bbp-reply-edit-link');
545
+ if (!link) {
546
+ throw friendly({
547
+ message: `${label} offers this account no Edit link. It offers: ${listed(post.links.map((l) => l.text))}.`,
548
+ hint: 'An account may edit its own posts (for the time the forums\' "Editing" setting gives), a moderator every post.',
549
+ });
550
+ }
551
+ await navigate(this, link.href);
552
+ await postForm(this);
553
+ });
554
+
555
+ /**
556
+ * Assert the forums offer (or do not offer) a link on a post, by its text:
557
+ * Edit, Reply, Merge, Close, Stick, Trash, Spam, Unapprove, Move, Split.
558
+ *
559
+ * Example #1: Then post 1 should offer the link "Edit"
560
+ * Example #2: And post 2 should not offer the link "Trash"
561
+ * Example #3: Then the last post should offer the link "Split"
562
+ * Example #4: Then post 1 should not offer the link "Merge"
563
+ * Example #5: And the last post should offer the link "Reply"
564
+ */
565
+ Then(/^(?:post (\d+)|the last post) should( not)* offer the link "([^"]*)"$/, async function (number, notCase, text) {
566
+ await settle(this);
567
+ const { post, label } = await postOnPage(this, number);
568
+ if (post.links.some((l) => same(l.text, text)) === Boolean(notCase)) {
569
+ throw friendly({
570
+ message: notCase
571
+ ? `Expected ${label.toLowerCase()} not to offer "${text}", but it does.`
572
+ : `Expected ${label.toLowerCase()} to offer "${text}". It offers: ${listed(post.links.map((l) => l.text))}.`,
573
+ hint: 'The links of a post depend on the forum role: a participant gets Edit on its own posts and Reply, a moderator every moderation link.',
574
+ });
575
+ }
576
+ });
577
+