|
| 1 | +import { Page, expect } from '@playwright/test'; |
| 2 | +import { WRAPUP_REASONS } from '../constants'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Utility functions for advanced task controls testing. |
| 6 | + * Provides functions for consult operations, transfer operations, and end consult actions. |
| 7 | + * These utilities handle complex multi-agent scenarios and task state transitions. |
| 8 | + * |
| 9 | + * @packageDocumentation |
| 10 | + */ |
| 11 | + |
| 12 | +// Array to store captured console logs for verification |
| 13 | +let capturedAdvancedLogs: string[] = []; |
| 14 | + |
| 15 | +/** |
| 16 | + * Sets up console logging to capture transfer and consult related callback logs. |
| 17 | + * Captures transfer success, consult start/end success, and related SDK messages. |
| 18 | + * @param page - The agent's main page |
| 19 | + * @returns Function to remove the console handler |
| 20 | + */ |
| 21 | +export function setupAdvancedConsoleLogging(page: Page): () => void { |
| 22 | + capturedAdvancedLogs.length = 0; |
| 23 | + |
| 24 | + const consoleHandler = (msg) => { |
| 25 | + const logText = msg.text(); |
| 26 | + if (logText.includes('WXCC_SDK_TASK_TRANSFER_SUCCESS') || |
| 27 | + logText.includes('WXCC_SDK_TASK_CONSULT_START_SUCCESS') || |
| 28 | + logText.includes('WXCC_SDK_TASK_CONSULT_END_SUCCESS') || |
| 29 | + logText.includes('AgentConsultTransferred') || |
| 30 | + logText.includes('onEnd invoked') || |
| 31 | + logText.includes('onTransfer invoked') || |
| 32 | + logText.includes('onConsult invoked')) { |
| 33 | + capturedAdvancedLogs.push(logText); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + page.on('console', consoleHandler); |
| 38 | + return () => page.off('console', consoleHandler); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Clears the captured advanced logs array. |
| 43 | + * Should be called before each test or verification to ensure clean state. |
| 44 | + */ |
| 45 | +export function clearAdvancedCapturedLogs(): void { |
| 46 | + capturedAdvancedLogs.length = 0; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Verifies that transfer success logs are present. |
| 51 | + * @throws Error if verification fails with detailed error message |
| 52 | + */ |
| 53 | +export function verifyTransferSuccessLogs(): void { |
| 54 | + const transferLogs = capturedAdvancedLogs.filter(log => log.includes('WXCC_SDK_TASK_TRANSFER_SUCCESS')); |
| 55 | + |
| 56 | + if (transferLogs.length === 0) { |
| 57 | + throw new Error(`No 'WXCC_SDK_TASK_TRANSFER_SUCCESS' logs found. Captured logs: ${JSON.stringify(capturedAdvancedLogs)}`); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Verifies that consult start success logs are present. |
| 63 | + * @throws Error if verification fails with detailed error message |
| 64 | + */ |
| 65 | +export function verifyConsultStartSuccessLogs(): void { |
| 66 | + const consultStartLogs = capturedAdvancedLogs.filter(log => log.includes('WXCC_SDK_TASK_CONSULT_START_SUCCESS')); |
| 67 | + |
| 68 | + if (consultStartLogs.length === 0) { |
| 69 | + throw new Error(`No 'WXCC_SDK_TASK_CONSULT_START_SUCCESS' logs found. Captured logs: ${JSON.stringify(capturedAdvancedLogs)}`); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Verifies that consult end success logs are present. |
| 75 | + * @throws Error if verification fails with detailed error message |
| 76 | + */ |
| 77 | +export function verifyConsultEndSuccessLogs(): void { |
| 78 | + const consultEndLogs = capturedAdvancedLogs.filter(log => log.includes('WXCC_SDK_TASK_CONSULT_END_SUCCESS')); |
| 79 | + |
| 80 | + if (consultEndLogs.length === 0) { |
| 81 | + throw new Error(`No 'WXCC_SDK_TASK_CONSULT_END_SUCCESS' logs found. Captured logs: ${JSON.stringify(capturedAdvancedLogs)}`); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Verifies that agent consult transferred logs are present (when consult is converted to transfer). |
| 87 | + * @throws Error if verification fails with detailed error message |
| 88 | + */ |
| 89 | +export function verifyConsultTransferredLogs(): void { |
| 90 | + const consultTransferredLogs = capturedAdvancedLogs.filter(log => log.includes('AgentConsultTransferred')); |
| 91 | + |
| 92 | + if (consultTransferredLogs.length === 0) { |
| 93 | + throw new Error(`No 'AgentConsultTransferred' logs found. Captured logs: ${JSON.stringify(capturedAdvancedLogs)}`); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Utility function to get all captured logs for debugging purposes. |
| 99 | + * @returns Array of all captured log messages |
| 100 | + */ |
| 101 | +export function getAllCapturedLogs(): string[] { |
| 102 | + return [...capturedAdvancedLogs]; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Initiates a consult with another agent via the agents tab. |
| 107 | + * @param page - The agent's main page |
| 108 | + * @param agentName - Name of the agent to consult with (e.g., 'User2 Agent2') |
| 109 | + * @returns Promise<void> |
| 110 | + */ |
| 111 | +export async function consultViaAgent(page: Page, agentName: string = 'User2 Agent2'): Promise<void> { |
| 112 | + // Click consult with another agent button |
| 113 | + await page.getByTestId('call-control:consult').nth(1).click(); |
| 114 | + // Navigate to Agents tab |
| 115 | + await page.getByRole('tab', { name: 'Agents' }).click(); |
| 116 | + |
| 117 | + //hover over the agent name - use exact match to avoid confusion with similar names |
| 118 | + await page.getByRole('listitem', { name: agentName, exact: true }).hover(); |
| 119 | + |
| 120 | + // Select the specific agent |
| 121 | + await page.getByRole('listitem', { name: agentName, exact: true }).getByRole('button').click(); |
| 122 | + |
| 123 | + // Wait a moment for the consult to be initiated |
| 124 | + await page.waitForTimeout(2000); |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Initiates a consult with a queue via the queues tab. |
| 129 | + * @param page - The agent's main page |
| 130 | + * @param queueName - Name of the queue to consult with (e.g., 'Customer Service Queue') |
| 131 | + * @returns Promise<void> |
| 132 | + */ |
| 133 | +export async function consultViaQueue(page: Page, queueName: string): Promise<void> { |
| 134 | + // Click consult with another agent button |
| 135 | + await page.getByTestId('call-control:consult').nth(1).click(); |
| 136 | + |
| 137 | + // Navigate to Queues tab |
| 138 | + await page.getByRole('tab', { name: 'Queues' }).click(); |
| 139 | + |
| 140 | + // Hover over the queue name - use exact match to avoid confusion with similar names |
| 141 | + await page.getByRole('listitem', { name: queueName, exact: true }).hover(); |
| 142 | + |
| 143 | + // Select the specific queue |
| 144 | + await page.getByRole('listitem', { name: queueName, exact: true }).getByRole('button').click(); |
| 145 | + |
| 146 | + // Wait a moment for the consult to be initiated |
| 147 | + await page.waitForTimeout(2000); |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Cancels an ongoing consult and resumes the original call. |
| 152 | + * @param page - The agent's main page |
| 153 | + * @returns Promise<void> |
| 154 | + */ |
| 155 | +export async function cancelConsult(page: Page): Promise<void> { |
| 156 | + // Click cancel consult button |
| 157 | + await page.getByTestId('cancel-consult-btn').click(); |
| 158 | + } |
| 159 | + |
| 160 | +/** |
| 161 | + * Initiates a transfer via the agents tab (without prior consult). |
| 162 | + * @param page - The agent's main page |
| 163 | + * @param agentName - Name of the agent to transfer to (e.g., 'User2 Agent2') |
| 164 | + * @returns Promise<void> |
| 165 | + */ |
| 166 | +export async function transferViaAgent(page: Page, agentName: string = 'User2 Agent2'): Promise<void> { |
| 167 | + // Click transfer call button |
| 168 | + await page.getByRole('group', { name: 'Call Control with Call' }).getByLabel('Transfer Call').click(); |
| 169 | + |
| 170 | + // Navigate to Agents tab |
| 171 | + await page.getByRole('tab', { name: 'Agents' }).click(); |
| 172 | + |
| 173 | + // Hover over the agent name - use exact match to avoid confusion with similar names |
| 174 | + await page.getByRole('listitem', { name: agentName, exact: true }).hover(); |
| 175 | + |
| 176 | + // Select the specific agent |
| 177 | + await page.getByRole('listitem', { name: agentName, exact: true }).getByRole('button').click(); |
| 178 | + |
| 179 | + // Wait a moment for the transfer to be processed |
| 180 | + await page.waitForTimeout(2000); |
| 181 | +} |
| 182 | + |
| 183 | +/** |
| 184 | + * Initiates a transfer via the queues tab (without prior consult). |
| 185 | + * @param page - The agent's main page |
| 186 | + * @param queueName - Name of the queue to transfer to (e.g., 'Customer Service Queue') |
| 187 | + * @returns Promise<void> |
| 188 | + */ |
| 189 | +export async function transferViaQueue(page: Page, queueName: string): Promise<void> { |
| 190 | + // Click transfer call button |
| 191 | + await page.getByRole('group', { name: 'Call Control with Call' }).getByLabel('Transfer Call').click(); |
| 192 | + |
| 193 | + // Navigate to Queues tab |
| 194 | + await page.getByRole('tab', { name: 'Queues' }).click(); |
| 195 | + |
| 196 | + // Hover over the queue name - use exact match to avoid confusion with similar names |
| 197 | + await page.getByRole('listitem', { name: queueName, exact: true }).hover(); |
| 198 | + |
| 199 | + // Select the specific queue |
| 200 | + await page.getByRole('listitem', { name: queueName, exact: true }).getByRole('button').click(); |
| 201 | + |
| 202 | + // Wait a moment for the transfer to be processed |
| 203 | + await page.waitForTimeout(2000); |
| 204 | +} |
0 commit comments