@webship-js/mediawiki-steps 0.0.0-stage → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +550 -2
  3. package/package.json +63 -4
  4. package/tests/features/README.md +26 -0
  5. package/tests/features/accessibility.feature +16 -0
  6. package/tests/features/account-creation.feature +28 -0
  7. package/tests/features/administration.feature +49 -0
  8. package/tests/features/categories.feature +28 -0
  9. package/tests/features/edit-conflict.feature +15 -0
  10. package/tests/features/history.feature +30 -0
  11. package/tests/features/preferences.feature +15 -0
  12. package/tests/features/reading.feature +84 -0
  13. package/tests/features/search.feature +34 -0
  14. package/tests/features/session.feature +43 -0
  15. package/tests/features/source-editor.feature +42 -0
  16. package/tests/features/talk-page.feature +19 -0
  17. package/tests/features/upload.feature +20 -0
  18. package/tests/features/visual-editor.feature +18 -0
  19. package/tests/features/watchlist.feature +24 -0
  20. package/tests/fixtures/blue-square.png +0 -0
  21. package/tests/step-definitions/README.md +41 -0
  22. package/tests/step-definitions/mediawiki-admin.steps.js +241 -0
  23. package/tests/step-definitions/mediawiki-editing.steps.js +413 -0
  24. package/tests/step-definitions/mediawiki-files.steps.js +93 -0
  25. package/tests/step-definitions/mediawiki-helpers.js +768 -0
  26. package/tests/step-definitions/mediawiki-history.steps.js +135 -0
  27. package/tests/step-definitions/mediawiki-pages.steps.js +710 -0
  28. package/tests/step-definitions/mediawiki-search.steps.js +234 -0
  29. package/tests/step-definitions/mediawiki-session.steps.js +287 -0
  30. package/tests/step-definitions/mediawiki-watchlist.steps.js +265 -0
@@ -0,0 +1,234 @@
1
+ 'use strict';
2
+
3
+ // Search: the search box of the skin with its suggestions (they load late: the
4
+ // skin loads its search module when the box gets the focus, then asks the wiki
5
+ // for titles), choosing a suggestion, a full search, and the results page.
6
+ //
7
+ // Waits: the skin's search module through the wiki's module loader
8
+ // (`mw.loader.using()`), then the answer to the suggestions request
9
+ // (rest.php/v1/search/title for the default skin, api.php action=opensearch for
10
+ // the others). No sleeps.
11
+
12
+ const { When, Then } = require('@cucumber/cucumber');
13
+ const { friendly } = require('webship-js/tests/step-definitions/webship');
14
+ const {
15
+ budget,
16
+ clean,
17
+ listed,
18
+ titleText,
19
+ config,
20
+ cutRequest,
21
+ CUT_HINT,
22
+ settle,
23
+ waitForModules,
24
+ afterNavigation,
25
+ eventually,
26
+ pageSummary,
27
+ } = require('./mediawiki-helpers');
28
+
29
+ /** The search module of the skin on screen. */
30
+ async function searchModule(world) {
31
+ const skin = await config(world, 'skin');
32
+ return ({ 'vector-2022': 'skins.vector.search', minerva: 'skins.minerva.search' })[skin] || 'mediawiki.searchSuggest';
33
+ }
34
+
35
+ /** The search box a person types in (the skin replaces it once its search module runs). */
36
+ function searchBox(world) {
37
+ return world.page.locator('#p-search input[name="search"], #searchform input[name="search"], #simpleSearch input[name="search"], input#searchInput').filter({ visible: true }).first();
38
+ }
39
+
40
+ /** The suggestions of the search box: the titles it offers, without the "Search for pages containing" entry. */
41
+ async function suggestions(world) {
42
+ return world.page.evaluate(() => {
43
+ const codex = [...document.querySelectorAll('#p-search [role="option"], .vector-search-box [role="option"]')]
44
+ .filter((o) => o.offsetParent && !o.closest('.cdx-typeahead-search__search-footer') && !/cdx-typeahead-search__search-footer/.test(o.className))
45
+ .map((o) => (o.querySelector('.cdx-menu-item__text__label, .cdx-search-result-title') || o).textContent.replace(/\s+/g, ' ').trim());
46
+ const legacy = [...document.querySelectorAll('.suggestions .suggestions-result')].filter((o) => o.offsetParent).map((o) => o.textContent.replace(/\s+/g, ' ').trim());
47
+ return codex.concat(legacy).filter((t) => t && !/^Search for pages containing/i.test(t));
48
+ }).catch(() => []);
49
+ }
50
+
51
+ /**
52
+ * Type in the search box of the skin, as a person does: the box gets the focus,
53
+ * the skin's search module loads, and every letter is typed. The step waits for
54
+ * the wiki's answer to the suggestions request.
55
+ *
56
+ * Example #1: When I type "Readi" in the search box
57
+ * Example #2: When we type "Second test" in the search box
58
+ * Example #3: And I type "Main" in the search box
59
+ * Example #4: When I type "Category:Test" in the search box
60
+ * Example #5: When I type "zzqq" in the search box
61
+ */
62
+ When(/^(I |we )*type "([^"]*)" in the search box$/, { timeout: 60000 }, async function (pronounCase, text) {
63
+ const page = this.page;
64
+ let box = searchBox(this);
65
+ if (!(await box.count())) {
66
+ // A narrow default skin folds the box behind a search button.
67
+ const toggle = page.locator('#p-search .search-toggle, .vector-search-box .search-toggle').filter({ visible: true });
68
+ if (await toggle.count()) await toggle.first().click();
69
+ box = searchBox(this);
70
+ }
71
+ if (!(await box.count())) throw friendly({ message: `The page has no search box: ${await pageSummary(this)}.`, hint: cutRequest(this) ? CUT_HINT : 'Open a page of the wiki first.' });
72
+ await box.click();
73
+ await waitForModules(this, await searchModule(this), { what: 'The search box of the skin' });
74
+ box = searchBox(this);
75
+ const answered = page.waitForResponse((r) => /\/rest\.php\/v1\/search\/title|action=opensearch|list=prefixsearch/.test(r.url()), { timeout: budget(this) }).catch(() => null);
76
+ await box.fill('');
77
+ await box.pressSequentially(text, { delay: 30 });
78
+ const response = await answered;
79
+ if (response && !/json/.test(response.headers()['content-type'] || '')) {
80
+ throw friendly({ message: `The wiki answered the suggestions request (${response.url()}) with ${response.headers()['content-type'] || 'no content type'}, not JSON.`, hint: 'On nginx, rest.php/v1/... needs the rule MediaWiki documents: location /rest.php/ { try_files $uri $uri/ /rest.php?$query_string; }.' });
81
+ }
82
+ await settle(this, 1000);
83
+ });
84
+
85
+ /**
86
+ * Assert that the suggestions of the search box list, or do not list, a page by
87
+ * its title.
88
+ *
89
+ * Example #1: Then the search suggestions should list "Reading test page"
90
+ * Example #2: Then the search suggestions should not list "Second test page"
91
+ * Example #3: And the search suggestions should list "Main Page"
92
+ * Example #4: Then the search suggestions should list "Category:Test guides"
93
+ * Example #5: And the search suggestions should not list "Reading test page"
94
+ */
95
+ Then(/^the search suggestions should( not)* list "([^"]*)"$/, async function (notCase, title) {
96
+ const want = titleText(title);
97
+ await eventually(this, () => suggestions(this), (list) => !!notCase !== list.includes(want),
98
+ (list) => ({ message: notCase ? `The search suggestions list "${want}".` : `The search suggestions do not list "${want}". They list: ${listed(list)}.`, hint: 'Type in the search box first ("When I type ... in the search box"): the suggestions are titles that start with what was typed.' }));
99
+ });
100
+
101
+ /**
102
+ * Choose a suggestion of the search box: the wiki opens that page.
103
+ *
104
+ * Example #1: When I choose the suggestion "Reading test page"
105
+ * Example #2: When we choose the suggestion "Main Page"
106
+ * Example #3: And I choose the suggestion "Second test page"
107
+ * Example #4: When I choose the suggestion "Category:Test guides"
108
+ * Example #5: When we choose the suggestion "Reading test page"
109
+ */
110
+ When(/^(I |we )*choose the suggestion "([^"]*)"$/, { timeout: 60000 }, async function (pronounCase, title) {
111
+ const want = titleText(title);
112
+ await eventually(this, () => suggestions(this), (list) => list.includes(want),
113
+ (list) => ({ message: `The search suggestions do not list "${want}". They list: ${listed(list)}.`, hint: 'Type in the search box first.' }));
114
+ const option = this.page.locator('#p-search [role="option"], .vector-search-box [role="option"], .suggestions .suggestions-result').filter({ visible: true }).filter({ hasText: want }).first();
115
+ await afterNavigation(this, () => option.click());
116
+ if (cutRequest(this)) throw friendly({ message: `The suggestion "${want}" did not open: ${cutRequest(this)}.`, hint: CUT_HINT });
117
+ });
118
+
119
+ /**
120
+ * Search the wiki for pages that contain words, as a person does: type the words
121
+ * in the search box and choose "Search for pages containing …". The browser ends
122
+ * on the results page.
123
+ *
124
+ * Example #1: When I search for "readers"
125
+ * Example #2: When we search for "conflict line"
126
+ * Example #3: And I search for "history"
127
+ * Example #4: When I search for "zzqqnothing"
128
+ * Example #5: When we search for "second version"
129
+ */
130
+ When(/^(I |we )*search for "([^"]*)"$/, { timeout: 90000 }, async function (pronounCase, words) {
131
+ const page = this.page;
132
+ if (!(await page.evaluate(() => !!window.mw).catch(() => false))) {
133
+ throw friendly({ message: `No page of the wiki is open: ${await pageSummary(this)}.`, hint: 'Open a page of the wiki first.' });
134
+ }
135
+ let box = searchBox(this);
136
+ if (!(await box.count())) {
137
+ const toggle = page.locator('#p-search .search-toggle').filter({ visible: true });
138
+ if (await toggle.count()) await toggle.first().click();
139
+ box = searchBox(this);
140
+ }
141
+ await box.click();
142
+ await waitForModules(this, await searchModule(this), { what: 'The search box of the skin' });
143
+ box = searchBox(this);
144
+ const answered = page.waitForResponse((r) => /\/rest\.php\/v1\/search\/title|action=opensearch/.test(r.url()), { timeout: budget(this) }).catch(() => null);
145
+ await box.fill('');
146
+ await box.pressSequentially(words, { delay: 30 });
147
+ await answered;
148
+ const containing = page.locator('#p-search .cdx-typeahead-search__search-footer, #p-search [role="option"]:has-text("Search for pages containing"), .suggestions .suggestions-special').filter({ visible: true }).first();
149
+ if (await containing.count()) {
150
+ await afterNavigation(this, () => containing.click());
151
+ } else {
152
+ // No suggestions list (a skin without one): the search form's own full search.
153
+ await afterNavigation(this, () => box.press('Enter'));
154
+ }
155
+ if (cutRequest(this)) throw friendly({ message: `The search did not open: ${cutRequest(this)}.`, hint: CUT_HINT });
156
+ const special = await config(this, 'wgCanonicalSpecialPageName');
157
+ if (special !== 'Search') {
158
+ throw friendly({ message: `The search for "${words}" did not open the results page: ${await pageSummary(this)}.`, hint: 'A title that matches the words exactly opens that page instead; choose words that are not a title.' });
159
+ }
160
+ });
161
+
162
+ /** The results of the results page: the titles found and the snippet of each. */
163
+ async function results(world) {
164
+ return world.page.evaluate(() => [...document.querySelectorAll('.mw-search-results .mw-search-result')].map((li) => ({
165
+ title: (li.querySelector('.mw-search-result-heading a') || {}).textContent || '',
166
+ snippet: ((li.querySelector('.searchresult') || {}).textContent || '').replace(/\s+/g, ' ').trim(),
167
+ matches: [...li.querySelectorAll('.searchmatch')].map((m) => m.textContent.trim()),
168
+ }))).catch(() => []);
169
+ }
170
+
171
+ /**
172
+ * Assert that the results page lists, or does not list, a page.
173
+ *
174
+ * Example #1: Then the search results should list the page "Reading test page"
175
+ * Example #2: Then the search results should not list the page "Second test page"
176
+ * Example #3: And the search results should list the page "Second test page"
177
+ * Example #4: Then the search results should list the page "History test page"
178
+ * Example #5: And the search results should not list the page "Main Page"
179
+ */
180
+ Then(/^the search results should( not)* list the page "([^"]*)"$/, async function (notCase, title) {
181
+ const want = titleText(title);
182
+ const found = await results(this);
183
+ if (!!notCase === found.some((r) => r.title.trim() === want)) {
184
+ throw friendly({ message: notCase ? `The search results list "${want}".` : `The search results do not list "${want}". They list: ${listed(found.map((r) => r.title))}.`, hint: 'Search first ("When I search for ..."); the wiki finds a page by the words of its text and title.' });
185
+ }
186
+ });
187
+
188
+ /**
189
+ * Assert that a result of the results page shows a word of the search
190
+ * highlighted in its snippet.
191
+ *
192
+ * Example #1: Then the search result "Reading test page" should mention "readers"
193
+ * Example #2: And the search result "Second test page" should mention "version"
194
+ * Example #3: Then the search result "History test page" should mention "history"
195
+ * Example #4: Then the search result "Reading test page" should mention "wiki"
196
+ * Example #5: And the search result "Reading test page" should mention "readers"
197
+ */
198
+ Then(/^the search result "([^"]*)" should mention "([^"]*)"$/, async function (title, word) {
199
+ const want = titleText(title);
200
+ const found = await results(this);
201
+ const result = found.find((r) => r.title.trim() === want);
202
+ if (!result || !result.matches.some((m) => m.toLowerCase() === clean(word).toLowerCase())) {
203
+ throw friendly({
204
+ message: result ? `The result "${want}" does not highlight "${word}"; it highlights ${listed(result.matches)} in "${result.snippet.slice(0, 200)}".` : `The search results do not list "${want}". They list: ${listed(found.map((r) => r.title))}.`,
205
+ hint: 'The results page highlights the words of the search in each snippet.',
206
+ });
207
+ }
208
+ });
209
+
210
+ /**
211
+ * Assert how many pages the search found, as the results page counts them, or
212
+ * that it found none ("There were no results matching the query.").
213
+ *
214
+ * Example #1: Then the search should find 1 page
215
+ * Example #2: Then the search should find 2 pages
216
+ * Example #3: Then the search should find no page
217
+ * Example #4: And the search should find 3 pages
218
+ * Example #5: And the search should find no page
219
+ */
220
+ Then(/^the search should find (\d+|no) pages?$/, async function (count) {
221
+ const want = count === 'no' ? 0 : Number(count);
222
+ const state = await this.page.evaluate(() => ({
223
+ none: !!document.querySelector('.mw-search-nonefound'),
224
+ total: (() => { const i = document.querySelector('.results-info'); return i && i.dataset && i.dataset.mwNumResultsTotal ? Number(i.dataset.mwNumResultsTotal) : null; })(),
225
+ listed: document.querySelectorAll('.mw-search-results .mw-search-result').length,
226
+ info: ((document.querySelector('.results-info') || document.querySelector('.mw-search-nonefound') || {}).textContent || '').replace(/\s+/g, ' ').trim(),
227
+ })).catch(() => ({ none: false, total: null, listed: 0, info: '' }));
228
+ const got = state.none ? 0 : (state.total !== null ? state.total : state.listed);
229
+ if (got !== want || (want === 0 && !state.none)) {
230
+ throw friendly({ message: `The search found ${got} page(s), not ${want}. The results page says: "${state.info}".`, hint: 'Search first ("When I search for ..."). A test page is found once the wiki has indexed it, at the save.' });
231
+ }
232
+ });
233
+
234
+ module.exports = { suggestions, results };
@@ -0,0 +1,287 @@
1
+ 'use strict';
2
+
3
+ // The session: logging in by a handle of the users registry or with a user name
4
+ // and a password, logging out, who is signed in, and the refusals of the account
5
+ // creation form.
6
+ //
7
+ // Accounts are configuration, not test data: they are listed in cucumber.js
8
+ // (`worldParameters.users`) and created once by the build of the wiki. A feature
9
+ // never creates or deletes an account: the account creation steps refuse to send
10
+ // a form that the wiki could accept.
11
+
12
+ const { Given, When, Then } = require('@cucumber/cucumber');
13
+ const { friendly } = require('webship-js/tests/step-definitions/webship');
14
+ const {
15
+ budget,
16
+ clean,
17
+ listed,
18
+ titleKey,
19
+ config,
20
+ cutRequest,
21
+ CUT_HINT,
22
+ openPage,
23
+ afterNavigation,
24
+ pageSummary,
25
+ messages,
26
+ hasMessage,
27
+ resolveUser,
28
+ accountName,
29
+ signedInName,
30
+ LOGIN_HINT,
31
+ THROTTLE,
32
+ submitLogin,
33
+ logInAs,
34
+ forgetSession,
35
+ waitForModules,
36
+ settle,
37
+ adminApi,
38
+ } = require('./mediawiki-helpers');
39
+
40
+ /**
41
+ * Log in as an account of the users registry (`worldParameters.users` in
42
+ * cucumber.js), by its handle. An account that logged in earlier in the run
43
+ * takes its session over; otherwise the step logs in through the login form.
44
+ *
45
+ * Example #1: Given I am a logged in user with the "editor" user
46
+ * Example #2: Given we are a logged in user with the "administrator" user
47
+ * Example #3: Given I am a logged in user with "reader"
48
+ * Example #4: And I am a logged in user with the "reader" user
49
+ * Example #5: Given we are a logged in user with the "editor" user
50
+ */
51
+ Given(/^(I am |we are )*a logged in user with( the)* "([^"]*)"( user)*$/, { timeout: 120000 }, async function (pronounCase, theCase, handle, userCase) {
52
+ await logInAs(this, handle);
53
+ });
54
+
55
+ /**
56
+ * Log in through the login form as an account of the users registry, as a
57
+ * person does: a new session, never one taken over. For the scenarios that
58
+ * test the form itself; the step fails when the wiki refuses the login. "and
59
+ * ask to be kept logged in" ticks "Keep me logged in" first.
60
+ *
61
+ * Example #1: When I log in as "editor" with the login form
62
+ * Example #2: When we log in as "reader" with the login form
63
+ * Example #3: When I log in as "editor" with the login form and ask to be kept logged in
64
+ * Example #4: And I log in as "administrator" with the login form
65
+ * Example #5: When we log in as "reader" with the login form and ask to be kept logged in
66
+ */
67
+ When(/^(I |we )*log in as "([^"]*)" with the login form( and ask to be kept logged in)?$/, { timeout: 120000 }, async function (pronounCase, handle, rememberCase) {
68
+ const user = resolveUser(this, handle);
69
+ const outcome = await submitLogin(this, user.username, user.password, { remember: !!rememberCase });
70
+ if (outcome.name !== user.wikiName) {
71
+ throw friendly({
72
+ message: `The login form refused "${handle}" (${user.username}): ${listed(outcome.errors)}.`,
73
+ hint: outcome.cut ? CUT_HINT : 'Check the user name and the password of the account in worldParameters.users, and that the build of the wiki created the account.',
74
+ });
75
+ }
76
+ });
77
+
78
+ /**
79
+ * Send the login form with a user name and a password. The step does not expect
80
+ * the login to succeed: assert the outcome with the next step. The wiki allows 5
81
+ * failed logins of one account from one address in 5 minutes, so a refusal
82
+ * scenario tries a name the other scenarios do not log in with.
83
+ *
84
+ * Example #1: When I log in with the user name "No such account here" and the password "Wrong-password-1"
85
+ * Example #2: When we log in with the user name "reader" and the password "Not-the-password-2"
86
+ * Example #3: And I log in with the user name "Nobody at all" and the password "Whatever-123!"
87
+ * Example #4: When I log in with the user name "" and the password "Wrong-password-1"
88
+ * Example #5: When we log in with the user name "Someone unknown" and the password "x"
89
+ */
90
+ When(/^(I |we )*log in with the user name "([^"]*)" and the password "([^"]*)"$/, { timeout: 120000 }, async function (pronounCase, username, password) {
91
+ this.__loginOutcome = await submitLogin(this, username, password);
92
+ });
93
+
94
+ /**
95
+ * Assert that the login page refused the login with an error message (or a part
96
+ * of it), and that nobody is signed in.
97
+ *
98
+ * Example #1: Then the login should be refused with "Incorrect username or password entered. Please try again."
99
+ * Example #2: Then the login should be refused with "Incorrect username or password entered."
100
+ * Example #3: And the login should be refused with "Please try again."
101
+ * Example #4: Then the login should be refused with "Incorrect username or password"
102
+ * Example #5: Then the login should be refused with "You have made too many recent login attempts."
103
+ */
104
+ Then(/^the login should be refused with "([^"]*)"$/, async function (expected) {
105
+ const name = await signedInName(this);
106
+ const errors = await messages(this, 'error');
107
+ const onLogin = titleKey(String(await config(this, 'wgCanonicalSpecialPageName') || '')) === 'Userlogin';
108
+ if (name || !onLogin || !hasMessage(errors, expected)) {
109
+ throw friendly({
110
+ message: name
111
+ ? `The login was not refused: the wiki says "${name}" is signed in.`
112
+ : `The login page did not refuse the login with "${expected}". Its errors: ${listed(errors)}; ${await pageSummary(this)}.`,
113
+ hint: cutRequest(this) ? CUT_HINT : 'Send the form first ("When I log in with the user name ... and the password ..."); the wiki writes one error for a wrong password and an unknown name alike.',
114
+ });
115
+ }
116
+ });
117
+
118
+ /**
119
+ * Log out as a person does: the "Log out" link of the user menu (opened first
120
+ * when the skin folds it), else the link wherever the skin puts it, else the
121
+ * logout page's own button. The step waits for the wiki to end the session.
122
+ *
123
+ * Example #1: When I log out
124
+ * Example #2: When we log out
125
+ * Example #3: And I log out
126
+ * Example #4: Then I log out
127
+ * Example #5: Given I log out
128
+ */
129
+ When(/^(I |we )*log out$/, { timeout: 90000 }, async function (pronounCase) {
130
+ const page = this.page;
131
+ forgetSession(this);
132
+ if (!(await signedInName(this))) await openPage(this, 'Special:BlankPage');
133
+ if (!(await signedInName(this))) return;
134
+ const link = page.locator('#pt-logout a, a#pt-logout, #pt-logout').filter({ visible: true });
135
+ if (!(await link.count())) {
136
+ const menu = page.locator('#vector-user-links-dropdown-checkbox, #p-personal-checkbox, #vector-user-links-dropdown .vector-dropdown-label').filter({ visible: true });
137
+ if (await menu.count()) await menu.first().click();
138
+ }
139
+ const target = page.locator('#pt-logout a, a#pt-logout').filter({ visible: true });
140
+ if (await target.count()) {
141
+ await afterNavigation(this, () => target.first().click());
142
+ } else {
143
+ await openPage(this, 'Special:UserLogout');
144
+ const button = page.getByRole('button', { name: 'Log out' });
145
+ if (await button.count()) await afterNavigation(this, () => button.first().click());
146
+ }
147
+ try {
148
+ await page.waitForFunction(() => window.mw && mw.config && mw.config.get('wgUserName') === null, null, { timeout: budget(this) });
149
+ } catch (err) {
150
+ throw friendly({ message: `Logging out did not end the session: the wiki still says "${await signedInName(this)}" is signed in (${await pageSummary(this)}).`, cause: err, hint: cutRequest(this) ? CUT_HINT : 'The skin offers "Log out" in the user menu; it posts to the API (action=logout) and opens Special:UserLogout.' });
151
+ }
152
+ });
153
+
154
+ /**
155
+ * Assert who is signed in, as the page says (`wgUserName`): an account of the
156
+ * users registry by its handle, or nobody. A page that is not the wiki's (the
157
+ * browser's own error page) is replaced by Special:BlankPage first.
158
+ *
159
+ * Example #1: Then I should be logged in as "editor"
160
+ * Example #2: Then we should be logged in as "administrator"
161
+ * Example #3: And I should be logged in as "reader"
162
+ * Example #4: Then I should not be logged in as "editor"
163
+ * Example #5: Then we should not be logged in as "reader"
164
+ */
165
+ Then(/^(I |we )*should( not)* be logged in as "([^"]*)"$/, async function (pronounCase, notCase, handle) {
166
+ const user = resolveUser(this, handle);
167
+ if ((await config(this, 'wgUserName')) === null && !(await this.page.evaluate(() => !!window.mw).catch(() => false))) await openPage(this, 'Special:BlankPage');
168
+ const name = await signedInName(this);
169
+ if (!!notCase === (name === user.wikiName)) {
170
+ throw friendly({
171
+ message: notCase ? `"${handle}" (${user.wikiName}) is signed in.` : `Expected "${handle}" (${user.wikiName}) to be signed in; the wiki says ${name ? `"${name}"` : 'nobody'} is.`,
172
+ hint: 'Log in with "Given I am a logged in user with the ... user" or the login form steps.',
173
+ });
174
+ }
175
+ });
176
+
177
+ /**
178
+ * Assert that somebody is, or nobody is, signed in, as the page says (`wgUserName`).
179
+ *
180
+ * Example #1: Then I should be logged in
181
+ * Example #2: Then I should not be logged in
182
+ * Example #3: And we should be logged in
183
+ * Example #4: And we should not be logged in
184
+ * Example #5: Then nobody should be logged in
185
+ */
186
+ Then(/^(?:(I |we )*should( not)* be logged in|(nobody) should be logged in)$/, async function (pronounCase, notCase, nobodyCase) {
187
+ if (!(await this.page.evaluate(() => !!window.mw).catch(() => false))) await openPage(this, 'Special:BlankPage');
188
+ const name = await signedInName(this);
189
+ const expectNobody = !!(notCase || nobodyCase);
190
+ if (expectNobody === !!name) {
191
+ throw friendly({
192
+ message: expectNobody ? `Expected nobody to be signed in; the wiki says "${name}" is.` : `Expected an account to be signed in; nobody is (${await pageSummary(this)}).`,
193
+ hint: 'The page says who is signed in through mw.config wgUserName.',
194
+ });
195
+ }
196
+ });
197
+
198
+ /**
199
+ * Try to create an account with the account creation form (Special:CreateAccount),
200
+ * and send it only when the wiki will refuse it: the passwords differ, or the
201
+ * form's own checks already show a problem (a name in use, a password the wiki
202
+ * refuses). A form the wiki could accept is not sent, and the step fails: a suite
203
+ * never creates an account. A handle of the users registry stands for its name.
204
+ *
205
+ * Example #1: When I try to create the account "Never created account" with the password "Unique-pass-2026-x" and the confirmation "Unique-pass-2026-y"
206
+ * Example #2: When I try to create the account "editor" with the password "Unique-pass-2026-x" and the confirmation "Unique-pass-2026-x"
207
+ * Example #3: When we try to create the account "Never created account" with the password "abc" and the confirmation "abc"
208
+ * Example #4: And I try to create the account "reader" with the password "Another-pass-2026" and the confirmation "Another-pass-2026"
209
+ * Example #5: When I try to create the account "Never created account" with the password "password" and the confirmation "password"
210
+ */
211
+ When(/^(I |we )*try to create the account "([^"]*)" with the password "([^"]*)" and the confirmation "([^"]*)"$/, { timeout: 120000 }, async function (pronounCase, who, password, retype) {
212
+ const page = this.page;
213
+ const name = accountName(this, who);
214
+ await openPage(this, 'Special:CreateAccount');
215
+ if (!(await page.locator('#wpName2').count())) {
216
+ throw friendly({ message: `The account creation form is not on Special:CreateAccount: ${await pageSummary(this)}.`, hint: 'A visitor may create accounts on a default wiki; a signed-in account sees the form only with the "createaccount" right.' });
217
+ }
218
+ await waitForModules(this, 'mediawiki.special.createaccount', { what: 'The checks of the account creation form' });
219
+ // The form checks the name and the password with the API while they are typed (list=users,
220
+ // action=validatepassword): wait for each answer, as a person waits for the message under the field.
221
+ const answer = (re) => page.waitForResponse((r) => /api\.php/.test(r.url()) && re.test(r.url() + ' ' + (r.request().postData() || '')), { timeout: budget(this) }).catch(() => null);
222
+ const nameChecked = answer(/ususers=|list=users/);
223
+ await page.locator('#wpName2').fill(name);
224
+ await page.locator('#wpName2').blur();
225
+ await nameChecked;
226
+ const passwordChecked = answer(/validatepassword/);
227
+ await page.locator('#wpPassword2').fill(password);
228
+ await page.locator('#wpPassword2').blur();
229
+ await passwordChecked;
230
+ await page.locator('#wpRetype').fill(retype);
231
+ await page.locator('#wpRetype').blur();
232
+ await settle(this, 1000);
233
+ const shown = await messages(this, 'error');
234
+ if (password === retype && !shown.length) {
235
+ let exists = false;
236
+ try {
237
+ const api = await adminApi(this);
238
+ const body = await api.call({ action: 'query', list: 'users', ususers: name });
239
+ exists = !!(body.query && body.query.users && body.query.users[0] && !body.query.users[0].missing);
240
+ } catch { exists = false; }
241
+ if (!exists) {
242
+ throw friendly({
243
+ message: `The form shows no problem with the account "${name}": sending it would create the account, so the step did not send it.`,
244
+ hint: 'A suite never creates an account. Test the refusals: different passwords, a name in use (a handle of the users registry), or a password the wiki refuses ("abc", "password").',
245
+ });
246
+ }
247
+ }
248
+ this.__accountTried = name;
249
+ await afterNavigation(this, () => page.locator('#wpCreateaccount').click());
250
+ });
251
+
252
+ /**
253
+ * Assert that the account creation form refused the account with an error
254
+ * message (or a part of it), and that the account does not exist unless its
255
+ * name was already in use.
256
+ *
257
+ * Example #1: Then the account creation should be refused with "The passwords you entered do not match."
258
+ * Example #2: Then the account creation should be refused with "Username entered already in use. Please choose a different name."
259
+ * Example #3: Then the account creation should be refused with "Passwords must be at least 8 characters."
260
+ * Example #4: And the account creation should be refused with "There are problems with some of your input."
261
+ * Example #5: Then the account creation should be refused with "The password entered is in a list of very commonly used passwords."
262
+ */
263
+ Then(/^the account creation should be refused with "([^"]*)"$/, async function (expected) {
264
+ const errors = await messages(this, 'error');
265
+ const special = String(await config(this, 'wgCanonicalSpecialPageName') || '');
266
+ if (!/^CreateAccount$/i.test(special) || !hasMessage(errors, expected)) {
267
+ throw friendly({
268
+ message: `The account creation form did not refuse with "${expected}". Its errors: ${listed(errors)}; ${await pageSummary(this)}.`,
269
+ hint: cutRequest(this) ? CUT_HINT : 'Send the form with "When I try to create the account ..." first.',
270
+ });
271
+ }
272
+ const name = this.__accountTried;
273
+ if (name) {
274
+ const users = (this.parameters && this.parameters.users) || {};
275
+ const known = Object.values(users).some((u) => clean(u.username).toLowerCase() === clean(name).toLowerCase());
276
+ if (!known) {
277
+ const api = await adminApi(this);
278
+ const body = await api.call({ action: 'query', list: 'users', ususers: name });
279
+ const user = body.query && body.query.users && body.query.users[0];
280
+ if (user && !user.missing) {
281
+ throw friendly({ message: `The account "${name}" exists on the wiki after the refusal.`, hint: 'The wiki created the account: remove it by hand (a wiki keeps accounts; block it), and check the scenario.' });
282
+ }
283
+ }
284
+ }
285
+ });
286
+
287
+ module.exports = {};