forked from webex/widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvancedTaskControlUtils.ts
More file actions
296 lines (263 loc) · 9.96 KB
/
Copy pathadvancedTaskControlUtils.ts
File metadata and controls
296 lines (263 loc) · 9.96 KB
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
import {Page, expect} from '@playwright/test';
import {loginExtension} from './incomingTaskUtils';
import {dismissOverlays} from './helperUtils';
import {AWAIT_TIMEOUT, FORM_FIELD_TIMEOUT, EXTENSION_REGISTRATION_TIMEOUT} from '../constants';
/**
* Utility functions for advanced task controls testing.
* Provides functions for consult operations, transfer operations, and end consult actions.
* These utilities handle complex multi-agent scenarios and task state transitions.
*
* @packageDocumentation
*/
// Array to store captured console logs for verification
let capturedAdvancedLogs: string[] = [];
/**
* Sets up console logging to capture transfer and consult related callback logs.
* Captures transfer success, consult start/end success, and related SDK messages.
* @param page - The agent's main page
* @returns Function to remove the console handler
*/
export function setupAdvancedConsoleLogging(page: Page): () => void {
capturedAdvancedLogs.length = 0;
const consoleHandler = (msg) => {
const logText = msg.text();
if (
logText.includes('WXCC_SDK_TASK_TRANSFER_SUCCESS') ||
logText.includes('WXCC_SDK_TASK_CONSULT_START_SUCCESS') ||
logText.includes('WXCC_SDK_TASK_CONSULT_END_SUCCESS') ||
logText.includes('AgentConsultTransferred') ||
logText.includes('onEnd invoked') ||
logText.includes('onTransfer invoked') ||
logText.includes('onConsult invoked')
) {
capturedAdvancedLogs.push(logText);
}
};
page.on('console', consoleHandler);
return () => page.off('console', consoleHandler);
}
/**
* Clears the captured advanced logs array.
* Should be called before each test or verification to ensure clean state.
*/
export function clearAdvancedCapturedLogs(): void {
capturedAdvancedLogs.length = 0;
}
/**
* Verifies that transfer success logs are present.
* @throws Error if verification fails with detailed error message
*/
export function verifyTransferSuccessLogs(): void {
const transferLogs = capturedAdvancedLogs.filter((log) => log.includes('WXCC_SDK_TASK_TRANSFER_SUCCESS'));
if (transferLogs.length === 0) {
throw new Error(
`No 'WXCC_SDK_TASK_TRANSFER_SUCCESS' logs found. Captured logs: ${JSON.stringify(capturedAdvancedLogs)}`
);
}
}
/**
* Verifies that consult start success logs are present.
* @throws Error if verification fails with detailed error message
*/
export function verifyConsultStartSuccessLogs(): void {
const consultStartLogs = capturedAdvancedLogs.filter((log) => log.includes('WXCC_SDK_TASK_CONSULT_START_SUCCESS'));
if (consultStartLogs.length === 0) {
throw new Error(
`No 'WXCC_SDK_TASK_CONSULT_START_SUCCESS' logs found. Captured logs: ${JSON.stringify(capturedAdvancedLogs)}`
);
}
}
/**
* Verifies that consult end success logs are present.
* @throws Error if verification fails with detailed error message
*/
export function verifyConsultEndSuccessLogs(): void {
const consultEndLogs = capturedAdvancedLogs.filter((log) => log.includes('WXCC_SDK_TASK_CONSULT_END_SUCCESS'));
if (consultEndLogs.length === 0) {
throw new Error(
`No 'WXCC_SDK_TASK_CONSULT_END_SUCCESS' logs found. Captured logs: ${JSON.stringify(capturedAdvancedLogs)}`
);
}
}
/**
* Verifies that agent consult transferred logs are present (when consult is converted to transfer).
* @throws Error if verification fails with detailed error message
*/
export function verifyConsultTransferredLogs(): void {
const consultTransferredLogs = capturedAdvancedLogs.filter((log) => log.includes('AgentConsultTransferred'));
if (consultTransferredLogs.length === 0) {
throw new Error(`No 'AgentConsultTransferred' logs found. Captured logs: ${JSON.stringify(capturedAdvancedLogs)}`);
}
}
/**
* Unified function to handle consult and transfer actions for agent, queue, and dial number.
* @param page - The agent's main page
* @param type - 'agent' | 'queue' | 'dialNumber'
* @param action - 'consult' | 'transfer'
* @param value - agentName, queueName, or phoneNumber
* @returns Promise<void>
*/
export async function consultOrTransfer(
page: Page,
type: 'agent' | 'queue' | 'dialNumber' | 'entryPoint',
action: 'consult' | 'transfer',
value: string
): Promise<void> {
await page.bringToFront();
await openConsultOrTransferMenu(page, action);
const popover = await getPopover(page);
if (type === 'agent') {
await performAgentSelection(page, popover, value, action);
} else if (type === 'queue') {
await performQueueSelection(page, popover, value);
} else if (type === 'dialNumber') {
await performDialNumberSelection(page, popover, value);
} else if (type === 'entryPoint') {
await performEntryPointSelection(page, popover, value);
}
await page.waitForTimeout(2000);
}
// ===== Internal helper functions =====
async function openConsultOrTransferMenu(page: Page, action: 'consult' | 'transfer'): Promise<void> {
await page.bringToFront();
await dismissOverlays(page);
if (action === 'consult') {
await page.getByTestId('call-control:consult').first().click({timeout: AWAIT_TIMEOUT});
} else {
await page.getByTestId('call-control:transfer').first().click({timeout: AWAIT_TIMEOUT});
}
}
async function getPopover(page: Page) {
const popover = page.locator('.agent-popover-content');
await expect(popover.locator('#consult-search')).toBeVisible({timeout: FORM_FIELD_TIMEOUT});
return popover;
}
async function clickCategory(
page: Page,
popover: ReturnType<Page['locator']>,
name: 'Agents' | 'Queues' | 'Dial Number' | 'Entry Point'
): Promise<void> {
const button = popover.getByRole('button', {name, exact: true});
await button.click({timeout: AWAIT_TIMEOUT});
await page.waitForTimeout(200);
}
async function clickListItemPrimaryButton(
page: Page,
popover: ReturnType<Page['locator']>,
value: string,
categoryLabel: string
): Promise<void> {
const listItem = popover.locator(`[role="listitem"][aria-label="${value}"]`).first();
await listItem.waitFor({state: 'visible', timeout: AWAIT_TIMEOUT});
await listItem.hover();
await listItem.scrollIntoViewIfNeeded();
await page.waitForTimeout(200);
const primaryButton = listItem.getByRole('button');
await primaryButton.waitFor({state: 'visible', timeout: AWAIT_TIMEOUT});
await primaryButton.scrollIntoViewIfNeeded();
await primaryButton.evaluate((el) => {
if (el.hasAttribute('disabled')) {
throw new Error(`${categoryLabel} button is disabled`);
}
});
let lastError;
for (let i = 0; i < 3; i++) {
try {
await primaryButton.click({timeout: AWAIT_TIMEOUT, force: true});
lastError = undefined;
break;
} catch (err) {
lastError = err;
await page.waitForTimeout(300);
}
}
if (lastError) {
throw lastError;
}
await page
.locator('.md-popover-backdrop')
.waitFor({state: 'hidden', timeout: 3000})
.catch(() => {});
}
async function performAgentSelection(
page: Page,
popover: ReturnType<Page['locator']>,
value: string,
action: 'consult' | 'transfer'
): Promise<void> {
const agentFirstName = value.split(' ')[0];
// Try up to 3 times: search, check visibility, reopen popover if needed.
// Backend propagation can delay agent availability in the list.
let currentPopover = popover;
for (let attempt = 0; attempt < 3; attempt++) {
if (attempt === 0) {
await clickCategory(page, currentPopover, 'Agents');
}
const searchBox = currentPopover.locator('#consult-search');
await searchBox.fill(agentFirstName);
await page.waitForTimeout(1000);
const listItem = currentPopover.locator(`[role="listitem"][aria-label="${value}"]`).first();
const isVisible = await listItem.isVisible().catch(() => false);
if (isVisible) {
await clickListItemPrimaryButton(page, currentPopover, value, 'Agent');
return;
}
// Close and reopen popover to force a fresh agent list fetch
await page.keyboard.press('Escape');
await page.waitForTimeout(3000);
await openConsultOrTransferMenu(page, action);
currentPopover = await getPopover(page);
await clickCategory(page, currentPopover, 'Agents');
}
// Final attempt — let it throw if agent still not found
await clickListItemPrimaryButton(page, currentPopover, value, 'Agent');
}
async function performQueueSelection(page: Page, popover: ReturnType<Page['locator']>, value: string): Promise<void> {
await clickCategory(page, popover, 'Queues');
await clickListItemPrimaryButton(page, popover, value, 'Queue');
}
async function performDialNumberSelection(
page: Page,
popover: ReturnType<Page['locator']>,
value: string
): Promise<void> {
if (!value || value.trim() === '') {
throw new Error(
'PW_DIAL_NUMBER_NAME is not set. Please provide the Dial Number list item name (e.g., cypher_pstn).'
);
}
await clickCategory(page, popover, 'Dial Number');
const search = popover.locator('#consult-search');
if (await search.isVisible({timeout: 500}).catch(() => false)) {
await search.fill(value, {timeout: AWAIT_TIMEOUT});
await page.waitForTimeout(300);
}
await popover
.getByRole('listitem', {name: value, exact: true})
.waitFor({state: 'visible', timeout: FORM_FIELD_TIMEOUT});
await clickListItemPrimaryButton(page, popover, value, 'Dial Number');
}
async function performEntryPointSelection(
page: Page,
popover: ReturnType<Page['locator']>,
value: string
): Promise<void> {
await clickCategory(page, popover, 'Entry Point');
if (value) {
const search = popover.locator('#consult-search');
if (await search.isVisible({timeout: 500}).catch(() => false)) {
await search.fill(value, {timeout: AWAIT_TIMEOUT});
await page.waitForTimeout(300);
}
}
await clickListItemPrimaryButton(page, popover, value, 'Entry Point');
}
/**
* Cancels an ongoing consult and resumes the original call.
* @param page - The agent's main page
* @returns Promise<void>
*/
export async function cancelConsult(page: Page): Promise<void> {
// Click cancel consult button
await page.getByTestId('cancel-consult-btn').click({timeout: AWAIT_TIMEOUT});
}