File size: 11,984 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Page, Locator, expect } from '@playwright/test';
import { Persona, MOCK } from './api';

import { O2C_PERSONA_LABELS } from './modules/o2c/labels';
import { R2R_PERSONA_LABELS } from './modules/r2r/labels';
import { A2R_PERSONA_LABELS } from './modules/a2r/labels';
import { EXP_PERSONA_LABELS } from './modules/exp/labels';
import { SCM_PERSONA_LABELS } from './modules/scm/labels';
import { P2M_PERSONA_LABELS } from './modules/p2m/labels';
import { PPM_PERSONA_LABELS } from './modules/ppm/labels';
import { SCP_PERSONA_LABELS } from './modules/scp/labels';
import { HCM_PERSONA_LABELS } from './modules/hcm/labels';
import { CX_PERSONA_LABELS } from './modules/cx/labels';

const LABEL: Record<Persona, string> = {
  ...O2C_PERSONA_LABELS,
  ...R2R_PERSONA_LABELS,
  ...A2R_PERSONA_LABELS,
  ...EXP_PERSONA_LABELS,
  ...SCM_PERSONA_LABELS,
  ...P2M_PERSONA_LABELS,
  ...PPM_PERSONA_LABELS,
  ...SCP_PERSONA_LABELS,
  ...HCM_PERSONA_LABELS,
  ...CX_PERSONA_LABELS,
  requester: 'Requester', approver_l1: 'Approver L1', approver_l2: 'Approver L2', buyer: 'Buyer', po_approver: 'PO Approver', po_approver_l2: 'PO Approver L2',
  receiver: 'Receiver', ap_specialist: 'AP Specialist', ap_supervisor: 'AP Supervisor', invoice_approver: 'Invoice Approver', payments_user: 'Payments User',
  gl_accountant: 'GL Accountant', supplier_admin: 'Supplier Administrator', integration_user: 'Integration User', read_only: 'Read Only',
  contract_author: 'Contract Author', contract_author_bu2: 'Contract Author (BU2)', legal_reviewer: 'Legal Reviewer', category_manager: 'Category Manager', supplier_user_a: 'Supplier User A',
};
/** Work-area entry points (mock equivalent of tests/ui-glossary.yaml nav keys). */
export const NAV = {
  purchase_requisitions: '/requisitions/shop', manage_requisitions: '/requisitions', process_requisitions: '/process-requisitions', purchase_orders: '/purchase-orders',
  receipts: '/receipts/expected', suppliers: '/suppliers', invoices: '/invoices', payments: '/payments', scheduled_processes: '/scheduled-processes',
  approvals: '/approvals', journals: '/journals', home: '/',
  procurement_contracts: '/p2p/contracts', terms_library: '/p2p/terms-library', supplier_portal: '/p2p/supplier-portal', notifications: '/worklist',
} as const;

/**

 * Page actions for the MOCK P2P UI. Locator policy (qa/test-strategy.md S1): getByRole / getByLabel first,

 * getByTestId only for read-only value cells. No CSS/XPath, no fixed sleeps; web-first assertions only.

 */
export class P2PUi {
  constructor(public page: Page) {}
  btn(name: string | RegExp) { return this.page.getByRole('button', { name, exact: typeof name === 'string' }); }
  link(name: string | RegExp) { return this.page.getByRole('link', { name, exact: typeof name === 'string' }); }
  field(label: string) { return this.page.getByLabel(label, { exact: true }); }
  value(testId: string) { return this.page.getByTestId(testId); }
  table(name: string) { return this.page.getByRole('table', { name, exact: true }); }
  row(tableName: string, text: string | RegExp) { return this.table(tableName).getByRole('row').filter({ hasText: text }); }
  /** Cell in the column whose header is `col` (header looked up by name; fails clearly if the column is missing, never nth(-1)). */
  async cellByHeader(tableName: string, rowText: string | RegExp, col: string) {
    const heads = (await this.table(tableName).getByRole('columnheader').allTextContents()).map((h) => h.trim());
    const i = heads.indexOf(col);
    expect(i, `column "${col}" missing in table "${tableName}" (headers: ${heads.join(' | ')})`).toBeGreaterThanOrEqual(0);
    return this.row(tableName, rowText).getByRole('cell').nth(i);
  }
  get ok() { return this.page.getByRole('status'); }
  get alert() { return this.page.getByRole('alert'); }

  async login(p: Persona) {
    await this.page.goto('/login');
    await this.field('Persona').selectOption(p);
    await this.btn('Sign In').click();
    await expect(this.value('current-role')).toHaveText(LABEL[p]);
    await expect(this.alert).toHaveCount(0);
  }
  async go(path: string) { await this.page.goto(path); }
  async task(name: string) { await this.page.getByRole('navigation', { name: 'Tasks' }).getByRole('link', { name, exact: true }).click(); }
  async expectOk(text?: string | RegExp) { await expect(this.ok).toBeVisible(); if (text) await expect(this.ok).toContainText(text); }
  async expectError(text?: string | RegExp) { await expect(this.alert).toBeVisible(); if (text) await expect(this.alert).toContainText(text); }
  /** Wait for the post-redirect banner; true = success, false = error. */
  async outcome() { await expect(this.ok.or(this.alert)).toBeVisible(); return (await this.ok.count()) > 0; }

  // ---------------- requisitions ----------------
  async shopAddToCart(item: string, quantity: number) {
    await this.go(NAV.purchase_requisitions);
    await this.page.getByRole('searchbox', { name: 'Search' }).fill(item);
    await this.btn('Search').click();
    const card = this.page.getByRole('group', { name: new RegExp('MOCK') }).filter({ hasText: item });
    await expect(card.getByTestId('item-price')).toBeVisible();
    await card.getByLabel('Quantity').fill(String(quantity));
    await card.getByRole('button', { name: 'Add to Cart' }).click();
    await this.expectOk('Added to cart');
  }
  /** Shopping Cart -> Review -> (optional edits) -> Submit. Returns requisition number. */
  async reviewAndSubmit(edit?: () => Promise<void>) {
    await this.link('Shopping Cart').click();
    await this.btn('Review').click();
    await this.expectOk('ready for review');
    if (edit) await edit();
    await this.btn('Submit').click();
    await this.expectOk('submitted');
    return (await this.value('req-number').textContent())!.trim();
  }
  async createCatalogRequisition(item: string, quantity: number) { await this.shopAddToCart(item, quantity); return this.reviewAndSubmit(); }
  async openRequisitionByNumber(num: string) {
    await this.go(NAV.manage_requisitions);
    await this.field('Requisition').fill(num);
    await this.btn('Search').click();
    await this.link(num).click();
    await expect(this.value('req-number')).toHaveText(num);
  }
  async openFromNotifications(docNumber: string) {
    await this.btn('Notifications').click();
    await this.table('Notifications').getByRole('link', { name: docNumber, exact: true }).click();
  }
  async decide(decision: 'Approve' | 'Reject' | 'Request Information', comment = '') {
    if (comment) await this.field('Comments').fill(comment);
    await this.btn(decision).click();
    return this.outcome();
  }
  historyRows() { return this.table('Approval History').getByRole('row'); }

  // ---------------- orders ----------------
  async createOrder(o: { quantity: number; price: number; description?: string; matchType?: '3-Way' | '2-Way' }) {
    await this.go(NAV.home);
    await this.task('Create Order');
    await this.field('Supplier').selectOption({ label: MOCK.supplier });
    await this.field('Description').fill(o.description ?? 'A4 copy paper, box (MOCK)');
    await this.field('Quantity').fill(String(o.quantity));
    await this.field('Price').fill(String(o.price));
    if (o.matchType) await this.field('Match Approval Level').selectOption({ label: o.matchType });
    await this.btn('Create').click();
    await this.expectOk('created');
    return (await this.value('po-number').textContent())!.trim();
  }
  async openOrder(num: string) {
    await this.go(NAV.purchase_orders);
    await this.field('Order').fill(num);
    await this.btn('Search').click();
    await this.link(num).click();
    await expect(this.value('po-number')).toHaveText(num);
  }

  // ---------------- receiving ----------------
  async receive(poNumber: string, quantity: number) {
    await this.go(NAV.home);
    await this.task('Receive Expected Shipments');
    await this.field('Purchase Order').fill(poNumber);
    await this.btn('Search').click();
    await this.row('Expected Shipments', poNumber).getByRole('button', { name: 'Receive' }).click();
    await this.field('Quantity').fill(String(quantity));
    await this.btn('Submit').click();
    await this.expectOk('Receipt');
    return (await this.value('receipt-number').textContent())!.trim();
  }

  // ---------------- invoices ----------------
  async createPoInvoice(o: { poNumber: string; number: string; amount: number | string; quantity: number; unitPrice: number | string; taxRateCode?: string; taxAmount?: string }) {
    await this.go(NAV.home);
    await this.task('Create Invoice');
    await this.field('Business Unit').selectOption(MOCK.bu);
    await this.field('Identifying PO').fill(o.poNumber);
    await this.field('Number').fill(o.number);
    await this.field('Amount').fill(String(o.amount));
    await this.field('Quantity').fill(String(o.quantity));
    await this.field('Unit Price').fill(String(o.unitPrice));
    if (o.taxRateCode) await this.field('Tax Rate Code').selectOption(o.taxRateCode);
    if (o.taxAmount !== undefined) await this.field('Tax Amount').fill(o.taxAmount);
    await this.btn('Save').click();
    await this.expectOk('saved');
  }
  async validate() { await this.btn('Validate').click(); await this.expectOk('validation status'); }
  async openInvoice(num: string) {
    await this.go(NAV.invoices);
    await this.field('Invoice Number').fill(num);
    await this.btn('Search').click();
    await this.link(num).click();
    await expect(this.value('inv-number')).toHaveText(num);
  }
  holdRow(code: string): Locator { return this.table('Holds').getByRole('row').filter({ has: this.page.getByTestId('hold-code').filter({ hasText: new RegExp(`^${code}$`) }) }); }

  // ---------------- payments ----------------
  async submitPpr(o: { name: string; supplier?: string; bank?: string; ppp?: string; review?: 'Stop process for review' | 'No review' }) {
    await this.go(NAV.home);
    await this.task('Submit Payment Process Request');
    await this.field('Name').fill(o.name);
    await this.field('Pay Through Date').fill(new Date(Date.now() + 30 * 864e5).toISOString().slice(0, 10));
    await this.field('Supplier or Party').selectOption({ label: o.supplier ?? MOCK.supplier });
    await this.field('Disbursement Bank Account').selectOption(o.bank ?? MOCK.bank);
    await this.field('Payment Process Profile').selectOption(o.ppp ?? MOCK.isoPpp);
    await this.field('Review installments').selectOption({ label: o.review ?? 'Stop process for review' });
    await this.btn('Submit').click();
    await this.expectOk('submitted');
  }
  async openPpr(name: string) {
    await this.task('Manage Payment Process Requests');
    await this.field('Name').fill(name);
    await this.btn('Search').click();
    await this.link(name).click();
  }

  // ---------------- scheduled processes ----------------
  /** Submit a scheduled process; returns the process id after its Status cell reads Succeeded. */
  async runProcess(name: string, params: Record<string, string> = {}) {
    await this.go(NAV.scheduled_processes);
    await this.btn('Schedule New Process').click();
    const dlg = this.page.getByRole('group', { name: 'Schedule New Process' });
    await dlg.getByLabel('Name', { exact: true }).selectOption(name);
    for (const [label, v] of Object.entries(params)) {
      const f = dlg.getByLabel(label, { exact: true });
      if ((await f.evaluate((el) => el.tagName)) === 'SELECT') await f.selectOption(v); else await f.fill(v);
    }
    await dlg.getByRole('button', { name: 'Submit' }).click();
    await this.expectOk(/Process \d+ submitted/);
    const id = /Process (\d+)/.exec((await this.ok.textContent()) || '')![1];
    await expect(this.value(`process-status-${id}`)).toHaveText('SUCCEEDED');
    return id;
  }
}