vasu-playwright-utils 0.11.1 → 0.11.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +15 -14
- package/src/vasu-playwright-lib/constants/index.ts +3 -0
- package/src/vasu-playwright-lib/constants/loadstate.ts +50 -0
- package/src/vasu-playwright-lib/constants/timeouts.ts +53 -0
- package/src/vasu-playwright-lib/index.ts +5 -0
- package/src/vasu-playwright-lib/setup/custom-logger.ts +75 -0
- package/src/vasu-playwright-lib/setup/index.ts +2 -0
- package/src/vasu-playwright-lib/types/index.ts +2 -0
- package/src/vasu-playwright-lib/types/optional-parameter-types.ts +66 -0
- package/src/vasu-playwright-lib/utils/action-utils.ts +343 -0
- package/src/vasu-playwright-lib/utils/assert-utils.ts +350 -0
- package/src/vasu-playwright-lib/utils/element-utils.ts +275 -0
- package/src/vasu-playwright-lib/utils/index.ts +10 -0
- package/src/vasu-playwright-lib/utils/locator-utils.ts +135 -0
- package/src/vasu-playwright-lib/utils/page-utils.ts +216 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vasu-playwright-utils",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.3",
|
|
4
4
|
"description": "Playwright Typescript Library with reusable utilities",
|
|
5
5
|
"main": "./dist/src/vasu-playwright-lib/index.js",
|
|
6
6
|
"types": "./dist/src/vasu-playwright-lib/index.d.ts",
|
|
@@ -38,30 +38,31 @@
|
|
|
38
38
|
"access": "public"
|
|
39
39
|
},
|
|
40
40
|
"files": [
|
|
41
|
-
"dist"
|
|
41
|
+
"dist",
|
|
42
|
+
"src"
|
|
42
43
|
],
|
|
43
44
|
"engines": {
|
|
44
45
|
"node": ">=18.0.0"
|
|
45
46
|
},
|
|
46
47
|
"dependencies": {
|
|
47
|
-
"@playwright/test": "~1.41.
|
|
48
|
-
"@types/node": "^20.11.
|
|
49
|
-
"@typescript-eslint/eslint-plugin": "^6.
|
|
50
|
-
"@typescript-eslint/parser": "^6.
|
|
48
|
+
"@playwright/test": "~1.41.1",
|
|
49
|
+
"@types/node": "^20.11.14",
|
|
50
|
+
"@typescript-eslint/eslint-plugin": "^6.20.0",
|
|
51
|
+
"@typescript-eslint/parser": "^6.20.0",
|
|
51
52
|
"allure-commandline": "^2.26.0",
|
|
52
|
-
"allure-playwright": "^2.
|
|
53
|
-
"axios": "^1.6.
|
|
53
|
+
"allure-playwright": "^2.11.1",
|
|
54
|
+
"axios": "^1.6.7",
|
|
54
55
|
"cross-env": "^7.0.3",
|
|
55
|
-
"dotenv": "^16.
|
|
56
|
+
"dotenv": "^16.4.1",
|
|
56
57
|
"eslint": "^8.56.0",
|
|
57
58
|
"eslint-config-prettier": "^9.1.0",
|
|
58
59
|
"eslint-import-resolver-typescript": "^3.6.1",
|
|
59
60
|
"eslint-plugin-import": "^2.29.1",
|
|
60
|
-
"eslint-plugin-jsdoc": "^48.0.
|
|
61
|
-
"eslint-plugin-playwright": "^0.
|
|
61
|
+
"eslint-plugin-jsdoc": "^48.0.4",
|
|
62
|
+
"eslint-plugin-playwright": "^0.22.1",
|
|
62
63
|
"eslint-plugin-prettier": "^5.1.3",
|
|
63
|
-
"husky": "^
|
|
64
|
-
"lint-staged": "^15.2.
|
|
64
|
+
"husky": "^9.0.7",
|
|
65
|
+
"lint-staged": "^15.2.1",
|
|
65
66
|
"prettier": "^3.2.4",
|
|
66
67
|
"rimraf": "^5.0.5",
|
|
67
68
|
"tslib": "^2.6.2",
|
|
@@ -75,7 +76,7 @@
|
|
|
75
76
|
"validate": "tsc --noEmit",
|
|
76
77
|
"prepublishOnly": "npm run build",
|
|
77
78
|
"postpublish": "npm run clean",
|
|
78
|
-
"ready": "rimraf dist node_modules package-lock.json && npm i && npm run validate && npm run build",
|
|
79
|
+
"ready": "rimraf dist node_modules package-lock.json && npm i && npm run validate && npm run build && npm run test",
|
|
79
80
|
"test": "playwright test",
|
|
80
81
|
"test:chromium": "playwright test --retries 0 --project=chromiumheadless",
|
|
81
82
|
"test:chromium-headed": "playwright test -j 1 --retries 0 --headed --project=chromium",
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* loadstate.ts
|
|
3
|
+
*
|
|
4
|
+
* This module provides two key constants: `defaultLoadState` and `defaultVisibleOnlyOption`.
|
|
5
|
+
*
|
|
6
|
+
* - `defaultLoadState` is used to set the initial page load state. It serves as the default value
|
|
7
|
+
* in functions such as `gotoURL`, `waitForPageLoadState`, and `clickAndNavigate`.
|
|
8
|
+
*
|
|
9
|
+
* - `defaultVisibleOnlyOption` configures the default visibility setting for locators. It dictates
|
|
10
|
+
* whether locators should, by default, find only visible elements. This setting is particularly
|
|
11
|
+
* useful in action utilities like `click`, `fill` etc.
|
|
12
|
+
*
|
|
13
|
+
* Both constants can be initialized within this module and can be overridden in specific functions
|
|
14
|
+
* as necessary, allowing for flexible and context-specific configurations.
|
|
15
|
+
*
|
|
16
|
+
* @module loadstate
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { WaitForLoadStateOptions } from '../types/optional-parameter-types';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Represents the load state option for waiting for specific events during page navigation.
|
|
23
|
+
* This constant is utilized in the gotoURL, waitForPageLoadState, and clickAndNavigate helper functions.
|
|
24
|
+
* It can be set to values like 'load', 'domcontentloaded', or 'networkidle'.
|
|
25
|
+
*/
|
|
26
|
+
let defaultLoadState: WaitForLoadStateOptions = 'domcontentloaded';
|
|
27
|
+
|
|
28
|
+
export function getDefaultLoadState(): WaitForLoadStateOptions {
|
|
29
|
+
return defaultLoadState;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function setDefaultLoadState(value: WaitForLoadStateOptions): void {
|
|
33
|
+
defaultLoadState = value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Default visibility setting for locators.
|
|
38
|
+
* This object holds the global configuration for whether locators should by default only find visible elements.
|
|
39
|
+
* It can be used across the project to maintain a consistent behavior for element visibility handling.
|
|
40
|
+
*/
|
|
41
|
+
export const defaultVisibleOnlyOption = { onlyVisible: true };
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Sets the default visibility filter for locator functions.
|
|
45
|
+
* This function allows changing the global default setting for whether locators should only find visible elements.
|
|
46
|
+
* @param value - The value to set for the default visibility filter (true if only visible elements should be found).
|
|
47
|
+
*/
|
|
48
|
+
export function setDefaultLocatorFilterVisibility(value: boolean): void {
|
|
49
|
+
defaultVisibleOnlyOption.onlyVisible = value;
|
|
50
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* timeouts.ts: This module provides Timeout constants that can be used to override various actions, conditional statements and assertions.
|
|
3
|
+
* Instead of hard coding the timeout when overriding any utility functions, use these Timeout constants.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Timeout constant for instant actions/assertions, set to 1000 milliseconds (1 second).
|
|
8
|
+
*/
|
|
9
|
+
export const INSTANT_TIMEOUT = 1000;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Timeout constant for small actions/assertions, set to 5000 milliseconds (5 seconds).
|
|
13
|
+
*/
|
|
14
|
+
export const SMALL_TIMEOUT = 5 * 1000;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Standard timeout constant, set to 15000 milliseconds (15 seconds).
|
|
18
|
+
*/
|
|
19
|
+
export const STANDARD_TIMEOUT = 15 * 1000;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Timeout constant for bigger actions/assertions, set to 30000 milliseconds (30 seconds).
|
|
23
|
+
*/
|
|
24
|
+
export const BIG_TIMEOUT = 30 * 1000;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Maximum timeout constant, set to 60000 milliseconds (1 minute).
|
|
28
|
+
*/
|
|
29
|
+
export const MAX_TIMEOUT = 60 * 1000;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Timeout constants used in the playwright.config.ts file.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Timeout constant for Playwright's expect function, set to 5000 milliseconds (5 seconds).
|
|
37
|
+
*/
|
|
38
|
+
export const EXPECT_TIMEOUT = 5 * 1000;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Timeout constant for Playwright's action functions, set to 5000 milliseconds (5 seconds).
|
|
42
|
+
*/
|
|
43
|
+
export const ACTION_TIMEOUT = 5 * 1000;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Timeout constant for Playwright's navigation functions, set to 30000 milliseconds (30 seconds).
|
|
47
|
+
*/
|
|
48
|
+
export const NAVIGATION_TIMEOUT = 30 * 1000;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Timeout constant for Playwright's test functions, set to 120000 milliseconds (2 minutes).
|
|
52
|
+
*/
|
|
53
|
+
export const TEST_TIMEOUT = 2 * 60 * 1000;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* custom-logger.ts: This module provides a custom logger for Playwright tests. It implements the Reporter interface from Playwright
|
|
3
|
+
* and uses the Winston logging library to provide detailed logs for test execution. The logger includes custom colors
|
|
4
|
+
* for different log levels and can be configured to log to the console or a file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Reporter, TestCase, TestError, TestResult } from '@playwright/test/reporter';
|
|
8
|
+
import winston from 'winston';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Custom colors for the logger
|
|
12
|
+
*/
|
|
13
|
+
const customColors = {
|
|
14
|
+
info: 'blue',
|
|
15
|
+
error: 'red',
|
|
16
|
+
};
|
|
17
|
+
winston.addColors(customColors);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Logger configuration
|
|
21
|
+
*/
|
|
22
|
+
export const logger = winston.createLogger({
|
|
23
|
+
level: 'info',
|
|
24
|
+
format: winston.format.combine(
|
|
25
|
+
winston.format.colorize({ all: true }),
|
|
26
|
+
winston.format.timestamp(),
|
|
27
|
+
winston.format.printf(({ timestamp, level, message }) => {
|
|
28
|
+
return `${timestamp} [${level}]: ${message}`;
|
|
29
|
+
}),
|
|
30
|
+
),
|
|
31
|
+
transports: [
|
|
32
|
+
new winston.transports.Console(),
|
|
33
|
+
// If you want to log to a file uncomment below line
|
|
34
|
+
// new winston.transports.File({ filename: 'logs/info.log', level: 'info' }),
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* CustomLogger class that implements the Reporter interface from Playwright
|
|
40
|
+
*/
|
|
41
|
+
export default class CustomLogger implements Reporter {
|
|
42
|
+
/**
|
|
43
|
+
* Logs the start of a test case
|
|
44
|
+
* @param {TestCase} test - The test case that is starting
|
|
45
|
+
*/
|
|
46
|
+
onTestBegin(test: TestCase): void {
|
|
47
|
+
logger.info(`Test Case Started : ${test.title}`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Logs the end of a test case
|
|
52
|
+
* @param {TestCase} test - The test case that ended
|
|
53
|
+
* @param {TestResult} result - The result of the test case
|
|
54
|
+
*/
|
|
55
|
+
onTestEnd(test: TestCase, result: TestResult): void {
|
|
56
|
+
if (result.status === 'passed') {
|
|
57
|
+
logger.info(`\x1b[32mTest Case Passed : ${test.title}\x1b[0m`); // Green color
|
|
58
|
+
} else if (result.status === 'skipped') {
|
|
59
|
+
logger.info(`\x1b[33mTest Case Skipped : ${test.title}\x1b[0m`); // Yellow color
|
|
60
|
+
} else if (result.status === 'failed' && result.error) {
|
|
61
|
+
// Playwright inbuild reporter logs the error
|
|
62
|
+
// logger.error(
|
|
63
|
+
// `Test Case Failed: ${test.title} Error: ${result.error.message}`,
|
|
64
|
+
// );
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Logs an error
|
|
70
|
+
* @param {TestError} error - The error
|
|
71
|
+
*/
|
|
72
|
+
onError(error: TestError): void {
|
|
73
|
+
logger.error(error.message);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types.ts: This module provides type definitions that are used as optional parameters for utility functions in other modules.
|
|
3
|
+
* These types are based on the parameters of Playwright's built-in methods and are used to provide type safety and code completion.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Locator, Page } from '@playwright/test';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 1. Navigation Options: These types are used for navigation actions such as going to a URL, reloading a page, or waiting for a certain load state.
|
|
10
|
+
* They are based on the parameters of Playwright's built-in navigation methods.
|
|
11
|
+
*/
|
|
12
|
+
export type GotoOptions = Parameters<Page['goto']>[1];
|
|
13
|
+
export type NavigationOptions = Parameters<Page['reload']>[0]; // Same for GoBack, GoForward
|
|
14
|
+
export type WaitForLoadStateOptions = Parameters<Page['waitForLoadState']>[0];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 2. Action Options: These types are used for actions such as clicking, filling input fields, typing, etc.
|
|
18
|
+
* They are based on the parameters of Playwright's built-in action methods.
|
|
19
|
+
*/
|
|
20
|
+
export type VisibilityOption = { onlyVisible?: boolean };
|
|
21
|
+
export type ClickOptions = Parameters<Locator['click']>[0] &
|
|
22
|
+
VisibilityOption & {
|
|
23
|
+
loadState?: WaitForLoadStateOptions;
|
|
24
|
+
};
|
|
25
|
+
export type FillOptions = Parameters<Locator['fill']>[1] & VisibilityOption;
|
|
26
|
+
export type PressSequentiallyOptions = Parameters<Locator['pressSequentially']>[1] & VisibilityOption;
|
|
27
|
+
export type ClearOptions = Parameters<Locator['clear']>[0] & VisibilityOption;
|
|
28
|
+
export type SelectValues = Parameters<Locator['selectOption']>[0] & VisibilityOption;
|
|
29
|
+
export type SelectOptions = Parameters<Locator['selectOption']>[1] & VisibilityOption;
|
|
30
|
+
export type CheckOptions = Parameters<Locator['check']>[0] & VisibilityOption;
|
|
31
|
+
export type HoverOptions = Parameters<Locator['hover']>[0] & VisibilityOption;
|
|
32
|
+
export type UploadValues = Parameters<Locator['setInputFiles']>[0] & VisibilityOption;
|
|
33
|
+
export type UploadOptions = Parameters<Locator['setInputFiles']>[1] & VisibilityOption;
|
|
34
|
+
export type DragOptions = Parameters<Locator['dragTo']>[1] & VisibilityOption;
|
|
35
|
+
export type DoubleClickOptions = Parameters<Locator['dblclick']>[0] & VisibilityOption;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 3. Expect Options: These types are used for assertions, Timeouts, etc in tests.
|
|
39
|
+
* They are based on the parameters of Playwright's built-in expect methods.
|
|
40
|
+
*/
|
|
41
|
+
export type TimeoutOption = { timeout?: number };
|
|
42
|
+
export type SoftOption = { soft?: boolean };
|
|
43
|
+
export type MessageOrOptions = string | { message?: string };
|
|
44
|
+
export type ExpectOptions = TimeoutOption & SoftOption & MessageOrOptions;
|
|
45
|
+
export type ExpectTextOptions = {
|
|
46
|
+
ignoreCase?: boolean;
|
|
47
|
+
useInnerText?: boolean;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type SwitchPageOptions = {
|
|
51
|
+
loadState?: 'load' | 'domcontentloaded' | 'networkidle';
|
|
52
|
+
timeout?: number;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 4. Locator Options: These types are used for locating elements on a page.
|
|
57
|
+
* They are based on the parameters of Playwright's built-in locator methods.
|
|
58
|
+
*/
|
|
59
|
+
export type LocatorOptions = Parameters<Page['locator']>[1] & VisibilityOption;
|
|
60
|
+
export type GetByTextOptions = Parameters<Locator['getByText']>[1] & VisibilityOption;
|
|
61
|
+
export type GetByRoleTypes = Parameters<Locator['getByRole']>[0] & VisibilityOption;
|
|
62
|
+
export type GetByRoleOptions = Parameters<Locator['getByRole']>[1] & VisibilityOption;
|
|
63
|
+
export type GetByLabelOptions = Parameters<Locator['getByLabel']>[1] & VisibilityOption;
|
|
64
|
+
export type GetByPlaceholderOptions = Parameters<Locator['getByPlaceholder']>[1] & VisibilityOption;
|
|
65
|
+
|
|
66
|
+
export type FrameOptions = Parameters<Page['frame']>[0];
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* action-utils.ts: This module provides a set of utility functions for performing various actions in Playwright tests.
|
|
3
|
+
* These actions include navigation, interaction with page elements, handling of dialogs, and more.
|
|
4
|
+
*/
|
|
5
|
+
import { Dialog, Locator } from '@playwright/test';
|
|
6
|
+
import { getPage } from './page-utils';
|
|
7
|
+
import {
|
|
8
|
+
CheckOptions,
|
|
9
|
+
ClearOptions,
|
|
10
|
+
ClickOptions,
|
|
11
|
+
DoubleClickOptions,
|
|
12
|
+
DragOptions,
|
|
13
|
+
FillOptions,
|
|
14
|
+
HoverOptions,
|
|
15
|
+
PressSequentiallyOptions,
|
|
16
|
+
SelectOptions,
|
|
17
|
+
TimeoutOption,
|
|
18
|
+
UploadOptions,
|
|
19
|
+
UploadValues,
|
|
20
|
+
VisibilityOption,
|
|
21
|
+
} from '../types/optional-parameter-types';
|
|
22
|
+
import { STANDARD_TIMEOUT } from '../constants/timeouts';
|
|
23
|
+
import { getLocator } from './locator-utils';
|
|
24
|
+
import { defaultVisibleOnlyOption, getDefaultLoadState } from '../constants/loadstate';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 1. Actions: This section contains functions for interacting with elements on a web page.
|
|
28
|
+
* These functions include clicking, filling input fields, typing, clearing input fields, checking and unchecking checkboxes, selecting options in dropdowns, and more.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Clicks on a specified element.
|
|
33
|
+
* @param {string | Locator} input - The element to click on.
|
|
34
|
+
* @param {ClickOptions} options - The click options.
|
|
35
|
+
*/
|
|
36
|
+
export async function click(input: string | Locator, options?: ClickOptions): Promise<void> {
|
|
37
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
38
|
+
await locator.click(options);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Clicks on a specified element and waits for navigation.
|
|
43
|
+
* @param {string | Locator} input - The element to click on.
|
|
44
|
+
* @param {ClickOptions} options - The click options.
|
|
45
|
+
*/
|
|
46
|
+
export async function clickAndNavigate(input: string | Locator, options?: ClickOptions): Promise<void> {
|
|
47
|
+
const timeout = options?.timeout || STANDARD_TIMEOUT;
|
|
48
|
+
await Promise.all([click(input, options), getPage().waitForEvent('framenavigated', { timeout: timeout })]);
|
|
49
|
+
await getPage().waitForLoadState(options?.loadState || getDefaultLoadState(), {
|
|
50
|
+
timeout: timeout,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Fills a specified element with a value.
|
|
56
|
+
* @param {string | Locator} input - The element to fill.
|
|
57
|
+
* @param {string} value - The value to fill the element with.
|
|
58
|
+
* @param {FillOptions} options - The fill options.
|
|
59
|
+
*/
|
|
60
|
+
export async function fill(input: string | Locator, value: string, options?: FillOptions): Promise<void> {
|
|
61
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
62
|
+
await locator.fill(value, options);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Fills a specified element with a value and press Enter.
|
|
67
|
+
* @param {string | Locator} input - The element to fill.
|
|
68
|
+
* @param {string} value - The value to fill the element with.
|
|
69
|
+
* @param {FillOptions} options - The fill options.
|
|
70
|
+
*/
|
|
71
|
+
export async function fillAndEnter(input: string | Locator, value: string, options?: FillOptions): Promise<void> {
|
|
72
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
73
|
+
await locator.fill(value, options);
|
|
74
|
+
await locator.press('Enter');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Types a value into a specified element, simulating keystrokes character by character.
|
|
79
|
+
* @param {string | Locator} input - The element into which the value will be typed. This can be either a string representing the selector or a Locator object.
|
|
80
|
+
* @param {string} value - The string value to be typed into the element, character by character.
|
|
81
|
+
* @param {PressSequentiallyOptions} [options] - Optional configuration for the typing action.
|
|
82
|
+
*/
|
|
83
|
+
export async function pressSequentially(
|
|
84
|
+
input: string | Locator,
|
|
85
|
+
value: string,
|
|
86
|
+
options?: PressSequentiallyOptions,
|
|
87
|
+
): Promise<void> {
|
|
88
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
89
|
+
await locator.pressSequentially(value, options);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Simulates the action of pressing a key or a combination of keys on the specified element.
|
|
94
|
+
* This function is useful for scenarios where you need to simulate key presses like 'Enter', 'Tab', etc.
|
|
95
|
+
* @param {string | Locator} input - The element on which the key press action will be performed. This can be either a string representing the selector or a Locator object.
|
|
96
|
+
* @param {string} key - The key or combination of keys to be pressed. For example, 'Enter', 'Tab', or 'Control+A'.
|
|
97
|
+
* @param {PressSequentiallyOptions} [options] - Optional configuration for the key press action. This can include options like delay between key presses.
|
|
98
|
+
*/
|
|
99
|
+
export async function pressKeyboard(
|
|
100
|
+
input: string | Locator,
|
|
101
|
+
key: string,
|
|
102
|
+
options?: PressSequentiallyOptions,
|
|
103
|
+
): Promise<void> {
|
|
104
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
105
|
+
await locator.press(key, options);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Clears the value of a specified element.
|
|
110
|
+
* @param {string | Locator} input - The element to clear.
|
|
111
|
+
* @param {ClearOptions} options - The clear options.
|
|
112
|
+
*/
|
|
113
|
+
export async function clear(input: string | Locator, options?: ClearOptions): Promise<void> {
|
|
114
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
115
|
+
await locator.clear(options);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Checks a specified checkbox or radio button.
|
|
120
|
+
* @param {string | Locator} input - The checkbox or radio button to check.
|
|
121
|
+
* @param {CheckOptions} options - The check options.
|
|
122
|
+
*/
|
|
123
|
+
export async function check(input: string | Locator, options?: CheckOptions): Promise<void> {
|
|
124
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
125
|
+
await locator.check(options);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Unchecks a specified checkbox or radio button.
|
|
130
|
+
* @param {string | Locator} input - The checkbox or radio button to uncheck.
|
|
131
|
+
* @param {CheckOptions} options - The uncheck options.
|
|
132
|
+
*/
|
|
133
|
+
export async function uncheck(input: string | Locator, options?: CheckOptions): Promise<void> {
|
|
134
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
135
|
+
await locator.uncheck(options);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Selects an option in a dropdown by its value.
|
|
140
|
+
* @param {string | Locator} input - The dropdown to select an option in.
|
|
141
|
+
* @param {string} value - The value of the option to select.
|
|
142
|
+
* @param {SelectOptions} options - The select options.
|
|
143
|
+
*/
|
|
144
|
+
export async function selectByValue(input: string | Locator, value: string, options?: SelectOptions): Promise<void> {
|
|
145
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
146
|
+
await locator.selectOption({ value: value }, options);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Selects options in a dropdown by their values (multi select).
|
|
151
|
+
* @param {string | Locator} input - The dropdown to select options in.
|
|
152
|
+
* @param {Array<string>} value - The values of the options to select.
|
|
153
|
+
* @param {SelectOptions} options - The select options.
|
|
154
|
+
*/
|
|
155
|
+
export async function selectByValues(
|
|
156
|
+
input: string | Locator,
|
|
157
|
+
value: Array<string>,
|
|
158
|
+
options?: SelectOptions,
|
|
159
|
+
): Promise<void> {
|
|
160
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
161
|
+
await locator.selectOption(value, options);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Selects an option in a dropdown by its text.
|
|
166
|
+
* @param {string | Locator} input - The dropdown to select an option in.
|
|
167
|
+
* @param {string} text - The text of the option to select.
|
|
168
|
+
* @param {SelectOptions} options - The select options.
|
|
169
|
+
*/
|
|
170
|
+
export async function selectByText(input: string | Locator, text: string, options?: SelectOptions): Promise<void> {
|
|
171
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
172
|
+
await locator.selectOption({ label: text }, options);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Selects an option in a dropdown by its index.
|
|
177
|
+
* @param {string | Locator} input - The dropdown to select an option in.
|
|
178
|
+
* @param {number} index - The index of the option to select.
|
|
179
|
+
* @param {SelectOptions} options - The select options.
|
|
180
|
+
*/
|
|
181
|
+
export async function selectByIndex(input: string | Locator, index: number, options?: SelectOptions): Promise<void> {
|
|
182
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
183
|
+
await locator.selectOption({ index: index }, options);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 2. Alerts: This section contains functions for handling alert dialogs.
|
|
188
|
+
* These functions include accepting and dismissing alerts, and getting the text of an alert.
|
|
189
|
+
* Note: These functions currently have some repetition and could be optimized by applying the DRY (Don't Repeat Yourself) principle.
|
|
190
|
+
*/
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Accepts an alert dialog.
|
|
194
|
+
* @param {string | Locator} input - The element to click to trigger the alert.
|
|
195
|
+
* @param {string} promptText - The text to enter into a prompt dialog.
|
|
196
|
+
* @returns {Promise<string>} - The message of the dialog.
|
|
197
|
+
*/
|
|
198
|
+
export async function acceptAlert(input: string | Locator, promptText?: string): Promise<string> {
|
|
199
|
+
const locator = getLocator(input);
|
|
200
|
+
let dialogMessage = '';
|
|
201
|
+
getPage().once('dialog', dialog => {
|
|
202
|
+
dialogMessage = dialog.message();
|
|
203
|
+
dialog.accept(promptText).catch(e => console.error('Error accepting dialog:', e));
|
|
204
|
+
});
|
|
205
|
+
await locator.click();
|
|
206
|
+
// temporary fix to alerts - Need to be fixed
|
|
207
|
+
// await getPage().waitForEvent('dialog');
|
|
208
|
+
return dialogMessage;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Dismisses an alert dialog.
|
|
213
|
+
* @param {string | Locator} input - The element to click to trigger the alert.
|
|
214
|
+
* @returns {Promise<string>} - The message of the dialog.
|
|
215
|
+
*/
|
|
216
|
+
export async function dismissAlert(input: string | Locator): Promise<string> {
|
|
217
|
+
const locator = getLocator(input);
|
|
218
|
+
let dialogMessage = '';
|
|
219
|
+
getPage().once('dialog', dialog => {
|
|
220
|
+
dialogMessage = dialog.message();
|
|
221
|
+
dialog.dismiss().catch(e => console.error('Error dismissing dialog:', e));
|
|
222
|
+
});
|
|
223
|
+
await locator.click({ noWaitAfter: true });
|
|
224
|
+
// temporary fix for alerts - Need to be fixed
|
|
225
|
+
// await getPage().waitForEvent('dialog');
|
|
226
|
+
return dialogMessage;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Gets the text of an alert dialog.
|
|
231
|
+
* @param {string | Locator} input - The element to click to trigger the alert.
|
|
232
|
+
* @returns {Promise<string>} - The message of the dialog.
|
|
233
|
+
*/
|
|
234
|
+
export async function getAlertText(input: string | Locator): Promise<string> {
|
|
235
|
+
const locator = getLocator(input);
|
|
236
|
+
let dialogMessage = '';
|
|
237
|
+
const dialogHandler = (dialog: Dialog) => {
|
|
238
|
+
dialogMessage = dialog.message();
|
|
239
|
+
};
|
|
240
|
+
getPage().once('dialog', dialogHandler);
|
|
241
|
+
await locator.click();
|
|
242
|
+
await getPage().waitForEvent('dialog');
|
|
243
|
+
getPage().off('dialog', dialogHandler);
|
|
244
|
+
return dialogMessage;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Hovers over a specified element.
|
|
249
|
+
* @param {string | Locator} input - The element to hover over.
|
|
250
|
+
* @param {HoverOptions} options - The hover options.
|
|
251
|
+
*/
|
|
252
|
+
export async function hover(input: string | Locator, options?: HoverOptions): Promise<void> {
|
|
253
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
254
|
+
await locator.hover(options);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Focuses on a specified element.
|
|
259
|
+
* @param {string | Locator} input - The element to focus on.
|
|
260
|
+
* @param {TimeoutOption} options - The timeout options.
|
|
261
|
+
*/
|
|
262
|
+
export async function focus(input: string | Locator, options?: TimeoutOption & VisibilityOption): Promise<void> {
|
|
263
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
264
|
+
await locator.focus(options);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Drags and drops a specified element to a destination.
|
|
269
|
+
* @param {string | Locator} input - The element to drag.
|
|
270
|
+
* @param {string | Locator} dest - The destination to drop the element at.
|
|
271
|
+
* @param {DragOptions} options - The drag options.
|
|
272
|
+
*/
|
|
273
|
+
export async function dragAndDrop(
|
|
274
|
+
input: string | Locator,
|
|
275
|
+
dest: string | Locator,
|
|
276
|
+
options?: DragOptions,
|
|
277
|
+
): Promise<void> {
|
|
278
|
+
const drag = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
279
|
+
const drop = getLocator(dest, { ...defaultVisibleOnlyOption, ...options });
|
|
280
|
+
await drag.dragTo(drop, options);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Double clicks on a specified element.
|
|
285
|
+
* @param {string | Locator} input - The element to double click on.
|
|
286
|
+
* @param {DoubleClickOptions} options - The double click options.
|
|
287
|
+
*/
|
|
288
|
+
export async function doubleClick(input: string | Locator, options?: DoubleClickOptions): Promise<void> {
|
|
289
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
290
|
+
await locator.dblclick(options);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Downloads a file from a specified element.
|
|
295
|
+
* @param {string | Locator} input - The element to download the file from.
|
|
296
|
+
* @param {string} path - The path to save the downloaded file to.
|
|
297
|
+
*/
|
|
298
|
+
export async function downloadFile(input: string | Locator, path: string, options?: ClickOptions): Promise<void> {
|
|
299
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
300
|
+
const downloadPromise = getPage().waitForEvent('download');
|
|
301
|
+
await click(locator, options);
|
|
302
|
+
const download = await downloadPromise;
|
|
303
|
+
// Wait for the download process to complete
|
|
304
|
+
console.log(await download.path());
|
|
305
|
+
// Save downloaded file somewhere
|
|
306
|
+
await download.saveAs(path);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Uploads files to a specified element.
|
|
311
|
+
* @param {string | Locator} input - The element to upload files to.
|
|
312
|
+
* @param {UploadValues} path - The files to upload.
|
|
313
|
+
* @param {UploadOptions} options - The upload options.
|
|
314
|
+
*/
|
|
315
|
+
export async function uploadFiles(input: string | Locator, path: UploadValues, options?: UploadOptions): Promise<void> {
|
|
316
|
+
const locator = getLocator(input, { ...defaultVisibleOnlyOption, ...options });
|
|
317
|
+
await locator.setInputFiles(path, options);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Scrolls a specified element into view.
|
|
322
|
+
* @param {string | Locator} input - The element to scroll into view.
|
|
323
|
+
* @param {TimeoutOption} options - The timeout options.
|
|
324
|
+
*/
|
|
325
|
+
export async function scrollLocatorIntoView(input: string | Locator, options?: TimeoutOption): Promise<void> {
|
|
326
|
+
const locator = getLocator(input);
|
|
327
|
+
await locator.scrollIntoViewIfNeeded(options);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* 3. JS: This section contains functions that use JavaScript to interact with elements on a web page.
|
|
332
|
+
* These functions include clicking on an element using JavaScript.
|
|
333
|
+
*/
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Clicks on a specified element using JavaScript.
|
|
337
|
+
* @param {string | Locator} input - The element to click on.
|
|
338
|
+
* @param {TimeoutOption} options - The timeout options.
|
|
339
|
+
*/
|
|
340
|
+
export async function clickByJS(input: string | Locator, options?: TimeoutOption): Promise<void> {
|
|
341
|
+
const locator = getLocator(input);
|
|
342
|
+
await locator.evaluate((el: HTMLElement) => el.click(), options);
|
|
343
|
+
}
|