-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathlayout.ts
100 lines (83 loc) · 2.61 KB
/
layout.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
import { Page } from '@playwright/test';
import { BaseTarget } from '../lib/targets/base';
export abstract class BaseLayout {
readonly path?: string;
constructor(public page: Page, protected readonly target: BaseTarget) {}
protected get baseUrl() {
return this.target.baseUrl;
}
get url() {
return `${this.baseUrl}/${this.path}`;
}
goto(waitUntil: 'networkidle' | 'domcontentloaded' | 'load' = 'load') {
return this.page.goto(this.url, { waitUntil });
}
screenshot() {
return this.page.screenshot({ fullPage: true });
}
async checkWebChannelMessage(command) {
await this.page.evaluate(async (command) => {
const noNotificationError = new Error(
`NoSuchBrowserNotification - ${command}`
);
await new Promise((resolve, reject) => {
const timeoutHandle = setTimeout(
() => reject(noNotificationError),
2000
);
function findMessage() {
const messages = JSON.parse(
sessionStorage.getItem('webChannelEvents') || '[]'
);
const m = messages.find((x) => x.command === command);
if (m) {
clearTimeout(timeoutHandle);
resolve(m);
} else {
setTimeout(findMessage, 50);
}
}
findMessage();
});
}, command);
}
async noSuchWebChannelMessage(command) {
await this.page.evaluate(async (command) => {
const unexpectedNotificationError = new Error(
`UnepxectedBrowserNotification - ${command}`
);
await new Promise((resolve, reject) => {
const timeoutHandle = setTimeout(resolve, 1000);
function findMessage() {
const messages = JSON.parse(
sessionStorage.getItem('webChannelEvents') || '[]'
);
const m = messages.find((x) => x.command === command);
if (m) {
clearTimeout(timeoutHandle);
reject(unexpectedNotificationError);
} else {
setTimeout(findMessage, 50);
}
}
findMessage();
});
}, command);
}
async listenToWebChannelMessages() {
await this.page.evaluate(() => {
function listener(msg) {
const detail = JSON.parse(msg.detail);
const events = JSON.parse(
sessionStorage.getItem('webChannelEvents') || '[]'
);
events.push({
command: detail.message.command,
detail: detail.message.data,
});
sessionStorage.setItem('webChannelEvents', JSON.stringify(events));
}
addEventListener('WebChannelMessageToChrome', listener);
});
}
}