@webship-js/leantime-steps 0.0.0-stage → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +255 -2
- package/package.json +64 -4
- package/tests/features/roles.feature +25 -0
- package/tests/features/session.feature +29 -0
- package/tests/features/sprints.feature +9 -0
- package/tests/features/tasks.feature +36 -0
- package/tests/step-definitions/leantime-helpers.js +666 -0
- package/tests/step-definitions/leantime-session.steps.js +185 -0
- package/tests/step-definitions/leantime-sprints.steps.js +42 -0
- package/tests/step-definitions/leantime-tasks.steps.js +337 -0
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Shared helpers for the step files of this pack.
|
|
4
|
+
//
|
|
5
|
+
// Not a step file: it registers no step definitions and no hooks. It holds the plumbing the steps use and that
|
|
6
|
+
// another pack can require instead of copying: the wait budget, presence assertions with friendly errors, URL
|
|
7
|
+
// building, a page's tour closed when it shows, the users registry, signing in (the form, and a session put back),
|
|
8
|
+
// the site's own API as the managing account (the test content a scenario makes and removes), the open task found by
|
|
9
|
+
// its own address, and the tasks a scenario made, which go after it.
|
|
10
|
+
//
|
|
11
|
+
// Everything here builds on webship-js's own helpers (`gotoUrl`, `smartSettle`, `waitForPageLoad`, `friendly`) so the
|
|
12
|
+
// steps behave like the core webship-js steps.
|
|
13
|
+
|
|
14
|
+
const { gotoUrl, smartSettle, waitForPageLoad, friendly } = require('webship-js/tests/step-definitions/webship');
|
|
15
|
+
|
|
16
|
+
let playwright;
|
|
17
|
+
try { playwright = require('playwright'); } catch { playwright = require(require.resolve('playwright', { paths: [require.resolve('webship-js/package.json')] })); }
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The per-assertion wait budget: `minWaitTime.page` from cucumber.js, at least 15 seconds (the site's screens load
|
|
21
|
+
* their data after the page), so a slow screen does not fail an assertion that would pass a moment later. Kept below
|
|
22
|
+
* webship-js's 45 second step timeout.
|
|
23
|
+
*
|
|
24
|
+
* @param {object} world
|
|
25
|
+
* @returns {number}
|
|
26
|
+
*/
|
|
27
|
+
function budget(world) {
|
|
28
|
+
return Math.max((world && world.minWaitTime && world.minWaitTime.page) || 0, 15000);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Escape a literal string for use in a RegExp.
|
|
33
|
+
*
|
|
34
|
+
* @param {string} text
|
|
35
|
+
* @returns {string}
|
|
36
|
+
*/
|
|
37
|
+
function escapeRegExp(text) {
|
|
38
|
+
return String(text).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A RegExp that matches the whole text (ignoring surrounding space and case), for exact names and labels.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} text
|
|
45
|
+
* @returns {RegExp}
|
|
46
|
+
*/
|
|
47
|
+
function exactly(text) {
|
|
48
|
+
return new RegExp('^\\s*' + escapeRegExp(text) + '\\s*$', 'i');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Collapse runs of white space and trim.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} text
|
|
55
|
+
* @returns {string}
|
|
56
|
+
*/
|
|
57
|
+
function tidy(text) {
|
|
58
|
+
return String(text === null || text === undefined ? '' : text).replace(/\s+/g, ' ').trim();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Run an action and rethrow its failure as a friendly error (action, target, cause and a hint) instead of
|
|
63
|
+
* Playwright's raw timeout stack.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} label
|
|
66
|
+
* @param {string} target
|
|
67
|
+
* @param {Function} fn
|
|
68
|
+
* @param {string} hint
|
|
69
|
+
*/
|
|
70
|
+
async function actOrExplain(label, target, fn, hint) {
|
|
71
|
+
try {
|
|
72
|
+
return await fn();
|
|
73
|
+
} catch (err) {
|
|
74
|
+
if (err && err.stack === err.message && /Hint:/.test(err.message)) throw err;
|
|
75
|
+
throw friendly({ action: label, target, cause: err, hint });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Wait until a locator has (or, with `notCase`, no longer has) a visible match and fail with a friendly error
|
|
81
|
+
* otherwise: the wait and the assertion are one call, so content that arrives a moment late still passes.
|
|
82
|
+
*
|
|
83
|
+
* @param {import('playwright').Locator} locator
|
|
84
|
+
* @param {string|null|undefined} notCase
|
|
85
|
+
* @param {{label: string, hint: string, timeout: number}} options
|
|
86
|
+
*/
|
|
87
|
+
async function assertPresence(locator, notCase, { label, hint, timeout }) {
|
|
88
|
+
try {
|
|
89
|
+
await locator.first().waitFor({ state: notCase ? 'hidden' : 'visible', timeout });
|
|
90
|
+
} catch {
|
|
91
|
+
throw friendly({
|
|
92
|
+
message: notCase ? `Expected not to see ${label}, but it is still on the page.` : `Expected to see ${label}, but it did not appear within ${timeout} ms.`,
|
|
93
|
+
hint,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Poll a reader until its value satisfies a test, and return the last value read, so a value that changes a moment
|
|
100
|
+
* after the action (a status after a save) still passes. A read is an answer of the site's API, which the site
|
|
101
|
+
* limits a minute for an account: once a second, by default.
|
|
102
|
+
*
|
|
103
|
+
* @param {Function} read - async () => value
|
|
104
|
+
* @param {Function} test - (value) => boolean
|
|
105
|
+
* @param {number} timeout
|
|
106
|
+
* @param {number} [every] - milliseconds between two reads
|
|
107
|
+
* @returns {Promise<{ok: boolean, value: *}>}
|
|
108
|
+
*/
|
|
109
|
+
async function waitForValue(read, test, timeout, every = 1000) {
|
|
110
|
+
const deadline = Date.now() + timeout;
|
|
111
|
+
let value;
|
|
112
|
+
for (;;) {
|
|
113
|
+
try { value = await read(); } catch { value = undefined; }
|
|
114
|
+
if (value !== undefined && test(value)) return { ok: true, value };
|
|
115
|
+
if (Date.now() >= deadline) return { ok: false, value };
|
|
116
|
+
await new Promise((resolve) => setTimeout(resolve, every));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The site root without a trailing slash.
|
|
122
|
+
*
|
|
123
|
+
* @param {object} world
|
|
124
|
+
* @returns {string}
|
|
125
|
+
*/
|
|
126
|
+
function siteRoot(world) {
|
|
127
|
+
return String(world.launchUrl).replace(/\/+$/, '');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Join the site root and a path with exactly one slash. Absolute URLs pass through.
|
|
132
|
+
*
|
|
133
|
+
* @param {object} world
|
|
134
|
+
* @param {string} url
|
|
135
|
+
* @returns {string}
|
|
136
|
+
*/
|
|
137
|
+
function siteUrl(world, url) {
|
|
138
|
+
if (!url) return siteRoot(world);
|
|
139
|
+
if (/^https?:\/\//i.test(url)) return url;
|
|
140
|
+
return siteRoot(world) + '/' + String(url).replace(/^\/+/, '');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Whether a window of help shows: the site shows a person the one of a screen once a session ("I'll explore on my
|
|
145
|
+
* own" / "Take the tour").
|
|
146
|
+
*
|
|
147
|
+
* @param {object} world
|
|
148
|
+
* @returns {Promise<boolean>}
|
|
149
|
+
*/
|
|
150
|
+
async function tourShown(world) {
|
|
151
|
+
return world.page.getByRole('link', { name: "I'll explore on my own" }).first().isVisible().catch(() => false);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Close a screen's window of help when one shows, by its own button ("I'll explore on my own"), and say so in the
|
|
156
|
+
* run, so a step reads the screen under it. "Don't show this again" is left as it is: that is the person's own
|
|
157
|
+
* setting, which a run does not change. Nothing happens when none shows.
|
|
158
|
+
*
|
|
159
|
+
* @param {object} world
|
|
160
|
+
*/
|
|
161
|
+
async function closeTour(world) {
|
|
162
|
+
const page = world.page;
|
|
163
|
+
if (!(await tourShown(world))) return;
|
|
164
|
+
await page.getByRole('link', { name: "I'll explore on my own" }).first().click().catch(() => {});
|
|
165
|
+
await page.getByRole('link', { name: "I'll explore on my own" }).first().waitFor({ state: 'hidden', timeout: budget(world) }).catch(() => {});
|
|
166
|
+
console.log(`[leantime-steps] a tour of ${page.url().replace(siteRoot(world), '') || '/'} showed and was closed with "I'll explore on my own".`);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Open a page of the site, wait for it to settle, and close a tour if one shows. The site draws parts of its screens
|
|
171
|
+
* after the page has loaded, so a step waits on what it needs next.
|
|
172
|
+
*
|
|
173
|
+
* @param {object} world
|
|
174
|
+
* @param {string} url
|
|
175
|
+
*/
|
|
176
|
+
async function open(world, url) {
|
|
177
|
+
await gotoUrl(world.page, siteUrl(world, url));
|
|
178
|
+
await world.page.waitForSelector('body', { state: 'attached', timeout: budget(world) });
|
|
179
|
+
await waitForPageLoad(world.page, (world.minWaitTime && world.minWaitTime.page) || 3000);
|
|
180
|
+
await closeTour(world);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* A user of the `users` registry in cucumber.js (`worldParameters.users`): its e-mail address (`username`, which the
|
|
185
|
+
* site logs in with), its password, and the whole name the site shows (`name`).
|
|
186
|
+
*
|
|
187
|
+
* @param {object} world
|
|
188
|
+
* @param {string} handle
|
|
189
|
+
* @returns {{username: string, password: string, name: string}}
|
|
190
|
+
*/
|
|
191
|
+
function resolveUser(world, handle) {
|
|
192
|
+
const users = (world.parameters && world.parameters.users) || {};
|
|
193
|
+
const entry = users[handle];
|
|
194
|
+
if (!entry) {
|
|
195
|
+
throw friendly({
|
|
196
|
+
message: `The account "${handle}" is not in the users registry. Known accounts: ${Object.keys(users).join(', ') || '(none)'}.`,
|
|
197
|
+
hint: `Add it to worldParameters.users in cucumber.js: users: { ${handle}: { username: '${handle}@example.com', password: process.env.LEANTIME_${handle.toUpperCase()}_PASSWORD, name: '${handle}' } }. The account itself is made once by the build of the site, never by a feature.`,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
const username = entry.username || handle;
|
|
201
|
+
const password = typeof entry === 'string' ? entry : entry.password;
|
|
202
|
+
if (!password) {
|
|
203
|
+
throw friendly({
|
|
204
|
+
message: `The account "${handle}" has no password in the users registry.`,
|
|
205
|
+
hint: `Set worldParameters.users.${handle}.password in cucumber.js, from an environment variable.`,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
return { username, password, name: entry.name || handle };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* One call of the site's JSON-RPC API in a request context that holds a session: the site takes the session's cookie
|
|
213
|
+
* with the header its own screens send (X-Requested-With). Its result, or the error it gave.
|
|
214
|
+
*
|
|
215
|
+
* @param {import('playwright').APIRequestContext} request
|
|
216
|
+
* @param {string} root
|
|
217
|
+
* @param {string} method - "Tickets.Tickets.getTicket"
|
|
218
|
+
* @param {object} params
|
|
219
|
+
* @returns {Promise<{status: number, result: *, error: *}>}
|
|
220
|
+
*/
|
|
221
|
+
async function rpcWith(request, root, method, params) {
|
|
222
|
+
const r = await request.post(`${root}/api/jsonrpc`, {
|
|
223
|
+
headers: { 'X-Requested-With': 'XMLHttpRequest', Accept: 'application/json' },
|
|
224
|
+
data: { jsonrpc: '2.0', method: `leantime.rpc.${method}`, params: params || {}, id: 1 },
|
|
225
|
+
ignoreHTTPSErrors: true, failOnStatusCode: false, maxRedirects: 0,
|
|
226
|
+
});
|
|
227
|
+
let body = null;
|
|
228
|
+
try { body = await r.json(); } catch { body = null; }
|
|
229
|
+
return { status: r.status(), result: body ? body.result : undefined, error: body ? body.error : undefined };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Whether the browser's context holds a session: the site keeps its session cookie only once someone logged in.
|
|
234
|
+
*
|
|
235
|
+
* @param {object} world
|
|
236
|
+
* @returns {Promise<boolean>}
|
|
237
|
+
*/
|
|
238
|
+
async function hasSession(world) {
|
|
239
|
+
const cookies = await world.context.cookies(siteRoot(world)).catch(() => []);
|
|
240
|
+
return cookies.some((c) => /session/i.test(c.name));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The account the browser's session is of, as the site's own API answers it (its e-mail address and its whole name),
|
|
245
|
+
* or null for a visitor. A context with no session asks nothing: the site counts every refused call of its API.
|
|
246
|
+
*
|
|
247
|
+
* @param {object} world
|
|
248
|
+
* @returns {Promise<{email: string, full: string}|null>}
|
|
249
|
+
*/
|
|
250
|
+
async function currentAccount(world) {
|
|
251
|
+
if (!(await hasSession(world))) return null;
|
|
252
|
+
const r = await rpcWith(world.context.request, siteRoot(world), 'Users.Users.getUser', {}).catch(() => null);
|
|
253
|
+
const me = r && r.status === 200 ? r.result : null;
|
|
254
|
+
if (!me || !me.username) return null;
|
|
255
|
+
return { email: String(me.username), full: tidy(`${me.firstname || ''} ${me.lastname || ''}`) };
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* The e-mail address of the account the browser's session is of; an empty string for a visitor.
|
|
260
|
+
*
|
|
261
|
+
* @param {object} world
|
|
262
|
+
* @returns {Promise<string>}
|
|
263
|
+
*/
|
|
264
|
+
async function currentUser(world) {
|
|
265
|
+
const me = await currentAccount(world);
|
|
266
|
+
return me ? me.email : '';
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Forget the browser's session: the page is emptied first, so no request of the site brings a new session back.
|
|
271
|
+
*
|
|
272
|
+
* @param {object} world
|
|
273
|
+
*/
|
|
274
|
+
async function dropSession(world) {
|
|
275
|
+
if (world.page.url() !== 'about:blank') await world.page.goto('about:blank').catch(() => {});
|
|
276
|
+
await world.context.clearCookies();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Fill and submit the login form: the e-mail address and the password, then Login. It asserts nothing: a wrong
|
|
281
|
+
* password comes back to the form with the site's message.
|
|
282
|
+
*
|
|
283
|
+
* @param {object} world
|
|
284
|
+
* @param {string} email
|
|
285
|
+
* @param {string} password
|
|
286
|
+
*/
|
|
287
|
+
async function submitLogin(world, email, password) {
|
|
288
|
+
const page = world.page;
|
|
289
|
+
await open(world, '/auth/login');
|
|
290
|
+
await actOrExplain('fill in the login form for', email, async () => {
|
|
291
|
+
const field = page.locator('input[name="username"]');
|
|
292
|
+
await field.waitFor({ state: 'visible', timeout: budget(world) });
|
|
293
|
+
await field.fill(email);
|
|
294
|
+
await page.locator('input[name="password"]').fill(password);
|
|
295
|
+
await page.locator('input[name="login"]').click();
|
|
296
|
+
}, 'The login form (its fields username and password) did not show at /auth/login. A session that is already open goes to its dashboard instead; a site that signs in through another provider only has no such form.');
|
|
297
|
+
await smartSettle(page, 2000);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const sessions = new Map();
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Sign in with an account of the users registry and make sure the session is that account's. An account that is
|
|
304
|
+
* already signed in is kept; an account that signed in earlier in the run gets its session back; otherwise the account
|
|
305
|
+
* goes through the login form.
|
|
306
|
+
*
|
|
307
|
+
* @param {object} world
|
|
308
|
+
* @param {string} handle
|
|
309
|
+
*/
|
|
310
|
+
async function signInAs(world, handle) {
|
|
311
|
+
const user = resolveUser(world, handle);
|
|
312
|
+
const key = siteRoot(world) + '|' + handle;
|
|
313
|
+
const same = (email) => String(email).toLowerCase() === String(user.username).toLowerCase();
|
|
314
|
+
if (same(await currentUser(world))) {
|
|
315
|
+
if (world.page.url() === 'about:blank' || !world.page.url().startsWith(siteRoot(world))) await open(world, '/dashboard/home');
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
await dropSession(world);
|
|
319
|
+
const saved = sessions.get(key);
|
|
320
|
+
if (saved) {
|
|
321
|
+
await world.context.addCookies(saved);
|
|
322
|
+
if (same(await currentUser(world))) {
|
|
323
|
+
await open(world, '/dashboard/home');
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
sessions.delete(key);
|
|
327
|
+
await dropSession(world);
|
|
328
|
+
}
|
|
329
|
+
await submitLogin(world, user.username, user.password);
|
|
330
|
+
const result = await waitForValue(() => currentUser(world), same, budget(world));
|
|
331
|
+
if (!result.ok) {
|
|
332
|
+
const said = tidy(await world.page.locator('.inputwrapper .error, .alert, [role="alert"], form p').first().innerText({ timeout: 1500 }).catch(() => ''));
|
|
333
|
+
throw friendly({
|
|
334
|
+
message: `Logging in as "${handle}" (${user.username}) did not work${said ? `: the site says "${said}"` : `; the page is ${world.page.url()}`}.`,
|
|
335
|
+
hint: 'Check the e-mail address and the password of this account in worldParameters.users of cucumber.js, and that the account exists on the site, is active, and may log in.',
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
await closeTour(world);
|
|
339
|
+
sessions.set(key, await world.context.cookies());
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// ---------------------------------------------------------------------------
|
|
343
|
+
// The site's own API, as the managing account
|
|
344
|
+
// ---------------------------------------------------------------------------
|
|
345
|
+
|
|
346
|
+
const managers = new Map();
|
|
347
|
+
|
|
348
|
+
/** The managing accounts' sessions of a run, all of them, and their end. */
|
|
349
|
+
const managerContexts = {
|
|
350
|
+
async dispose() {
|
|
351
|
+
for (const m of managers.values()) await m.dispose().catch(() => {});
|
|
352
|
+
managers.clear();
|
|
353
|
+
},
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* A session of its own for the managing account (`worldParameters.manager`, a handle of the users registry), for the
|
|
358
|
+
* test content a scenario makes and removes: it logs in through the site's login form (with the form's own token) and
|
|
359
|
+
* calls the JSON-RPC API the screens call, with that session. It never touches the browser's session.
|
|
360
|
+
*
|
|
361
|
+
* @param {object} world
|
|
362
|
+
* @returns {Promise<import('playwright').APIRequestContext>}
|
|
363
|
+
*/
|
|
364
|
+
async function managerSession(world) {
|
|
365
|
+
const handle = (world.parameters && world.parameters.manager) || 'owner';
|
|
366
|
+
const root = siteRoot(world);
|
|
367
|
+
const key = root + '|' + handle;
|
|
368
|
+
if (managers.has(key)) return managers.get(key);
|
|
369
|
+
const user = resolveUser(world, handle);
|
|
370
|
+
const ctx = await playwright.request.newContext({ baseURL: root, ignoreHTTPSErrors: true });
|
|
371
|
+
const refuse = async (said) => {
|
|
372
|
+
await ctx.dispose();
|
|
373
|
+
throw friendly({
|
|
374
|
+
message: `The managing account "${handle}" could not log in to ${root} for the test content: ${said}.`,
|
|
375
|
+
hint: 'Check worldParameters.manager and the account in worldParameters.users of cucumber.js: a Company Manager (or above) of the projects the features use, who logs in with an e-mail address and a password.',
|
|
376
|
+
});
|
|
377
|
+
};
|
|
378
|
+
const form = await ctx.get('/auth/login', { failOnStatusCode: false });
|
|
379
|
+
const token = ((await form.text().catch(() => '')).match(/name="_token" value="([^"]+)"/) || [])[1] || '';
|
|
380
|
+
if (!token) return refuse(`the login page gave no form token (it answered ${form.status()})`);
|
|
381
|
+
const login = await ctx.post('/auth/login', { form: { _token: token, username: user.username, password: user.password, login: 'Login', redirectUrl: '' }, maxRedirects: 0, failOnStatusCode: false });
|
|
382
|
+
if (/\/auth\/login/.test(String(login.headers().location || '')) || login.status() === 200) return refuse('the site refused the e-mail address and the password');
|
|
383
|
+
const me = await rpcWith(ctx, root, 'Users.Users.getUser', {});
|
|
384
|
+
if (!me.result || String(me.result.username || '').toLowerCase() !== String(user.username).toLowerCase()) return refuse(`its session is not the account's (the site answered ${me.status})`);
|
|
385
|
+
managers.set(key, ctx);
|
|
386
|
+
return ctx;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* One call of the site's JSON-RPC API as the managing account: its result, or a friendly error with the site's words.
|
|
391
|
+
*
|
|
392
|
+
* @param {object} world
|
|
393
|
+
* @param {string} method - "Tickets.Tickets.addTicket"
|
|
394
|
+
* @param {object} params
|
|
395
|
+
* @param {string} what - what the call does, for the error ("make the task …")
|
|
396
|
+
* @returns {Promise<*>}
|
|
397
|
+
*/
|
|
398
|
+
async function api(world, method, params, what) {
|
|
399
|
+
const ctx = await managerSession(world);
|
|
400
|
+
const r = await rpcWith(ctx, siteRoot(world), method, params);
|
|
401
|
+
if (r.status !== 200 || r.error) {
|
|
402
|
+
const said = r.error && typeof r.error === 'object' ? tidy(`${r.error.message || ''} ${typeof r.error.data === 'string' ? r.error.data : ''}`) : '';
|
|
403
|
+
throw friendly({
|
|
404
|
+
message: `Could not ${what}: the site answered ${r.status}${said ? ` (${said})` : ''}.`,
|
|
405
|
+
hint: r.status === 429
|
|
406
|
+
? 'The site limits the calls of its API a minute for an account (120 by default, LEAN_RATELIMIT_API; more on a test site the Webship Workspace builds): wait a minute, or raise it for the test site.'
|
|
407
|
+
: 'The managing account (worldParameters.manager) must be a Company Manager or above, in the team of the projects the features use.',
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
return r.result;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* A project by its name ("Webship site"), among the ones the managing account is in: { id, name }. The step's
|
|
415
|
+
* project, else the one of cucumber.js.
|
|
416
|
+
*
|
|
417
|
+
* @param {object} world
|
|
418
|
+
* @param {string} [which]
|
|
419
|
+
*/
|
|
420
|
+
const projectLists = new Map();
|
|
421
|
+
|
|
422
|
+
async function findProject(world, which) {
|
|
423
|
+
const wanted = String(which || (world.parameters && world.parameters.project) || '').trim();
|
|
424
|
+
const manager = resolveUser(world, (world.parameters && world.parameters.manager) || 'owner');
|
|
425
|
+
// The managing account's projects are read once a run: the site limits its API calls a minute.
|
|
426
|
+
const key = siteRoot(world) + '|' + manager.username;
|
|
427
|
+
if (!projectLists.has(key)) {
|
|
428
|
+
const me = await api(world, 'Users.Users.getUser', {}, 'read the managing account');
|
|
429
|
+
projectLists.set(key, await api(world, 'Projects.Projects.getProjectsAssignedToUser', { userId: Number(me.id) }, `read the projects of ${manager.username}`));
|
|
430
|
+
}
|
|
431
|
+
const list = projectLists.get(key);
|
|
432
|
+
const found = (Array.isArray(list) ? list : []).find((p) => exactly(wanted).test(String(p.name)));
|
|
433
|
+
if (!found) throw friendly({ message: `There is no project "${wanted}" that the managing account is in.`, hint: 'Name the project as the site shows it (Webship site); the managing account must be in its team.' });
|
|
434
|
+
return { id: String(found.id), name: String(found.name) };
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* The tasks of a project whose title is the text (or whose number is, as "#1"), milestones left out.
|
|
439
|
+
*
|
|
440
|
+
* @param {object} world
|
|
441
|
+
* @param {{id: string}} project
|
|
442
|
+
* @param {string} title
|
|
443
|
+
* @returns {Promise<object[]>}
|
|
444
|
+
*/
|
|
445
|
+
async function findTasks(world, project, title) {
|
|
446
|
+
const list = await api(world, 'Tickets.Tickets.getAll', { searchCriteria: { currentProject: Number(project.id), excludeType: 'milestone', status: 'all' }, limit: 1000 }, `look for the task "${title}"`);
|
|
447
|
+
const number = String(title).trim().replace(/^#/, '');
|
|
448
|
+
return (Array.isArray(list) ? list : []).filter((t) => t.type !== 'milestone' && (exactly(title).test(String(t.headline)) || String(t.id) === number));
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* The one task of a title or a number in a project (the step's, else the last one a scenario used): { id, headline,
|
|
453
|
+
* project }.
|
|
454
|
+
*
|
|
455
|
+
* @param {object} world
|
|
456
|
+
* @param {string} title
|
|
457
|
+
* @param {string} [projectName]
|
|
458
|
+
*/
|
|
459
|
+
async function taskNamed(world, title, projectName) {
|
|
460
|
+
const project = await findProject(world, projectName || (world.leantime && world.leantime.project));
|
|
461
|
+
const found = await findTasks(world, project, title);
|
|
462
|
+
if (!found.length) throw friendly({ message: `There is no task "${title}" in the project ${project.name}.`, hint: 'Make it first with Given there is a task "…" in the project "…", or name a task of the sample content (its title, or its number as #1).' });
|
|
463
|
+
return { id: String(found[0].id), headline: String(found[0].headline), project };
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Remember a task a scenario made, so it goes after the scenario.
|
|
468
|
+
*
|
|
469
|
+
* @param {object} world
|
|
470
|
+
* @param {string} id
|
|
471
|
+
*/
|
|
472
|
+
function remember(world, id) {
|
|
473
|
+
world.leantime = world.leantime || {};
|
|
474
|
+
world.leantime.made = world.leantime.made || new Set();
|
|
475
|
+
world.leantime.made.add(String(id));
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Remove a task, as the managing account, with what the site keeps of it (its comments, its history, its time).
|
|
480
|
+
*
|
|
481
|
+
* @param {object} world
|
|
482
|
+
* @param {string} id
|
|
483
|
+
*/
|
|
484
|
+
async function removeTask(world, id) {
|
|
485
|
+
await api(world, 'Tickets.Tickets.delete', { id: Number(id) }, `remove the task #${id}`);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Make a task as the managing account: the project's first status, a line of text.
|
|
490
|
+
*
|
|
491
|
+
* @param {object} world
|
|
492
|
+
* @param {{id: string}} project
|
|
493
|
+
* @param {string} title
|
|
494
|
+
* @returns {Promise<string>} its number
|
|
495
|
+
*/
|
|
496
|
+
async function makeTask(world, project, title) {
|
|
497
|
+
const made = await api(world, 'Tickets.Tickets.addTicket', { values: { headline: title, description: '<p>Made by a test scenario; it goes after the scenario.</p>', projectId: Number(project.id), type: 'task', status: 3 } }, `make the task "${title}"`);
|
|
498
|
+
const id = String(Array.isArray(made) ? made[0] : made);
|
|
499
|
+
if (!/^[1-9][0-9]*$/.test(id)) throw friendly({ message: `The site made no task "${title}" (it answered ${JSON.stringify(made)}).`, hint: 'The managing account must be allowed to add tasks to the project.' });
|
|
500
|
+
remember(world, id);
|
|
501
|
+
return id;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* A sprint of a project by its name: { id, name }.
|
|
506
|
+
*
|
|
507
|
+
* @param {object} world
|
|
508
|
+
* @param {{id: string, name: string}} project
|
|
509
|
+
* @param {string} name
|
|
510
|
+
*/
|
|
511
|
+
async function findSprint(world, project, name) {
|
|
512
|
+
const list = await api(world, 'Sprints.Sprints.getAllSprints', { projectId: Number(project.id) }, `read the sprints of ${project.name}`);
|
|
513
|
+
const sprints = Array.isArray(list) ? list : [];
|
|
514
|
+
const found = sprints.find((s) => exactly(name).test(String(s.name)));
|
|
515
|
+
if (!found) throw friendly({ message: `There is no sprint "${name}" in the project ${project.name}. Its sprints: ${sprints.map((s) => `"${s.name}"`).join(', ') || 'none'}.`, hint: 'Name a sprint as the project shows it.' });
|
|
516
|
+
return { id: String(found.id), name: String(found.name) };
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// ---------------------------------------------------------------------------
|
|
520
|
+
// The open task
|
|
521
|
+
// ---------------------------------------------------------------------------
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* The task the scenario opened (or the last one it named): its number, from the world.
|
|
525
|
+
*
|
|
526
|
+
* @param {object} world
|
|
527
|
+
* @returns {string}
|
|
528
|
+
*/
|
|
529
|
+
function openTaskId(world) {
|
|
530
|
+
const id = world.leantime && world.leantime.task;
|
|
531
|
+
if (!id) throw friendly({ message: 'No task is open.', hint: 'Open one first: When I open the task "…".' });
|
|
532
|
+
return id;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* The window of an open task, found by its own address: the site opens a task in a window of the page, whose form
|
|
537
|
+
* sends to /tickets/showTicket/<number>. Never the first or the last window on the page.
|
|
538
|
+
*
|
|
539
|
+
* @param {object} world
|
|
540
|
+
* @param {string} [id]
|
|
541
|
+
* @returns {import('playwright').Locator}
|
|
542
|
+
*/
|
|
543
|
+
function taskWindow(world, id) {
|
|
544
|
+
const n = id || openTaskId(world);
|
|
545
|
+
return world.page.locator('.nyroModalCont').filter({ has: world.page.locator(`form[action$="/tickets/showTicket/${n}"]`) }).first();
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* Open a task in its window, on the project's board, by the task's own address: Leantime opens the window from the
|
|
550
|
+
* address's hash (#/tickets/showTicket/<number>), as its own links do. The board goes first, and its window of help
|
|
551
|
+
* is closed when it shows: the site keeps one window at a time, and a window of help that comes over the task's takes
|
|
552
|
+
* the task's with it when it closes. Then the task is asked for, and asked again when such a window came over it.
|
|
553
|
+
* Waits until its window is there.
|
|
554
|
+
*
|
|
555
|
+
* @param {object} world
|
|
556
|
+
* @param {{id: string, project: {id: string}}} task
|
|
557
|
+
*/
|
|
558
|
+
async function openTask(world, task) {
|
|
559
|
+
await open(world, `/projects/changeCurrentProject/${task.project.id}`);
|
|
560
|
+
await open(world, '/tickets/showKanban');
|
|
561
|
+
world.leantime = world.leantime || {};
|
|
562
|
+
world.leantime.task = task.id;
|
|
563
|
+
world.leantime.project = task.project.name;
|
|
564
|
+
const win = taskWindow(world, task.id);
|
|
565
|
+
const ask = (n) => world.page.evaluate((id) => {
|
|
566
|
+
if (window.location.hash) window.location.hash = '';
|
|
567
|
+
window.location.hash = `#/tickets/showTicket/${id}`;
|
|
568
|
+
}, String(n));
|
|
569
|
+
await actOrExplain('open the task', `#${task.id}`, async () => {
|
|
570
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
571
|
+
await ask(task.id);
|
|
572
|
+
const shown = await win.waitFor({ state: 'visible', timeout: attempt < 3 ? 10000 : budget(world) }).then(() => true).catch(() => false);
|
|
573
|
+
if (shown && !(await tourShown(world))) return;
|
|
574
|
+
if (attempt >= 3) {
|
|
575
|
+
if (shown) return;
|
|
576
|
+
throw new Error(`its window did not show, asked for three times at #/tickets/showTicket/${task.id}`);
|
|
577
|
+
}
|
|
578
|
+
await closeTour(world);
|
|
579
|
+
if (await win.isVisible().catch(() => false)) return;
|
|
580
|
+
}
|
|
581
|
+
}, 'The task\'s window did not open: the account must be in the project\'s team, and the task must be in the project.');
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Pick an option of one of the open task's lists (its status, its assignee): the site draws a list of its own over
|
|
586
|
+
* the hidden select, which the select names by its data-id.
|
|
587
|
+
*
|
|
588
|
+
* @param {object} world
|
|
589
|
+
* @param {string} field - the select's name: status, editorId
|
|
590
|
+
* @param {string} label - the option, as the list shows it
|
|
591
|
+
*/
|
|
592
|
+
async function pickInTask(world, field, label) {
|
|
593
|
+
const win = taskWindow(world);
|
|
594
|
+
const select = win.locator(`select[name="${field}"]`).first();
|
|
595
|
+
await select.waitFor({ state: 'attached', timeout: budget(world) });
|
|
596
|
+
if (await select.isDisabled()) throw friendly({ message: `The task's ${field === 'editorId' ? 'assignee' : field} cannot be changed: its list is disabled for this account.`, hint: 'A Commenter or a Read Only person may change no task.' });
|
|
597
|
+
const listId = await select.getAttribute('data-id');
|
|
598
|
+
await win.locator(`.ss-main[data-id="${listId}"]`).click();
|
|
599
|
+
const option = world.page.locator(`.ss-content[data-id="${listId}"] .ss-option`, { hasText: exactly(label) });
|
|
600
|
+
const shown = await option.first().waitFor({ state: 'visible', timeout: budget(world) }).then(() => true).catch(() => false);
|
|
601
|
+
if (!shown) {
|
|
602
|
+
const offered = await world.page.locator(`.ss-content[data-id="${listId}"] .ss-option`).allInnerTexts().catch(() => []);
|
|
603
|
+
throw friendly({ message: `The task's list offers no "${label}". It offers: ${offered.map((o) => `"${tidy(o)}"`).join(', ') || 'nothing'}.`, hint: 'Name an option as the list shows it.' });
|
|
604
|
+
}
|
|
605
|
+
await option.first().click();
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Save the open task (its Save button), and wait until the site has taken it.
|
|
610
|
+
*
|
|
611
|
+
* @param {object} world
|
|
612
|
+
*/
|
|
613
|
+
async function saveTask(world) {
|
|
614
|
+
const save = taskWindow(world).locator('input[name="saveTicket"]:visible').first();
|
|
615
|
+
if (await save.isDisabled()) throw friendly({ message: 'The task cannot be saved: its Save button is disabled for this account.', hint: 'A Commenter or a Read Only person may change no task.' });
|
|
616
|
+
await save.click();
|
|
617
|
+
await smartSettle(world.page, 2000);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* The task as the site's API gives it now, as the managing account.
|
|
622
|
+
*
|
|
623
|
+
* @param {object} world
|
|
624
|
+
* @param {string} [id]
|
|
625
|
+
*/
|
|
626
|
+
async function taskNow(world, id) {
|
|
627
|
+
return api(world, 'Tickets.Tickets.getTicket', { id: Number(id || openTaskId(world)) }, `read the task #${id || openTaskId(world)}`);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
module.exports = {
|
|
631
|
+
budget,
|
|
632
|
+
escapeRegExp,
|
|
633
|
+
exactly,
|
|
634
|
+
tidy,
|
|
635
|
+
actOrExplain,
|
|
636
|
+
assertPresence,
|
|
637
|
+
waitForValue,
|
|
638
|
+
siteRoot,
|
|
639
|
+
siteUrl,
|
|
640
|
+
tourShown,
|
|
641
|
+
closeTour,
|
|
642
|
+
open,
|
|
643
|
+
resolveUser,
|
|
644
|
+
rpcWith,
|
|
645
|
+
currentAccount,
|
|
646
|
+
currentUser,
|
|
647
|
+
dropSession,
|
|
648
|
+
submitLogin,
|
|
649
|
+
signInAs,
|
|
650
|
+
managerSession,
|
|
651
|
+
managerContexts,
|
|
652
|
+
api,
|
|
653
|
+
findProject,
|
|
654
|
+
findTasks,
|
|
655
|
+
taskNamed,
|
|
656
|
+
remember,
|
|
657
|
+
removeTask,
|
|
658
|
+
makeTask,
|
|
659
|
+
findSprint,
|
|
660
|
+
openTaskId,
|
|
661
|
+
taskWindow,
|
|
662
|
+
openTask,
|
|
663
|
+
pickInTask,
|
|
664
|
+
saveTask,
|
|
665
|
+
taskNow,
|
|
666
|
+
};
|