import { Page, expect } from '@playwright/test'; import { P2PUi } from '../../ui'; import { NAV, TASK, FIELD, BUTTON, TID, TABLE, MENU, DATA } from './labels'; /** * Page actions for the MOCK O2C pack. Same locator policy as P2P (getByRole/getByLabel first, getByTestId for read-only * values only, no CSS/XPath, no sleeps). Every label comes from ./labels.ts. */ export class O2CUi extends P2PUi { constructor(page: Page) { super(page); } async nav(key: keyof typeof NAV) { await this.go(NAV[key]); } heading(name: string) { return this.page.getByRole('heading', { level: 1, name, exact: true }); } tab(name: string) { return this.page.getByRole('tab', { name, exact: true }); } dialog(name: string) { return this.page.getByRole('dialog', { name, exact: true }); } /** Create Order -> Add -> Submit (O2C-OM-001 steps 3-10). Returns the order number. */ async createOrder(o: { customer?: string; quantity?: number; po?: string; submit?: boolean; extra?: Record } = {}) { await this.nav('order_management'); await this.task(TASK.createOrder); await expect(this.heading(TASK.createOrder)).toBeVisible(); await this.field(FIELD.customer).selectOption({ label: o.customer ?? DATA.customer }); await this.field(FIELD.businessUnit).selectOption(DATA.bu); await this.page.getByRole('textbox', { name: FIELD.purchaseOrder, exact: true }).fill(o.po ?? `ZZ-CPO-${Date.now().toString(36).toUpperCase()}`); for (const [label, value] of Object.entries(o.extra ?? {})) await this.field(label).selectOption(value); // flexfield / page-extension selects await this.page.getByRole('searchbox', { name: FIELD.searchItem, exact: true }).fill(DATA.item); await this.page.getByRole('spinbutton', { name: FIELD.quantity, exact: true }).fill(String(o.quantity ?? 5)); await this.btn(BUTTON.add).click(); await this.expectOk(`Line added: item ${DATA.item}, quantity ${o.quantity ?? 5}.`); const num = (await this.value(TID.orderNumber).textContent())!.trim(); if (o.submit !== false) { await this.btn(BUTTON.submit).click(); await this.expectOk(`Order ${num} submitted`); } return num; } async openOrder(num: string) { await this.nav('order_management'); await this.field(FIELD.order).fill(num); await this.btn(BUTTON.search).click(); await this.link(num).click(); await expect(this.value(TID.orderNumber)).toHaveText(num); } async searchTransaction(by: { number?: string; salesOrder?: string }) { await this.go(NAV.home); await this.task(TASK.manageTransactions); if (by.number) await this.field(FIELD.transactionNumber).fill(by.number); if (by.salesOrder) await this.field(FIELD.salesOrder).fill(by.salesOrder); await this.btn(BUTTON.search).click(); } async openTransaction(by: { number?: string; salesOrder?: string }) { await this.searchTransaction(by); const rows = this.table(TABLE.transactions).getByRole('row'); await expect(rows).toHaveCount(2); // header + exactly one transaction await this.table(TABLE.transactions).getByRole('link').click(); // the only data row's link (no positional index) await expect(this.value(TID.trxNumber)).toBeVisible(); return (await this.value(TID.trxNumber).textContent())!.trim(); } async actionsMenu(item: string) { await this.btn(BUTTON.actions).click(); await this.page.getByRole('menuitem', { name: item, exact: true }).click(); } journal(dialogName = MENU.viewAccounting) { return this.dialog(dialogName).getByRole('table', { name: TABLE.journalLines, exact: true }).getByRole('row'); } /** Assert a journal line by side, account class and amount. */ async expectJournal(side: 'Dr' | 'Cr', accountClass: string, amount: string) { await expect(this.cells(this.journal(), [side, accountClass, amount])).toHaveCount(1); } /** Rows that contain an exact cell for each value (row text concatenates cells, so match cells, not text). */ cells(rows: import('@playwright/test').Locator, values: string[]) { return values.reduce((r, v) => r.filter({ has: this.page.getByRole('cell', { name: v, exact: true }) }), rows); } }