forked from webex/widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitUtils.ts
More file actions
203 lines (189 loc) · 9.12 KB
/
Copy pathinitUtils.ts
File metadata and controls
203 lines (189 loc) · 9.12 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
import {Page, expect, BrowserContext} from '@playwright/test';
import dotenv from 'dotenv';
import {BASE_URL, AWAIT_TIMEOUT, NETWORK_OPERATION_TIMEOUT, WIDGET_INIT_TIMEOUT, UI_SETTLE_TIMEOUT} from '../constants';
dotenv.config();
/**
* Performs login using an access token from environment variables
* @param page - The Playwright page object
* @param agentId - Agent identifier to get access token for (e.g., 'AGENT1', 'AGENT2')
* @description Requires PW_{agentId}_ACCESS_TOKEN environment variable to be set
* @throws {Error} When PW_{agentId}_ACCESS_TOKEN environment variable is not defined
* @example
* ```typescript
* // Ensure PW_AGENT1_ACCESS_TOKEN is set in .env file
* await loginViaAccessToken(page, 'AGENT1');
*
* // Different agents with their own access tokens
* await loginViaAccessToken(page, 'AGENT2'); // Uses PW_AGENT2_ACCESS_TOKEN
* await loginViaAccessToken(page, 'ADMIN'); // Uses PW_ADMIN_ACCESS_TOKEN
* ```
*/
export const loginViaAccessToken = async (page: Page, accessToken: string): Promise<void> => {
await page.goto(BASE_URL);
await page.getByRole('textbox').click({timeout: AWAIT_TIMEOUT});
if (!accessToken) {
throw new Error(`ACCESS_TOKEN is not defined, OAuth failed`);
}
await page.getByRole('textbox').fill(accessToken, {timeout: AWAIT_TIMEOUT});
};
/**
* Performs OAuth login with Webex using agent credentials from environment variables
* @param page - The Playwright page object
* @param agentId - Agent identifier to validate against environment variables (e.g., 'SET_1_AGENT1', 'SET_2_AGENT2')
* @param customPassword - Optional custom password. If not provided, uses PW_SANDBOX_PASSWORD from environment
* @description Validates credentials against {agentId}_USERNAME and PW_SANDBOX_PASSWORD (or custom password)
* @throws {Error} When agent credentials are not found in environment variables
* @example
* ```typescript
* // OAuth login with agent credentials from environment variables
* await oauthLogin(page, 'SET_1_AGENT1'); // validates against SET_1_AGENT1_USERNAME/PW_SANDBOX_PASSWORD
* await oauthLogin(page, 'SET_1_AGENT2'); // validates against SET_1_AGENT2_USERNAME/PW_SANDBOX_PASSWORD
* await oauthLogin(page, 'SET_2_AGENT1'); // validates against SET_2_AGENT1_USERNAME/PW_SANDBOX_PASSWORD
* await oauthLogin(page, 'custom_user', 'custom_password'); // uses custom password
* ```
*/
export const oauthLogin = async (page: Page, username: string, customPassword?: string): Promise<void> => {
// Check 1: Validate username parameter is provided
if (!username) {
throw new Error('Username parameter is required');
}
// Check 2: Validate username is not empty string
if (username.trim() === '') {
throw new Error('Username cannot be empty string');
}
// Check 3: Get credentials from environment variables or use custom password
const password = customPassword || process.env[`PW_SANDBOX_PASSWORD`];
// Check 6: Validate environment variables are set
if (!username || !password) {
throw new Error(`Environment variables ${username} and PW_SANDBOX_PASSWORD must be set`);
}
await page.goto(BASE_URL);
await page.locator('#select-base-triggerid').getByText('Access Token').click({timeout: AWAIT_TIMEOUT});
await page.getByTestId('samples:login_option_oauth').getByText('Login with Webex').click({timeout: AWAIT_TIMEOUT});
await page.getByTestId('samples:login_with_webex_button').click({timeout: AWAIT_TIMEOUT});
await page.waitForURL(/idbroker.*\.webex\.com/, {timeout: NETWORK_OPERATION_TIMEOUT});
await page.getByRole('textbox', {name: 'name@example.com'}).fill(username, {timeout: NETWORK_OPERATION_TIMEOUT});
await page.getByRole('link', {name: 'Sign in'}).click({timeout: NETWORK_OPERATION_TIMEOUT});
await page.getByRole('textbox', {name: 'Password'}).fill(password, {timeout: NETWORK_OPERATION_TIMEOUT});
await page.getByRole('button', {name: 'Sign in'}).click({timeout: NETWORK_OPERATION_TIMEOUT});
};
/**
* Enables all available contact center widgets
* @param page - The Playwright page object
* @description Checks all widget checkboxes including station login, user state, tasks, and call controls
* @example
* ```typescript
* await enableAllWidgets(page);
* await initialiseWidgets(page); // Now all widgets will be available
* ```
*/
export const enableAllWidgets = async (page: Page): Promise<void> => {
await page.getByTestId('samples:widget-stationLogin').check({timeout: AWAIT_TIMEOUT});
await page.getByTestId('samples:widget-userState').check({timeout: AWAIT_TIMEOUT});
await page.getByTestId('samples:widget-incomingTask').check({timeout: AWAIT_TIMEOUT});
await page.getByTestId('samples:widget-taskList').check({timeout: AWAIT_TIMEOUT});
await page.getByTestId('samples:widget-callControl').check({timeout: AWAIT_TIMEOUT});
await page.getByTestId('samples:widget-callControlCAD').check({timeout: AWAIT_TIMEOUT});
await page.getByTestId('samples:widget-outdialCall').check({timeout: AWAIT_TIMEOUT});
};
/**
* Enables multi-login functionality for the SDK
* @param page - The Playwright page object
* @description Must be called before SDK initialization to take effect
* @example
* ```typescript
* await enableMultiLogin(page);
* await initialiseWidgets(page); // Multi-login is now enabled
* ```
*/
export const enableMultiLogin = async (page: Page): Promise<void> => {
await page.getByTestId('samples:multi-login-enable-checkbox').check({timeout: AWAIT_TIMEOUT});
};
/**
* Disables multi-login functionality for the SDK
* @param page - The Playwright page object
* @description Must be called before SDK initialization to take effect
* @example
* ```typescript
* await disableMultiLogin(page);
* await initialiseWidgets(page); // Multi-login is now disabled
* ```
*/
export const disableMultiLogin = async (page: Page): Promise<void> => {
await page.getByTestId('samples:multi-login-enable-checkbox').uncheck({timeout: AWAIT_TIMEOUT});
};
/**
* Initializes the widgets by clicking the init widgets button and waiting for station-login widget to be visible
* @param page - The Playwright page object
* @description The station-login widget should be checked/enabled before using this function.
* If the widget is not visible after the initial timeout, retries once more with another attempt.
* @throws {Error} When station-login widget is not visible after two initialization attempts
* @example
* ```typescript
* // Ensure station-login widget is checked first
* await page.getByTestId('samples:widget-stationLogin').check();
* await initialiseWidgets(page);
* ```
*/
export const initialiseWidgets = async (page: Page): Promise<void> => {
await page.getByTestId('samples:init-widgets-button').click({timeout: AWAIT_TIMEOUT});
try {
await page.getByTestId('station-login-widget').waitFor({state: 'visible', timeout: WIDGET_INIT_TIMEOUT});
} catch (error) {
// First attempt failed, try clicking init widgets button again
await page.reload();
await page.waitForTimeout(UI_SETTLE_TIMEOUT); // Wait for page to settle
await page.getByTestId('samples:init-widgets-button').click({timeout: AWAIT_TIMEOUT});
try {
await page.getByTestId('station-login-widget').waitFor({state: 'visible', timeout: WIDGET_INIT_TIMEOUT});
} catch (secondError) {
// Second attempt also failed, throw error
const totalTimeout = (WIDGET_INIT_TIMEOUT * 2) / 1000;
throw new Error(
`Station login widget failed to become visible after two initialization attempts (${totalTimeout} seconds total)`
);
}
}
};
/**
* Reloads the page and reinitializes widgets to simulate agent relogin
* @param page - The Playwright page object
* @description Useful for testing state persistence after page reload
* @throws {Error} When widget reinitialization fails after reload
* @example
* ```typescript
* // Test state persistence
* await changeUserState(page, 'Available');
* await agentRelogin(page); // State should persist after reload
* ```
*/
// Helper method for agent relogin - simulates user login along with page reload
export const agentRelogin = async (page: Page): Promise<void> => {
await page.reload();
await page.waitForTimeout(UI_SETTLE_TIMEOUT); // Wait for page to settle
await initialiseWidgets(page);
};
/**
* Creates a new page in the same browser context for multi-login testing
* @param context - The Playwright browser context
* @returns Promise<Page> - The new page with widgets initialized
* @description Useful for testing multi-login scenarios
* @throws {Error} When widget initialization fails on the new page
* @example
* ```typescript
* const context = await browser.newContext();
* const primaryPage = await context.newPage();
* const secondaryPage = await setupMultiLoginPage(context);
*
* // Test state synchronization between pages
* await changeUserState(primaryPage, 'Available');
* await verifyCurrentState(secondaryPage, 'Available');
* ```
*/
// Helper method for multisession - creates new page and initializes widgets in same context
export const setupMultiLoginPage = async (context: BrowserContext): Promise<Page> => {
const multiLoginPage = await context.newPage();
await multiLoginPage.goto(BASE_URL);
await initialiseWidgets(multiLoginPage);
return multiLoginPage;
};