|
| 1 | +import {Page, expect, BrowserContext} from '@playwright/test'; |
| 2 | +import dotenv from 'dotenv'; |
| 3 | +import {BASE_URL} from '../constants'; |
| 4 | + |
| 5 | +dotenv.config(); |
| 6 | + |
| 7 | +/** |
| 8 | + * Performs login using an access token from environment variables |
| 9 | + * @param page - The Playwright page object |
| 10 | + * @param agentId - Agent identifier to get access token for (e.g., 'AGENT1', 'AGENT2') |
| 11 | + * @description Requires PW_{agentId}_ACCESS_TOKEN environment variable to be set |
| 12 | + * @throws {Error} When PW_{agentId}_ACCESS_TOKEN environment variable is not defined |
| 13 | + * @example |
| 14 | + * ```typescript |
| 15 | + * // Ensure PW_AGENT1_ACCESS_TOKEN is set in .env file |
| 16 | + * await loginViaAccessToken(page, 'AGENT1'); |
| 17 | + * |
| 18 | + * // Different agents with their own access tokens |
| 19 | + * await loginViaAccessToken(page, 'AGENT2'); // Uses PW_AGENT2_ACCESS_TOKEN |
| 20 | + * await loginViaAccessToken(page, 'ADMIN'); // Uses PW_ADMIN_ACCESS_TOKEN |
| 21 | + * ``` |
| 22 | + */ |
| 23 | +export const loginViaAccessToken = async (page: Page, agentId: string): Promise<void> => { |
| 24 | + await page.goto(BASE_URL); |
| 25 | + const accessToken = process.env[`PW_${agentId}_ACCESS_TOKEN`]; |
| 26 | + await page.getByRole('textbox').click(); |
| 27 | + if (!accessToken) { |
| 28 | + throw new Error(`PW_${agentId}_ACCESS_TOKEN is not defined, OAuth failed`); |
| 29 | + } |
| 30 | + await page.getByRole('textbox').fill(accessToken); |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Performs OAuth login with Webex using agent credentials from environment variables |
| 35 | + * @param page - The Playwright page object |
| 36 | + * @param agentId - Agent identifier to validate against environment variables (e.g., 'AGENT1', 'AGENT2') |
| 37 | + * @description Validates credentials against PW_{agentId}_USERNAME and PW_{agentId}_PASSWORD |
| 38 | + * @throws {Error} When agent credentials are not found in environment variables |
| 39 | + * @example |
| 40 | + * ```typescript |
| 41 | + * // OAuth login with agent credentials from environment variables |
| 42 | + * await oauthLogin(page, 'AGENT1'); // validates against PW_AGENT1_USERNAME/PW_AGENT1_PASSWORD |
| 43 | + * await oauthLogin(page, 'AGENT2'); // validates against PW_AGENT2_USERNAME/PW_AGENT2_PASSWORD |
| 44 | + * await oauthLogin(page, 'ADMIN'); // validates against PW_ADMIN_USERNAME/PW_ADMIN_PASSWORD |
| 45 | + * ``` |
| 46 | + */ |
| 47 | +export const oauthLogin = async (page: Page, agentId: string): Promise<void> => { |
| 48 | + // Check 1: Validate agentId parameter is provided |
| 49 | + if (!agentId) { |
| 50 | + throw new Error('Agent ID parameter is required'); |
| 51 | + } |
| 52 | + |
| 53 | + // Check 2: Validate agentId is not empty string |
| 54 | + if (agentId.trim() === '') { |
| 55 | + throw new Error('Agent ID cannot be empty string'); |
| 56 | + } |
| 57 | + |
| 58 | + // Check 3: Get credentials from environment variables |
| 59 | + const username = process.env[`PW_${agentId}_USERNAME`]; |
| 60 | + const password = process.env[`PW_PASSWORD`]; |
| 61 | + // Check 4: Validate environment variables are set |
| 62 | + if (!username || !password) { |
| 63 | + throw new Error(`Environment variables PW_${agentId}_USERNAME and PW_PASSWORD must be set`); |
| 64 | + } |
| 65 | + |
| 66 | + await page.goto(BASE_URL); |
| 67 | + await page.locator('#select-base-triggerid').getByText('Access Token').click(); |
| 68 | + await page.getByTestId('samples:login_option_oauth').getByText('Login with Webex').click(); |
| 69 | + await page.getByTestId('samples:login_with_webex_button').click(); |
| 70 | + await page.getByRole('textbox', {name: 'name@example.com'}).fill(username); |
| 71 | + await page.getByRole('link', {name: 'Sign in'}).click(); |
| 72 | + await page.getByRole('textbox', {name: 'Password'}).fill(password); |
| 73 | + await page.getByRole('button', {name: 'Sign in'}).click(); |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * Enables all available contact center widgets |
| 78 | + * @param page - The Playwright page object |
| 79 | + * @description Checks all widget checkboxes including station login, user state, tasks, and call controls |
| 80 | + * @example |
| 81 | + * ```typescript |
| 82 | + * await enableAllWidgets(page); |
| 83 | + * await initialiseWidgets(page); // Now all widgets will be available |
| 84 | + * ``` |
| 85 | + */ |
| 86 | +export const enableAllWidgets = async (page: Page): Promise<void> => { |
| 87 | + await page.getByTestId('samples:widget-stationLogin').check(); |
| 88 | + await page.getByTestId('samples:widget-userState').check(); |
| 89 | + await page.getByTestId('samples:widget-incomingTask').check(); |
| 90 | + await page.getByTestId('samples:widget-taskList').check(); |
| 91 | + await page.getByTestId('samples:widget-callControl').check(); |
| 92 | + await page.getByTestId('samples:widget-callControlCAD').check(); |
| 93 | + await page.getByTestId('samples:widget-outdialCall').check(); |
| 94 | +}; |
| 95 | + |
| 96 | +/** |
| 97 | + * Enables multi-login functionality for the SDK |
| 98 | + * @param page - The Playwright page object |
| 99 | + * @description Must be called before SDK initialization to take effect |
| 100 | + * @example |
| 101 | + * ```typescript |
| 102 | + * await enableMultiLogin(page); |
| 103 | + * await initialiseWidgets(page); // Multi-login is now enabled |
| 104 | + * ``` |
| 105 | + */ |
| 106 | +export const enableMultiLogin = async (page: Page): Promise<void> => { |
| 107 | + await page.getByTestId('samples:multi-login-enable-checkbox').check(); |
| 108 | +}; |
| 109 | + |
| 110 | +/** |
| 111 | + * Disables multi-login functionality for the SDK |
| 112 | + * @param page - The Playwright page object |
| 113 | + * @description Must be called before SDK initialization to take effect |
| 114 | + * @example |
| 115 | + * ```typescript |
| 116 | + * await disableMultiLogin(page); |
| 117 | + * await initialiseWidgets(page); // Multi-login is now disabled |
| 118 | + * ``` |
| 119 | + */ |
| 120 | +export const disableMultiLogin = async (page: Page): Promise<void> => { |
| 121 | + await page.getByTestId('samples:multi-login-enable-checkbox').uncheck(); |
| 122 | +}; |
| 123 | + |
| 124 | +/** |
| 125 | + * Initializes the widgets by clicking the init widgets button and waiting for station-login widget to be visible |
| 126 | + * @param page - The Playwright page object |
| 127 | + * @description The station-login widget should be checked/enabled before using this function |
| 128 | + * @throws {Error} When station-login widget is not visible after initialization |
| 129 | + * @example |
| 130 | + * ```typescript |
| 131 | + * // Ensure station-login widget is checked first |
| 132 | + * await page.getByTestId('samples:widget-stationLogin').check(); |
| 133 | + * await initialiseWidgets(page); |
| 134 | + * ``` |
| 135 | + */ |
| 136 | +export const initialiseWidgets = async (page: Page): Promise<void> => { |
| 137 | + await page.getByTestId('samples:init-widgets-button').click(); |
| 138 | + |
| 139 | + await page.getByTestId('station-login-widget').waitFor({state: 'visible', timeout: 50000}); |
| 140 | +}; |
| 141 | + |
| 142 | +/** |
| 143 | + * Reloads the page and reinitializes widgets to simulate agent relogin |
| 144 | + * @param page - The Playwright page object |
| 145 | + * @description Useful for testing state persistence after page reload |
| 146 | + * @throws {Error} When widget reinitialization fails after reload |
| 147 | + * @example |
| 148 | + * ```typescript |
| 149 | + * // Test state persistence |
| 150 | + * await changeUserState(page, 'Available'); |
| 151 | + * await agentRelogin(page); // State should persist after reload |
| 152 | + * ``` |
| 153 | + */ |
| 154 | +// Helper method for agent relogin - simulates user login along with page reload |
| 155 | +export const agentRelogin = async (page: Page): Promise<void> => { |
| 156 | + await page.reload(); |
| 157 | + await initialiseWidgets(page); |
| 158 | +}; |
| 159 | + |
| 160 | +/** |
| 161 | + * Creates a new page in the same browser context for multi-login testing |
| 162 | + * @param context - The Playwright browser context |
| 163 | + * @returns Promise<Page> - The new page with widgets initialized |
| 164 | + * @description Useful for testing multi-login scenarios |
| 165 | + * @throws {Error} When widget initialization fails on the new page |
| 166 | + * @example |
| 167 | + * ```typescript |
| 168 | + * const context = await browser.newContext(); |
| 169 | + * const primaryPage = await context.newPage(); |
| 170 | + * const secondaryPage = await setupMultiLoginPage(context); |
| 171 | + * |
| 172 | + * // Test state synchronization between pages |
| 173 | + * await changeUserState(primaryPage, 'Available'); |
| 174 | + * await verifyCurrentState(secondaryPage, 'Available'); |
| 175 | + * ``` |
| 176 | + */ |
| 177 | +// Helper method for multisession - creates new page and initializes widgets in same context |
| 178 | +export const setupMultiLoginPage = async (context: BrowserContext): Promise<Page> => { |
| 179 | + const multiLoginPage = await context.newPage(); |
| 180 | + await multiLoginPage.goto(BASE_URL); |
| 181 | + await initialiseWidgets(multiLoginPage); |
| 182 | + return multiLoginPage; |
| 183 | +}; |
0 commit comments