This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathselectors.ts
More file actions
104 lines (97 loc) · 3.2 KB
/
selectors.ts
File metadata and controls
104 lines (97 loc) · 3.2 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
import { DappeteerElementHandle } from "../element";
import { DappeteerPage, Serializable } from "../page";
// TODO: change text() with '.';
export const getElementByContent = (
page: DappeteerPage,
text: string,
type = "*",
options?: { timeout?: number; visible?: boolean }
): Promise<DappeteerElementHandle | null> =>
page.waitForXPath(`//${type}[contains(text(), '${text}')]`, {
timeout: 20000,
visible: true,
...options,
});
export const getElementByTestId = (
page: DappeteerPage,
testId: string,
options: {
visible?: boolean;
detached?: boolean;
hidden?: boolean;
timeout?: number;
} = {}
): Promise<DappeteerElementHandle | null> =>
page.waitForSelector(`[data-testid="${testId}"]`, {
timeout: 20000,
visible: true,
...options,
});
export const getInputByLabel = (
page: DappeteerPage,
text: string,
excludeSpan = false,
timeout = 1000
): Promise<DappeteerElementHandle> =>
page.waitForXPath(
[
`//label[contains(.,'${text}')]/following-sibling::textarea`,
`//label[contains(.,'${text}')]/following-sibling::*//input`,
`//h6[contains(.,'${text}')]/parent::node()/parent::node()/following-sibling::input`,
`//h6[contains(.,'${text}')]/parent::node()/parent::node()/following-sibling::*//input`,
...(!excludeSpan
? [
`//span[contains(.,'${text}')]/parent::node()/parent::node()/following-sibling::*//input`,
`//span[contains(.,'${text}')]/following-sibling::*//input`,
]
: []),
].join("|"),
{ timeout, visible: true }
);
export const getSettingsSwitch = (
page: DappeteerPage,
text: string
): Promise<DappeteerElementHandle | null> =>
page.waitForXPath(
[
`//span[contains(.,'${text}')]/parent::div/following-sibling::div/div/div/div`,
`//span[contains(.,'${text}')]/parent::div/following-sibling::div/div/label/div`,
`//span[contains(.,'${text}')]/parent::div/following-sibling::div/label/div`,
].join("|"),
{ visible: true }
);
export const getErrorMessage = async (
page: DappeteerPage
): Promise<string | false> => {
const options: Parameters<DappeteerPage["waitForSelector"]>[1] = {
timeout: 1000,
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const errorElement: DappeteerElementHandle<Serializable> | null =
await Promise.race([
page.waitForSelector(`span.error`, options),
page.waitForSelector(`.typography--color-error-1`, options),
page.waitForSelector(`.typography--color-error-default`, options),
page.waitForSelector(`.box--color-error-default`, options),
page.waitForSelector(`.mm-box--color-error-default`, options),
]).catch(() => null);
if (!errorElement) return false;
return page.evaluate(
(node) => (node as unknown as HTMLElement).textContent,
errorElement.getSource()
);
};
export const getButton = async (
page: DappeteerPage,
text: string,
options?: {
timeout?: number;
visible?: boolean;
}
): Promise<DappeteerElementHandle> => {
return await Promise.race([
getElementByTestId(page, text, options),
getElementByContent(page, text, "button", options),
getElementByContent(page, text, "span", options),
]);
};