-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtestUtils.ts
110 lines (99 loc) · 3.78 KB
/
testUtils.ts
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
import { Notification, VSBrowser, NotificationType, Workbench, SideBarView, ModalDialog } from 'vscode-extension-tester';
/**
* @author Ondrej Dockal <odockal@redhat.com>
*/
export async function getNotifications(...types: NotificationType[]): Promise<Notification[]> {
const center = await new Workbench().openNotificationsCenter();
const notifications: Notification[] = [];
await Promise.all(
types.map(async (type) => {
notifications.push(...(await center.getNotifications(type)));
}),
);
return notifications;
}
export async function cleanUpNotifications(): Promise<void> {
await new Workbench().executeCommand('test');
const nc = await new Workbench().openNotificationsCenter();
await nc.getDriver().sleep(1000);
const notifications = await nc.getNotifications(NotificationType.Any);
if (notifications.length > 0) {
await nc.clearAllNotifications();
await nc.getDriver().sleep(1000);
}
await nc.close();
}
export async function findNotification(text: string): Promise<Notification | undefined> {
await new Workbench().executeCommand('test');
const center = await new Workbench().openNotificationsCenter();
await center.getDriver().sleep(1000);
const notifications = await center.getNotifications(NotificationType.Any);
// eslint-disable-next-line no-restricted-syntax
for (const notification of notifications) {
// eslint-disable-next-line no-await-in-loop
const message = await notification.getMessage();
if (message.includes(text)) {
return notification;
}
}
return undefined;
}
export async function notificationExists(text: string): Promise<boolean> {
const notifications = await getNotifications(NotificationType.Any);
// eslint-disable-next-line no-restricted-syntax
for (const notification of notifications) {
if (notification) {
// eslint-disable-next-line no-await-in-loop
const message = await notification.getMessage();
if (message.includes(text)) {
return true;
}
}
}
return false;
}
export async function safeNotificationExists(text: string): Promise<boolean> {
let result: boolean;
try {
result = await notificationExists(text);
} catch (err) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (err.name === 'StaleElementReferenceError') {
result = await notificationExists(text);
} else {
throw err;
}
}
return result;
}
// eslint-disable-next-line @typescript-eslint/ban-types
export async function waitForEvent(func: Function, timeout: number): Promise<unknown | undefined> {
const obj = await VSBrowser.instance.driver.wait(func, timeout);
return obj;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const asyncFilter = async (arr, predicate): Promise<void> => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const results = await Promise.all(arr.map(predicate));
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
return arr.filter((_v, index) => results[index]);
};
export async function sectionHasItems(sideBar: SideBarView): Promise<boolean> {
const sections = await sideBar.getContent().getSections();
return (await sections[0].getVisibleItems()).length > 0;
}
export async function sectionHasItem(sideBar: SideBarView, name: string): Promise<boolean> {
const section = await sideBar.getContent().getSection(name);
return !!section;
}
export async function modalDialogExists(name: string): Promise<ModalDialog | undefined> {
try {
const dialog = new ModalDialog();
if ((await dialog.getDetails()).indexOf(name) >= 0) {
return dialog;
}
} catch (error) {
// omit error
}
return undefined;
}