-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsharedCommands.ts
More file actions
654 lines (577 loc) · 23.3 KB
/
sharedCommands.ts
File metadata and controls
654 lines (577 loc) · 23.3 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
import {addStep, endStep, startStep} from '@wdio/allure-reporter';
import {isNil} from 'lodash';
import {ChainablePromiseArray, ChainablePromiseElement} from 'webdriverio';
// region LOGGING
/**
* Logs a message to the console and adds it as a step in the Allure report.
* It prefixes the message with a platform-specific tag.
* @param message The message to be logged and added to Allure.
*/
export async function addStepAndLog(message: string): Promise<void> {
let modifiedMessage: string;
if (browser.isMobile) {
if (browser.isIOS) {
modifiedMessage = `[iOS] ${message}`
} else {
modifiedMessage = `[Android] ${message}`
}
} else {
modifiedMessage = `[Web] ${message}`
}
await addStep(modifiedMessage);
console.log(modifiedMessage);
}
/**
* Starts a step in the Allure report with a given message and logs it to the console.
* It prefixes the message with a platform-specific tag.
* @param message The message for starting a step and logging.
*/
export async function startStepAndLog(message: string): Promise<void> {
let modifiedMessage: string;
if (browser.isMobile) {
if (browser.isIOS) {
modifiedMessage = `[iOS] ${message}`
} else {
modifiedMessage = `[Android] ${message}`
}
} else {
modifiedMessage = `[Web] ${message}`
}
await startStep(modifiedMessage);
console.log(modifiedMessage);
}
// endregion LOGGING
//region SELECTORS
type Selector = {
android?: string;
ios?: string;
mobileBrowser?: string;
web?: string;
variables?: Record<string, string>;
iosSelectionMethod?: (selector: string) => ChainablePromiseElement;
androidSelectionMethod?: (selector: string) => ChainablePromiseElement;
};
type SelectorArray = {
android?: string;
ios?: string;
mobileBrowser?: string;
web?: string;
variables?: Record<string, string>;
iosSelectionMethod?: (selector: string) => ChainablePromiseArray;
androidSelectionMethod?: (selector: string) => ChainablePromiseArray;
};
/**
* Selects the element with the selector for the specific platform.
* @param selector The selector, the selector can define variables with {{variableName}} that are replaced
* with the corresponding value defined in the variables object.
* @param selector.android Android selector.
* @param selector.ios iOS selector.
* @param selector.web Web selector.
* @param selector.mobileBrowser Mobile browser selector.
* @param selector.variables Variables that can be replaced in the selector string.
* @param selector.iosSelectionMethod The iosSelectionMethod specifies a special selection method on iOS.
* The possible methods are:
* - getByPredicateString
* - getByClassChain
* - getByAccessibilityId
* @param selector.androidSelectionMethod The androidSelectionMethod specifies a special selection method on Android.
* The possible method is:
* - getById
* - getByAccessibilityId
* If no method is specified, browser.$(selector) is used (which means a XPath/CSS selector).
* @returns The selected element.
*/
export function select(selector: Selector): ChainablePromiseElement {
const {android, ios, mobileBrowser, web} = selector;
const {iosSelectionMethod, androidSelectionMethod, variables} = selector;
const {webSelector, mobileBrowserSelector, androidSelector, iosSelector} =
applyVariablesToSelectors(variables, web, mobileBrowser, android, ios);
if (browser.isNativeContext) {
if (browser.isAndroid) {
if (androidSelectionMethod) {
return androidSelectionMethod(androidSelector);
} else {
return browser.$(androidSelector);
}
} else if (browser.isIOS) {
if (iosSelectionMethod) {
return iosSelectionMethod(iosSelector);
} else {
return browser.$(iosSelector);
}
}
}
if (browser.isMobile && mobileBrowserSelector) {
return browser.$(mobileBrowserSelector);
} else {
return browser.$(webSelector);
}
}
/**
* Selects the element with the selector for the specific platform.
* @param selector The selector, the selector can define variables with {{variableName}} that are replaced
* with the corresponding value defined in the variables object.
* @param selector.android Android selector.
* @param selector.ios iOS selector.
* @param selector.web Web selector.
* @param selector.mobileBrowser Mobile browser selector.
* @param selector.variables Variables that can be replaced in the selector string.
* @param selector.iosSelectionMethod The iosSelectionMethod specifies a special selection method on iOS.
* The possible methods are:
* - getArrayByPredicateString
* - getArrayByClassChain
* - getArrayByAccessibilityId
* @param selector.androidSelectionMethod The androidSelectionMethod specifies a special selection method on Android.
* The possible method is:
* - getArrayById
* - getArrayByAccessibilityId
* If no method is specified, browser.$$(selector) is used (which means a XPath/CSS selector).
* @returns The selected array of elements.
*/
export function selectArray(selector: SelectorArray): ChainablePromiseArray {
const {android, ios, mobileBrowser, web} = selector;
const {iosSelectionMethod, androidSelectionMethod, variables} = selector;
const {webSelector, mobileBrowserSelector, androidSelector, iosSelector} =
applyVariablesToSelectors(variables, web, mobileBrowser, android, ios);
if (browser.isNativeContext) {
if (browser.isAndroid) {
if (androidSelectionMethod) {
return androidSelectionMethod(androidSelector);
} else {
return browser.$$(androidSelector);
}
} else if (browser.isIOS) {
if (iosSelectionMethod) {
return iosSelectionMethod(iosSelector);
} else {
return browser.$$(iosSelector);
}
}
}
if (browser.isMobile && mobileBrowserSelector) {
return browser.$$(mobileBrowserSelector);
} else {
return browser.$$(webSelector);
}
}
/**
* If there are variables provided in the select() or selectArray() functions
* they will be used to update the provided selectors
* @param variables Variables to be used for an update
* @param webSelector Web selector that may contain variables
* @param mobileBrowserSelector Mobile browser selector that may contain variables
* @param androidSelector Android selector that may contain variables
* @param iosSelector iOS selector that may contain variables
* @returns mobileSelector, webSelector Updated selectors with variables overridden to actual values
*/
function applyVariablesToSelectors(
variables: Record<string, string>,
webSelector: string,
mobileBrowserSelector: string,
androidSelector: string,
iosSelector: string,
) {
if (!isNil(variables)) {
for (const variableKey in variables) {
if (Object.hasOwn(variables, variableKey)) {
const variableValue = variables[variableKey];
if (isNil(variableValue)) {
continue;
}
webSelector = webSelector.replaceAll(`{{${variableKey}}}`, variableValue);
mobileBrowserSelector = mobileBrowserSelector.replaceAll(`{{${variableKey}}}`, variableValue);
androidSelector = androidSelector.replaceAll(`{{${variableKey}}}`, variableValue);
iosSelector = iosSelector.replaceAll(`{{${variableKey}}}`, variableValue);
}
}
}
return {
webSelector,
mobileBrowserSelector,
androidSelector,
iosSelector,
};
}
/**
* This selector can be used for Android automation, if the resource-id is provided in the app.
*
* https://github.com/appium/appium-uiautomator2-driver?tab=readme-ov-file#element-location
* @param searchBy The ID used to locate the element.
* @returns ChainablePromiseElement A WebdriverIO element promise, allowing further chainable operations.
*/
export function getById(searchBy: string): ChainablePromiseElement {
return browser.$(`id:${searchBy}`);
}
/**
* This selector can be used for Android automation, if the resource-id is provided in the app.
*
* https://github.com/appium/appium-uiautomator2-driver?tab=readme-ov-file#element-location
* @param searchBy The ID used to locate the array of elements.
* @returns ChainablePromiseArray An WebdriverIO elements promise array, allowing further chainable operations.
*/
export function getArrayById(searchBy: string): ChainablePromiseArray {
return browser.$$(`id:${searchBy}`);
}
/**
* This selector can be used for both iOS and Android automation,
* if the Accessibility ID is provided in the app.
*
* @param searchBy The accessibility ID used to locate the element.
* @returns ChainablePromiseElement A WebdriverIO element promise, allowing further chainable operations.
*/
export function getByAccessibilityId(searchBy: string): ChainablePromiseElement {
return browser.$(`~${searchBy}`);
}
/**
* This selector can be used for both iOS and Android automation,
* if the Accessibility ID is provided in the app.
*
* https://webdriver.io/docs/selectors/#accessibility-id
* @param searchBy The accessibility ID used to locate the array of elements.
* @returns ChainablePromiseArray An WebdriverIO elements promise array, allowing further chainable operations.
*/
export function getArrayByAccessibilityId(searchBy: string): ChainablePromiseArray {
return browser.$$(`~${searchBy}`);
}
/**
* Finds an iOS element using a predicate string.
*
* https://webdriver.io/docs/selectors/#ios-xcuitest-predicate-strings-and-class-chains
* @param searchBy The predicate string used to locate the element.
* @returns ChainablePromiseElement A WebdriverIO element promise, allowing further chainable operations.
*/
export function getByPredicateString(searchBy: string): ChainablePromiseElement {
return browser.$(`-ios predicate string:${searchBy}`);
}
/**
* Finds an iOS element using a predicate string.
*
* https://webdriver.io/docs/selectors/#ios-xcuitest-predicate-strings-and-class-chains
* @param searchBy The predicate string used to locate the array of elements.
* @returns ChainablePromiseArray An WebdriverIO elements promise array, allowing further chainable operations.
*/
export function getArrayByPredicateString(searchBy: string): ChainablePromiseArray {
return browser.$$(`-ios predicate string:${searchBy}`);
}
/**
* Finds an iOS element using a class chain.
*
* https://webdriver.io/docs/selectors/#ios-xcuitest-predicate-strings-and-class-chains
* @param searchBy The class chain string used to locate the element.
* @returns ChainablePromiseElement A WebdriverIO element promise, allowing further chainable operations.
*/
export function getByClassChain(searchBy: string): ChainablePromiseElement {
return browser.$(`-ios class chain:${searchBy}`);
}
/**
* Finds an iOS element using a class chain.
*
* https://webdriver.io/docs/selectors/#ios-xcuitest-predicate-strings-and-class-chains
* @param searchBy The class chain string used to locate the array of elements.
* @returns ChainablePromiseArray An WebdriverIO elements promise array, allowing further chainable operations.
*/
export function getArrayByClassChain(searchBy: string): ChainablePromiseArray {
return browser.$$(`-ios class chain:${searchBy}`);
}
// endregion SELECTORS
// region TEST UTILITY FUNCTIONS
/**
* Retrieves and returns the text content of a specified element after ensuring it is displayed.
* @param element The WebdriverIO element from which to retrieve text.
* @returns Promise<string> A promise resolving to the text content of the element, useful for assertions and verification processes.
*/
export async function getTextFromElement(element: ChainablePromiseElement): Promise<string> {
await element.waitForDisplayed();
return await element.getText();
}
/**
* Verifies if a text value equals an expected text value and logs the verification step.
* @param title A descriptive title of the verification being performed.
* @param valueUnderVerification The actual value to verify.
* @param expectedValue The value expected for the verification to pass.
*/
export async function verifyTextEquality(
title: string,
valueUnderVerification: string,
expectedValue: string,
): Promise<void> {
const titleMessage = `Verify ${title}`;
const validationMessage = `${titleMessage} | Actual value: "${valueUnderVerification}" | Should equal: "${expectedValue}"`;
await startStepAndLog(titleMessage);
await addStepAndLog(validationMessage);
await expect(valueUnderVerification).toEqualString(expectedValue, {
message: validationMessage
});
await takeScreenshotWithTitle('After verification');
await endStep();
}
/**
* Verifies if a value contains an expected substring text and logs the verification step.
* @param title A descriptive title of the verification being performed.
* @param valueUnderVerification The actual value to verify.
* @param expectedValue The substring expected within the value for the verification to pass.
*/
export async function verifyPartialText(
title: string,
valueUnderVerification: string,
expectedValue: string,
): Promise<void> {
const titleMessage = `Verify ${title}`;
const validationMessage = `${titleMessage} | Actual value: "${valueUnderVerification}" | Should contain partial text: "${expectedValue}"`;
await startStepAndLog(titleMessage);
await addStepAndLog(validationMessage);
await expect(valueUnderVerification).toContainString(expectedValue, {
message: validationMessage
});
await takeScreenshotWithTitle('After verification');
await endStep();
}
/**
* Verifies if a value is greater than the provided base value and logs the verification step.
* @param title A descriptive title of the verification being performed.
* @param valueUnderVerification The actual value to verify.
* @param expectedValue The base value that the actual should be greater than.
*/
export async function verifyGreaterThan(
title: string,
valueUnderVerification: number,
expectedValue: number,
): Promise<void> {
const titleMessage = `Verify ${title}`;
const validationMessage = `${titleMessage} | Actual value: ${valueUnderVerification} | Should be greater than: ${expectedValue}`;
await startStepAndLog(titleMessage);
await addStepAndLog(validationMessage);
await expect(valueUnderVerification).toBeGreaterThanNumber(expectedValue, {
message: validationMessage
});
await takeScreenshotWithTitle('After verification');
await endStep();
}
/**
* Verifies if a value is less than the provided base value and logs the verification step.
* @param title A descriptive title of the verification being performed.
* @param valueUnderVerification The actual value to verify.
* @param expectedValue The base value that the actual should be less than.
*/
export async function verifyLessThan(
title: string,
valueUnderVerification: number,
expectedValue: number,
): Promise<void> {
const titleMessage = `Verify ${title}`;
const validationMessage = `${titleMessage} | Actual value: ${valueUnderVerification} | Should be less than: ${expectedValue}`;
await startStepAndLog(titleMessage);
await addStepAndLog(validationMessage);
await expect(valueUnderVerification).toBeLessThanNumber(expectedValue, {
message: validationMessage
});
await takeScreenshotWithTitle('After verification');
await endStep();
}
/**
* Verifies if a value is greater than or equal to the provided base value and logs the verification step.
* @param title A descriptive title of the verification being performed.
* @param valueUnderVerification The actual value to verify.
* @param expectedValue The base value that the actual should be greater than or equal to.
*/
export async function verifyGreaterThanOrEqual(
title: string,
valueUnderVerification: number,
expectedValue: number,
): Promise<void> {
const titleMessage = `Verify ${title}`;
const validationMessage = `${titleMessage} | Actual value: ${valueUnderVerification} | Should be greater than or equal to: ${expectedValue}`;
await startStepAndLog(titleMessage);
await addStepAndLog(validationMessage);
await expect(valueUnderVerification).toBeGreaterThanOrEqualNumber(expectedValue, {
message: validationMessage
});
await takeScreenshotWithTitle('After verification');
await endStep();
}
/**
* Verifies if a value is less than or equal to the provided base value and logs the verification step.
* @param title A descriptive title of the verification being performed.
* @param valueUnderVerification The actual value to verify.
* @param expectedValue The base value that the actual should be less than or equal to.
*/
export async function verifyLessThanOrEqual(
title: string,
valueUnderVerification: number,
expectedValue: number,
): Promise<void> {
const titleMessage = `Verify ${title}`;
const validationMessage = `${titleMessage} | Actual value: ${valueUnderVerification} | Should be less than or equal to: ${expectedValue}`;
await startStepAndLog(titleMessage);
await addStepAndLog(validationMessage);
await expect(valueUnderVerification).toBeLessThanOrEqualNumber(expectedValue, {
message: validationMessage
});
await takeScreenshotWithTitle('After verification');
await endStep();
}
/**
* Verifies if a numeric value equals an expected numeric value and logs the verification step.
* @param title A descriptive title of the verification being performed.
* @param valueUnderVerification The actual value to verify.
* @param expectedValue The value expected for the verification to pass.
*/
export async function verifyNumberEquality(
title: string,
valueUnderVerification: number,
expectedValue: number,
): Promise<void> {
const titleMessage = `Verify ${title}`;
const validationMessage = `${titleMessage} | Actual value: ${valueUnderVerification} | Should equal: ${expectedValue}`;
await startStepAndLog(titleMessage);
await addStepAndLog(validationMessage);
await expect(valueUnderVerification).toEqualNumber(expectedValue, {
message: validationMessage
});
await takeScreenshotWithTitle('After verification');
await endStep();
}
/**
* Verifies if a boolean value equals an expected boolean value and logs the verification step.
* @param title A descriptive title of the verification being performed.
* @param valueUnderVerification The actual boolean value to verify.
* @param expectedValue The expected boolean value.
*/
export async function verifyBooleanEquality(
title: string,
valueUnderVerification: boolean,
expectedValue: boolean,
): Promise<void> {
const titleMessage = `Verify ${title}`;
const validationMessage = `${titleMessage} | Actual value: ${valueUnderVerification} | Should equal: ${expectedValue}`;
await startStepAndLog(titleMessage);
await addStepAndLog(validationMessage);
await expect(valueUnderVerification).toEqualBoolean(expectedValue, {
message: validationMessage
});
await takeScreenshotWithTitle('After verification');
await endStep();
}
// endregion TEST UTILITY FUNCTIONS
// region WAIT FUNCTIONS
/**
* Waits until a page is completely loaded by ensuring all specified elements are displayed.
* @param elementDescription The title of the page being loaded.
* @param elementsForValidation A variable number of WebdriverIO elements to verify visibility.
* @returns Resolves when all elements are confirmed to be displayed, ensuring page load completion.
*/
export async function waitUntilElementsAreLoaded(
elementDescription: string,
...elementsForValidation: ChainablePromiseElement[]
): Promise<void> {
await startStepAndLog(`Wait until ${elementDescription} is loaded`);
if (elementsForValidation.length > 0) {
await Promise.all(
elementsForValidation.map(async (element: ChainablePromiseElement): Promise<void> => {
await element.waitForDisplayed();
}),
);
}
await endStep();
}
// endregion WAIT FUNCTIONS
// region UTILITIES
/**
* Inputs a given value into a specified element, considering platform-specific methods.
* @param element The WebdriverIO element to receive the input.
* @param valueToBeEntered The string value to be entered.
* @param inputName A name of the input for logging purposes.
*/
export async function enterValue(
element: ChainablePromiseElement,
valueToBeEntered: string,
inputName: string,
): Promise<void> {
await addStepAndLog(`Enter ${valueToBeEntered} to the ${inputName}`);
if (browser.isMobile && browser.isNativeContext) {
(browser.isIOS) ?
await enterTextValueCharByChar(element, valueToBeEntered) :
await element.sendKeys([valueToBeEntered]);
} else {
await element.setValue(valueToBeEntered);
}
}
/**
* Enters text into a web element character-by-character with validation to prevent
* the WebdriverIO setValue() bug that causes missing or corrupted characters.
*
* @param {ChainablePromiseElement} element - The WebdriverIO element to receive the text input.
* Should be a text input element that supports the `addValue()` method.
* @param {string} valueToBeEntered - The complete text string to be entered into the element.
* Can contain any valid UTF-8 characters including spaces, numbers, and special characters.
*
* @see {@link https://github.com/webdriverio/webdriverio/issues/1886} - WebdriverIO setValue() bug report
* @see {@link https://github.com/webdriverio/webdriverio/issues/686} - ChromeDriver character skipping issue
*/
export async function enterTextValueCharByChar(
element: ChainablePromiseElement,
valueToBeEntered: string,
): Promise<void> {
for (let i = 0; i < valueToBeEntered.length; i++) {
const char = valueToBeEntered[i]!;
await element.addValue(char);
await browser.waitUntil(
async () => {
const currentValue = await element.getValue();
return currentValue.length === i + 1 && currentValue.endsWith(char);
},
{timeout: 1000, interval: 50},
);
}
}
/**
* Pauses the execution for a specified number of milliseconds.
* @param ms The number of milliseconds to wait.
* @returns A promise that resolves after the specified delay, useful for timing or waiting scenarios.
*/
export async function sleep(ms: number): Promise<void> {
return await new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Takes a screenshot and saves it with a specified title in the Allure report.
* @param title The title to go with the screenshot in the Allure report.
* @returns Completes after the screenshot is successfully taken and logged in the report.
*/
export async function takeScreenshotWithTitle(title: string): Promise<void> {
await startStep(`[SCREENSHOT] ${title}`);
await browser.takeScreenshot();
await endStep();
}
/**
* Clicks a button and adds a step log.
* @param element Button to click on.
* @param buttonName A custom button name for logging.
* @param shouldScroll A flag that is helpful to avoid potential mobile testing failures due to scrolling issues
* (disabled by default).
*/
export async function clickElement(
element: ChainablePromiseElement,
buttonName: string,
shouldScroll: boolean = false,
): Promise<void> {
await addStepAndLog(`Click ${buttonName}`);
if (shouldScroll) {
await element.scrollIntoView();
}
await element.click();
}
/**
* Generates a formatted date-time string based on the local machine's current date and time
* @returns {string} A formatted string in YYYY-MM-DD HH:MM:SS format
*/
export function getDateTimeString(): string {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
//endregion UTILITIES