35 lines
1.7 KiB
JavaScript
35 lines
1.7 KiB
JavaScript
import assert from 'node:assert/strict';
|
|||
|
|
|
||
|
|
export function ui({ version, step }) {
|
||
|
|
const action = (scope, name) => scope.locator('[data-action="' + name + '"]:visible').first();
|
||
|
|
const field = (scope, name) => scope.locator('[name="' + name + '"]');
|
||
|
|
const dialog = page => page.locator('dialog[open]').last();
|
||
|
|
async function fill(scope, values) {
|
||
|
|
for (const [key, value] of Object.entries(values)) await field(scope, key).fill(value);
|
||
|
|
}
|
||
|
|
async function navigate(page, section) {
|
||
|
|
while (await page.locator('dialog[open]').count()) await page.keyboard.press('Escape');
|
||
|
|
const link = version === 'old' ? '[data-tab="' + section + '"]' : 'a[href="/next/' + section + '"]';
|
||
|
|
await page.locator('nav:visible').locator(link).first().click();
|
||
|
|
await page.waitForLoadState('networkidle');
|
||
|
|
}
|
||
|
|
async function write(name, page, endpoint, click, { status = 200, after } = {}) {
|
||
|
|
let response;
|
||
|
|
await step(name, async () => {
|
||
|
|
const waiting = page.waitForResponse(r => {
|
||
|
|
const url = new URL(r.url()).pathname;
|
||
|
|
return (
|
||
|
|
r.request().method() === 'POST' && (typeof endpoint === 'string' ? url === endpoint : endpoint.test(url))
|
||
|
|
);
|
||
|
|
});
|
||
|
|
[response] = await Promise.all([waiting, click()]);
|
||
|
|
assert.equal(response.status(), status, name + ' ' + (await response.text()));
|
||
|
|
assert.ok(response.request().headers()['idempotency-key'], name + ' 缺少原操作号');
|
||
|
|
if (after) await after();
|
||
|
|
await page.waitForLoadState('networkidle');
|
||
|
|
});
|
||
|
|
return response;
|
||
|
|
}
|
||
|
|
const submit = form => form.locator('button[type="submit"]').click();
|
||
|
|
return { action, field, dialog, fill, navigate, write, submit };
|
||
|
|
}
|