Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 5x 4x 5x 5x 2x 1x 1x 1x 1x 4x | /**
* Reusable Playwright fixtures for blong-browser applications.
*
* Provides a `portal` fixture that handles login automatically,
* so individual .play.ts test files can focus on the actual test scenario.
*
* Usage in playwright.config.ts:
* ```ts
* import {defineConfig} from '@playwright/test';
* export default defineConfig({...});
* ```
*
* Usage in test files:
* ```ts
* import {test, expect} from '@feasibleone/blong-browser/playwright';
* test('browse corals', async ({portal}) => {
* await portal.menuClick('marine.coral.browse');
* await expect(portal.page).toHaveScreenshot();
* });
* ```
*/
import {test as base, expect, type Page} from '@playwright/test';
import {coverageFixture} from './playwright/coverage.js';
import {writePendingReport} from './playwright/translate.js';
export {expect, Page};
/**
* Default timeout (ms) for element-level waits in the Portal helpers.
*
* Kept deliberately short (3-5s) so a genuinely missing element fails fast
* instead of hanging on the default ~30s test timeout. Test/global timeouts
* stay generous because network + dev-server compile can still be slow.
*
* Override with the `BLONG_ELEMENT_TIMEOUT` env var to iterate faster: a page
* that fails to render then costs seconds per element instead of minutes.
*/
export const BLONG_ELEMENT_TIMEOUT = Number(process.env['BLONG_ELEMENT_TIMEOUT']) || 5_000;
/**
* Timeout (ms) for the app's **first** element after a navigation.
*
* The element budget above covers elements inside an application that is already
* running. The login form is not one of them: the dev server serves the module graph
* one file per request, so the app starts executing seconds after `page.goto()` has
* resolved with `load`. Measured in CI, the app's first boot log appears 3-4.5s after
* the document loaded and the form is in the DOM ~6s in, and the page is blank until
* then — with several suites booting in parallel on a cold runner it is slower still.
*
* Bounding that wait by `BLONG_ELEMENT_TIMEOUT` is not "failing fast on a missing
* element", it is racing the server: the fill is given its whole budget while there is
* no form to fill, and a run where everything renders correctly is reported as
* `page.fill: Timeout 5000ms exceeded`.
*
* 15s is roughly 2.5x the slowest boot measured in CI (F-223), and stays inside the
* tightest test budget in the repository: `core/blong-realm` runs 20s smoke checks, and
* a boot wait longer than the test it belongs to would be cut off by the test timeout
* instead of reporting itself (D-246). Override with `BLONG_BOOT_TIMEOUT` to iterate
* faster.
*/
export const BLONG_BOOT_TIMEOUT = Number(process.env['BLONG_BOOT_TIMEOUT']) || 15_000;
/**
* How long a menu click waits for the submenu it just opened to render.
*
* A group in the menubar is a toggle whose popup is a React state change, so the
* next clickable item is in the DOM a frame later — not at once, and not late
* enough to deserve the element budget. Bounding the wait keeps a closed chain a
* cheap retry instead of a five-second hang on an element that never appears.
*/
const MENU_SETTLE_TIMEOUT = 750;
/** Options configurable via playwright.config.ts `use` block or CLI. */
export interface IBlongTestOptions {
/** Login username. */
blongUsername: string;
/** Login password. */
blongPassword: string;
/** Grant all permissions after login. Defaults to `false`; set to `true` in suites that need it. */
blongPermissions: boolean;
/**
* Browser messages this spec provokes on purpose, so the run does not print them.
*
* A spec that asserts a refusal (a role without the capability) or exercises the
* renewal path (a revoked session) knows which lines the browser will report;
* declaring them here keeps the evidence — the message is still collected in
* `Portal.browserErrors` and still attached to a failure — while leaving it out of
* the run's log, which is otherwise a wall of lines every reader learns to skip.
*
* **Substrings, not regular expressions.** A fixture option crosses Playwright's
* worker boundary and arrives as plain JSON, where a `RegExp` is an empty object —
* `patterns.some is not a function`, thrown the first time the page said anything.
* The line a pattern is matched against is the whole note, URL and line number
* included, so `'rpc/blong/flow/find'` finds a message whose own text names no URL.
*
* Hooked to the spec rather than to the message: "this spec expects this" is a
* statement the author makes and a reviewer can check, where a global ignore list
* would hide the same line in the spec that does *not* expect it.
*/
blongExpectedBrowserErrors?: readonly string[];
/**
* UI language the run activates after login.
*
* The switch goes through `languageActivate`, so the database dictionary for
* that language is read before the first assertion — a spec then sees a
* translated page, not one that was still English when it captured.
*/
blongLanguage?: string;
/**
* Opt in to translation harvesting: the seed files a run may add its missing
* keys to. Unset means collection is off and nothing is written.
*
* A test never writes a seed; it writes one pending report, and the run's
* `globalTeardown` merges them under a lock (`D-632`).
*/
blongTranslationSeed?: {
language?: string;
/** Label seed, e.g. `meta/db/bg-coreLabelSeed.yaml`. */
label?: string;
/** Resource seed, e.g. `meta/db/bg-coreTranslationSeed.yaml`. */
translation?: string;
};
}
/** How a spec declares the browser messages it provokes on purpose. */
export interface IPortalOptions {
/** Messages not to echo; recorded and reported exactly as the others are. */
expected?: readonly string[];
}
/**
* Portal helper — wraps common portal interactions
* so tests read like user stories rather than CSS selector chains.
*/
export class Portal {
readonly page: Page;
/**
* Everything the browser said during the session — console errors and warnings,
* and uncaught exceptions.
*
* Collected, not just printed: a broken page is otherwise reported as whichever
* element never appeared, and the reason stays in a trace nobody opens.
*/
readonly browserErrors: string[] = [];
/** The first uncaught exception, if the page threw — the app is dead after it. */
fatalError: Error | undefined;
/** Messages not to echo, from the spec that knows it caused them. */
readonly #expected: readonly string[];
/** Rejects as soon as the page throws, so no wait outlives the application. */
readonly #fatalWait: Promise<never>;
#rejectFatal!: (error: Error) => void;
constructor(page: Page, options: IPortalOptions = {}) {
this.page = page;
this.#expected = options.expected ?? [];
this.#fatalWait = new Promise<never>((_resolve, reject) => {
this.#rejectFatal = reject;
});
// Recorded whether or not anyone is waiting on it; this keeps the rejection
// from surfacing as an unhandled one when no wait is in flight.
this.#fatalWait.catch(() => undefined);
page.on('console', message => {
Iif (message.type() !== 'error' && message.type() !== 'warning') return;
// Where it came from: a browser message like "Failed to load resource:
// 404" names no URL in its text, and the URL is the whole answer.
const {url, lineNumber} = message.location();
const where = url === undefined ? '' : ` (${url}:${lineNumber})`;
this.#note(`${message.type()}: ${message.text()}${where}`);
});
page.on('pageerror', error => {
this.#note(`pageerror: ${error.message}`);
this.fatalError ??= error;
this.#rejectFatal(error);
});
}
/**
* Record a browser message, and echo it unless the spec declared it expected.
*
* The echo is what makes a broken page visible while the run goes on, so it stays
* the default: silence is what a spec asks for, per message.
*/
#note(line: string): void {
this.browserErrors.push(line);
if (this.#expected.some(pattern => line.includes(pattern))) return;
console.error(`[browser] ${line}`);
}
/** A failure carrying everything the browser said — never a bare timeout. */
failure(reason: unknown): Error {
const base = reason instanceof Error ? reason : new Error(String(reason));
Iif (this.browserErrors.length === 0) return base;
return new Error(
`${base.message}\n[browser errors]\n${this.browserErrors
.map(line => ` ${line}`)
.join('\n')}`,
{cause: base},
);
}
/**
* Run an action, giving up the moment the page throws.
*
* An uncaught exception means the bundle is dead: every element that follows is
* missing for a reason, and waiting an element timeout for each of them buries
* the cause behind a slow failure instead of naming it.
*/
async #guard<T>(action: Promise<T>): Promise<T> {
if (this.fatalError !== undefined) throw this.failure(this.fatalError);
// The race abandons the action, so its own rejection must not go unhandled.
action.catch(() => undefined);
try {
return await Promise.race([action, this.#fatalWait]);
} catch (reason) {
throw this.failure(this.fatalError ?? reason);
}
}
// ── Login ──────────────────────────────────────────────────────────────
/** Log in via the Login form. Waits for the portal menubar to appear. */
async login(username: string, password: string): Promise<void> {
// The login form is the app's first paint, so it is the one element waited for
// with the boot budget rather than the element budget — see
// {@link BLONG_BOOT_TIMEOUT}. Waiting for it here and filling it below keeps the
// two questions apart: "is the app up?" is the server's pace, "can this field be
// filled?" is the element budget, and one budget cannot answer both.
await this.#guard(
this.page
.locator('input[name="username"]')
.waitFor({state: 'visible', timeout: BLONG_BOOT_TIMEOUT}),
);
const timeout = BLONG_ELEMENT_TIMEOUT;
await this.#guard(this.page.fill('input[name="username"]', username, {timeout}));
await this.#guard(this.page.fill('input[name="password"]', password, {timeout}));
await this.#guard(this.page.getByTestId('login-submit').click({timeout}));
// Wait for portal to render (menubar appears)
await this.#guard(
this.page
.locator('.blong-portal-menubar')
.waitFor({state: 'visible', timeout: BLONG_ELEMENT_TIMEOUT}),
);
// The menubar appears from the suite's own configuration; the composed one —
// every page-owning realm's menu, and the brand it may set — is applied a
// moment later (App.tsx calls `portalConfigMerge` once authenticated). A
// capture taken in between shows a portal that is only half-composed, so
// every spec waits for the shell to say the composed answer landed.
await this.#guard(
this.page
.locator('html[data-portal-config="merged"]')
.waitFor({state: 'attached', timeout: BLONG_ELEMENT_TIMEOUT}),
);
}
// ── Menu ─────────────────────────────────────────────────────────────────
/**
* Click a portal menu item by its semantic triple.
* For grouped menus, first opens the parent group.
*
* The group a page sits in is the model's own decision (`classifier`,
* `menu.group`, `menu.root` — `@@D-777`), so it is never guessed from the
* method's subject: a page whose subject is `sport` may sit under `Registry`
* or inside the shared `Classifiers` root, and a group's `data-testid` is the
* id the menu builder gave it — `menuGroupId('$Subject')` is `subject`, not
* `$subject`. Clicking the wrong id opens nothing and leaves the leaf hidden.
*
* The chain comes from the rendered menu instead of from a parallel copy of
* it: the leaf is in the DOM even while its submenu is collapsed, so the
* anchors above it are the ids a click needs, whatever the menu was composed
* from and whether or not the deployment exposes the test store.
*
* @param method - semantic triple, e.g. 'marine.coral.browse'
*/
async menuClick(method: string): Promise<void> {
const menuId = method.replace(/\./g, '-');
const item = this.page.getByTestId(`portal-menu-${menuId}`);
// The menubar paints before the composed menu lands in it: the shell renders an
// empty bar and fills it once `portalConfigMerge` answers, so a click taken at
// the wrong moment finds no item at all — not even the group it would open. Any
// menu item arriving is what says the menu has been rendered.
await this.page
.locator('[data-testid^="portal-menu-"]')
.first()
.waitFor({state: 'attached', timeout: BLONG_ELEMENT_TIMEOUT})
.catch(() => undefined);
// Opening a group is a toggle and the submenu it opens renders a moment
// later, so a group click that lands while the previous submenu is still
// settling can leave the chain closed — the leaf is then in the DOM but
// hidden, and the click below waits out its timeout on an element no
// amount of waiting can reveal. Walk the chain again until the leaf is
// actually visible, which is also what reopens a group that a click closed.
for (let attempt = 0; attempt < 3; attempt++) {
if (await item.isVisible().catch(() => false)) break;
const groups = await this.#menuGroups(menuId, method);
for (const [index, groupId] of groups.entries()) {
const group = this.page.getByTestId(`portal-menu-${groupId}`).first();
if (!(await group.count())) continue;
if (!(await group.isVisible().catch(() => false))) continue;
await this.#guard(group.click());
// The click only requests the submenu: the next target — the group
// below this one, or the leaf itself — appears a frame or two later,
// and clicking before it does is what leaves the chain half open.
const next =
index + 1 < groups.length
? this.page.getByTestId(`portal-menu-${groups[index + 1]}`).first()
: item;
await next
.waitFor({state: 'visible', timeout: MENU_SETTLE_TIMEOUT})
.catch(() => undefined);
}
}
// Click the specific menu item
await this.#guard(item.click({timeout: BLONG_ELEMENT_TIMEOUT}));
}
/**
* The group ids holding the leaf, outermost first.
*
* Three answers, in order of how much they can be trusted: the anchors above
* the leaf in the rendered menu (the ids a click needs, by construction); the
* composed config's own `id`s, for a menu that has not rendered the leaf yet;
* and the method's subject, which is what a flat single-group menu needs and
* is only reached when neither of the others knows the page.
*/
async #menuGroups(menuId: string, method: string): Promise<string[]> {
const rendered = await this.#menuAncestors(menuId);
if (rendered.length) return rendered;
const composed = await this.#menuPath(method);
if (composed.length) return composed;
return [method.split('.')[0]!];
}
/**
* The ids of the group anchors enclosing the leaf, outermost first.
*
* Read from the DOM the menubar actually rendered: the leaf sits inside the
* `<li>` of each group that must be opened, whose own anchor carries the id the
* template gave it. No ancestors means the leaf is not in the DOM at all.
*/
async #menuAncestors(menuId: string): Promise<string[]> {
return this.page.evaluate(id => {
const leaf = document.querySelector(`[data-testid="portal-menu-${id}"]`);
if (!leaf) return [];
const chain: string[] = [];
let node: Element | null = leaf.parentElement;
while (node) {
// Only the anchor a node *owns* counts. A plain `querySelector` in
// the subtree returns the first anchor anywhere below it, so every
// ancestor reported the same top-level group — and the last one even
// the leaf. PrimeReact puts the anchor under `.p-menuitem-content`,
// beside the submenu `<ul>` rather than inside it.
const anchor = node.querySelector(
':scope > a[data-testid^="portal-menu-"], :scope > .p-menuitem-content > a[data-testid^="portal-menu-"]',
);
const testid = anchor?.getAttribute('data-testid');
if (testid && anchor !== leaf) chain.unshift(testid.slice('portal-menu-'.length));
node = node.parentElement;
}
return chain;
}, menuId);
}
/**
* The ids of the groups holding `method`, outermost first.
*
* Read from the composed portal config (`portal.portalConfig.menu`) through the
* integration test hook. An empty answer means the store or the menu is not
* reachable from this deployment, and the caller falls back.
*/
async #menuPath(method: string): Promise<string[]> {
return this.page.evaluate(target => {
const store = (globalThis as {__blongStore?: {getState?: () => unknown}}).__blongStore;
const state = store?.getState?.() as
| {portal?: {portalConfig?: {menu?: unknown[]}}}
| undefined;
const menu = state?.portal?.portalConfig?.menu;
if (!Array.isArray(menu)) return [];
type Node = {id?: string; method?: string; items?: Node[]} | string;
const walk = (items: Node[], ancestors: string[]): string[] | undefined => {
for (const item of items) {
if (typeof item === 'string') {
if (item === target) return ancestors;
continue;
}
if (Array.isArray(item.items)) {
const found = walk(item.items, [...ancestors, item.id ?? '']);
if (found) return found;
}
if (item.method === target) return ancestors;
}
return undefined;
};
return (walk(menu as Node[], []) ?? []).filter(Boolean);
}, method);
}
// ── Tabs ─────────────────────────────────────────────────────────────────
/** Close a tab. Finds the close button inside the tab header. */
async tabClose(tabId: string): Promise<void> {
await this.page.getByTestId(`portal-tab-close${tabId}`).click();
}
/**
* Close every open tab, back to the empty portal.
*
* The portal keeps closed-page state out of the shell but not out of the tab strip: a
* page opened while two tabs are standing is a different picture from one opened on an
* empty portal, and a test that visits several pages in a row must therefore pass
* through empty between them or every capture after the first moves (`@@D-748`).
*/
async tabsCloseAll(): Promise<void> {
const closeButtons = this.page.locator('.blong-tab-close');
for (let remaining = await closeButtons.count(); remaining > 0; remaining -= 1) {
await this.#guard(closeButtons.last().click({timeout: BLONG_ELEMENT_TIMEOUT}));
await this.page
.locator('.blong-tab-close')
.nth(remaining - 1)
.waitFor({state: 'detached', timeout: BLONG_ELEMENT_TIMEOUT})
.catch(() => undefined);
}
}
// ── Editor toolbar ───────────────────────────────────────────────────────
/** Click the Save button and wait for the success icon. */
async save(): Promise<void> {
await this.page.getByTestId('editor-save').click();
// Wait for the save icon to change to a check mark
await this.page
.locator('[data-testid="editor-save"] .pi-check')
.waitFor({state: 'visible', timeout: BLONG_ELEMENT_TIMEOUT});
}
/** Click the Edit button to enter edit mode. */
async edit(): Promise<void> {
await this.page.getByTestId('editor-edit').click();
}
/** Click the Reset/Cancel button. */
async cancel(): Promise<void> {
await this.page.getByTestId('editor-cancel').click();
}
/** Click the Refresh button. */
async refresh(): Promise<void> {
await this.page.getByTestId('editor-refresh').click();
}
// ── Server ───────────────────────────────────────────────────────────────
/**
* One call through the page's handler proxy.
*
* A spec that has to leave the database as it found it cannot always use the
* page: a model whose delete button deactivates (`deletion: {mode:
* 'deactivate'}`) keeps its row, and a leftover under the same name is what the
* next run opens instead of its own record. The remove handler behind that
* button is a real delete, and this is how a spec reaches it. `@@D-829`
*/
async call<T = unknown>(method: string, params: Record<string, unknown> = {}): Promise<T> {
// The proxy is installed when the provider mounts, which the fixture's login
// has already waited for; asking again keeps a spec that starts with a call
// from racing the mount.
await this.page
.waitForFunction(
() => typeof (window as {__blongHandler?: unknown}).__blongHandler === 'object',
undefined,
{timeout: 15_000},
)
.catch(() => undefined);
return this.page.evaluate(
async ([name, args]) => {
const handler = (window as {__blongHandler?: Record<string, unknown>})
.__blongHandler;
if (!handler) {
throw new Error(
'the page exposed no `__blongHandler`: the integration intent (testHook) is off',
);
}
return (handler[name] as (p: unknown, m: object) => Promise<unknown>)(args, {});
},
[method, params] as [string, Record<string, unknown>],
) as Promise<T>;
}
// ── Form fields ──────────────────────────────────────────────────────────
/**
* Fill a text input by its field name.
* Fields use `name` attributes like `coral.coralName`.
*/
async fill(fieldName: string, value: string): Promise<void> {
await this.page.fill(`input[name="${fieldName}"]`, value);
}
/** Fill a textarea by its field name. */
async fillTextarea(fieldName: string, value: string): Promise<void> {
await this.page.fill(`textarea[name="${fieldName}"]`, value);
}
// ── Table ────────────────────────────────────────────────────────────────
/** Click a cell in a table. Uses data-testid pattern: `{fieldName}-{rowIndex}`. */
async tableRowClick(fieldName: string, rowIndex: number): Promise<void> {
await this.page.getByTestId(`${fieldName}-${rowIndex}`).click();
}
/**
* Click a row that contains specific text in a table column.
* Waits for the row to appear (useful after search/filter).
*/
async tableRowClickByText(text: string): Promise<void> {
await this.page.locator(`td:has-text("${text}")`).first().click();
}
/** Click the Add button on a table widget. */
async tableAdd(fieldName: string): Promise<void> {
await this.page.getByTestId(`${fieldName}-addButton`).click();
}
/** Click the Delete button on a table widget. */
async tableDelete(fieldName: string): Promise<void> {
await this.page.getByTestId(`${fieldName}-deleteButton`).click();
}
// ── Waiting ──────────────────────────────────────────────────────────────
/** Wait for the form to finish loading (loading skeleton disappears). */
async waitForFormLoad(): Promise<void> {
await this.#guard(
this.page
.locator('.blong-editor')
.last()
.waitFor({state: 'visible', timeout: BLONG_ELEMENT_TIMEOUT}),
);
// Wait for loading indicators to disappear
await this.page
.locator('.p-skeleton')
.first()
.waitFor({state: 'hidden', timeout: BLONG_ELEMENT_TIMEOUT})
.catch(() => {});
}
/**
* Wait for form data to be populated from the API.
* Use after `waitForFormLoad()` when opening an existing record,
* to ensure the API response has been received before filling fields.
*/
async waitForFormData(): Promise<void> {
await this.#guard(
this.page.waitForFunction(
() => {
const editors = document.querySelectorAll('.blong-editor');
const last = editors[editors.length - 1];
if (!last) return false;
const inputs = last.querySelectorAll(
'input:not([type="hidden"]):not([type="checkbox"])',
);
return Array.from(inputs).some(i => (i as HTMLInputElement).value !== '');
},
{timeout: BLONG_ELEMENT_TIMEOUT},
),
);
}
/** Wait for the table data to load (rows appear after loading completes). */
async waitForTableData(): Promise<void> {
// PrimeReact DataTable renders an empty state row synchronously
// before the API response arrives. Waiting for ANY <tr> would resolve
// on that empty row before data has loaded. Instead, wait for the
// loading overlay to disappear first.
await this.page
.locator('.p-datatable-loading-overlay')
.waitFor({state: 'hidden', timeout: BLONG_ELEMENT_TIMEOUT})
.catch(() => {});
await this.#guard(
this.page
// Either a data row or the table's explicit empty state. A page whose
// answer is legitimately empty — a search before it is typed, a filter
// that matches nothing — is loaded, and must not be mistaken for a page
// that never rendered (which is what the fixtures now report anyway).
//
// `:visible` because the portal keeps every opened page mounted
// (`renderActiveOnly={false}`): the tables of inactive tabs are still
// in the DOM, and without this a spec that opens a second page waits
// on the first page's hidden row.
.locator('.p-datatable-tbody tr:visible, .p-datatable-emptymessage:visible')
.first()
.waitFor({state: 'visible', timeout: BLONG_ELEMENT_TIMEOUT}),
);
}
}
/** What a session needs to become an authenticated portal. */
export interface ISignInOptions {
/** Login name. */
username: string;
/** Login password. */
password: string;
/** Grant every permission after login, so permission-gated controls are visible. */
permissions?: boolean;
/** UI language to activate, which awaits the database dictionary. */
language?: string;
}
/**
* Open the app and sign in, returning the helper a spec drives.
*
* The sequence is the same whether the page is this test's own or one a whole worker
* shares, so it lives here rather than inside the fixture: a shared session
* (`./playwright/shared.js`) boots once per worker and must sign in exactly the way a
* per-test boot does, or a spec would measure a different app depending on which
* fixture it imported.
*/
export async function openPortal(
page: Page,
options: ISignInOptions,
existing?: Portal,
): Promise<Portal> {
const {username, password, permissions = false, language} = options;
// A caller that already built the helper keeps it — the `portal` fixture constructs
// one before navigating so that a page which throws *while booting* is reported, and
// two helpers watching one page would report every console line twice.
const portal = existing ?? new Portal(page, {expected: []});
// Navigate to the app root. Relative (`./`), not `/`: the dev server serves the app
// under the framework base path (`/s/`, see `defineBlongViteConfig`), and an
// origin-absolute `/` would resolve past it to a 404 page.
await page.goto('./');
await portal.login(username, password);
if (permissions) {
// Grant all permissions so permission-gated toolbar buttons are visible
await page.evaluate(() => {
const store = (window as unknown as Record<string, unknown>).__blongStore as
| {getState: () => {setPermissions: (p: boolean) => void}}
| undefined;
store?.getState().setPermissions(true);
});
}
if (language) {
// The same store the permission escape uses, and only exposed by a test intent.
// Activation awaits the database dictionary, so a spec asserts on a page that is
// already translated.
await page.evaluate(async value => {
const win = window as unknown as {
__blongI18n?: {activate?: (value: string) => Promise<void>};
__blongStore?: {
getState: () => {languageActivate?: (value: string) => Promise<void>};
};
};
// `activate` is the app's own hook (it awaits the database dictionary); the
// store form is the fallback for a portal that exposes `testHook`.
if (win.__blongI18n?.activate) await win.__blongI18n.activate(value);
else await win.__blongStore?.getState().languageActivate?.(value);
}, language);
await page.waitForSelector(`html[data-language="${language}"]`, {
state: 'attached',
timeout: BLONG_ELEMENT_TIMEOUT,
});
}
return portal;
}
/**
* Playwright test fixture extended with blong portal and coverage support.
*
* The `portal` fixture automatically:
* - Navigates to the app
* - Logs in with configured credentials
* - Provides a Portal helper for menu clicks, form fills, saves, etc.
*
* Browser-side JavaScript coverage is collected automatically when
* `NODE_V8_COVERAGE` environment variable is set (by `blong-dev playwright --coverage`).
* The coverage fixture runs silently and produces V8-format JSON files
* compatible with `c8 report` aggregation.
*/
export const test = coverageFixture(
base.extend<IBlongTestOptions & {portal: Portal}>({
blongUsername: ['admin', {option: true}],
blongPassword: ['admin', {option: true}],
blongPermissions: [false, {option: true}],
blongExpectedBrowserErrors: [[], {option: true}],
blongLanguage: [undefined as string | undefined, {option: true}],
blongTranslationSeed: [
undefined as IBlongTestOptions['blongTranslationSeed'],
{option: true},
],
portal: async (
{
page,
blongUsername,
blongPassword,
blongPermissions,
blongExpectedBrowserErrors,
blongLanguage,
blongTranslationSeed,
},
use,
) => {
// Constructed before navigating so the boot itself is watched: a page that
// throws while it loads is exactly the case this fixture exists to report.
// `Portal` echoes browser console errors/warnings to the runner's stderr
// (prefixed `[browser]`) and gives up a wait the moment the page throws,
// instead of letting every later element wait out its full timeout.
const portal = new Portal(page, {expected: blongExpectedBrowserErrors});
// Collection starts before the app boots, so the first authenticated
// render is recorded rather than only the screens a spec reopens.
if (blongTranslationSeed) {
await page.addInitScript(() => {
(window as unknown as {__blongI18nCollect?: boolean}).__blongI18nCollect = true;
});
}
// The boot and the sign-in, exactly as a shared session does them: the
// fixture is the portal object plus the per-test bookkeeping around it.
await openPortal(
page,
{
username: blongUsername,
password: blongPassword,
permissions: blongPermissions,
language: blongLanguage,
},
portal,
);
// eslint-disable-next-line @eslint-react/rules-of-hooks -- Playwright fixture `use()`, not a React Hook
await use(portal);
if (blongTranslationSeed) {
const report = await page.evaluate(() => {
const api = (
window as unknown as {
__blongI18n?: {report?: () => unknown};
}
).__blongI18n;
return api?.report?.() as
| {language?: string; missing?: string[]; missingResources?: string[]}
| undefined;
});
if (report) writePendingReport(blongTranslationSeed, report);
}
// An uncaught exception is a broken application, whether or not the
// assertion that followed happened to pass. Reporting it here means the
// suite never goes green over a page that threw.
if (portal.fatalError !== undefined) throw portal.failure(portal.fatalError);
},
}),
);
|