/
channels.spec.ts
199 lines (181 loc) · 5.97 KB
/
channels.spec.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
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
/**
* Copyright 2017 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import domain from 'domain';
import { fixtures as baseFixtures } from './fixtures';
import type { ChromiumBrowser } from '..';
type DomainFixtures = {
domain: any;
};
const fixtures = baseFixtures.declareWorkerFixtures<DomainFixtures>();
fixtures.defineWorkerFixture('domain', async ({ }, test) => {
const local = domain.create();
local.run(() => { });
let err;
local.on('error', e => err = e);
await test(null);
if (err)
throw err;
});
const { it, expect } = fixtures;
it('should work', async ({browser}) => {
expect(!!browser['_connection']).toBeTruthy();
});
it('should scope context handles', async ({browserType, browser, server}) => {
const GOLDEN_PRECONDITION = {
_guid: '',
objects: [
{ _guid: 'BrowserType', objects: [
{ _guid: 'Browser', objects: [] }
] },
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'Playwright', objects: [] },
{ _guid: 'Selectors', objects: [] },
{ _guid: 'Electron', objects: [] },
]
};
await expectScopeState(browser, GOLDEN_PRECONDITION);
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
await expectScopeState(browser, {
_guid: '',
objects: [
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'BrowserType', objects: [
{ _guid: 'Browser', objects: [
{ _guid: 'BrowserContext', objects: [
{ _guid: 'Frame', objects: [] },
{ _guid: 'Page', objects: [] },
{ _guid: 'Request', objects: [] },
{ _guid: 'Response', objects: [] },
]},
] },
] },
{ _guid: 'Playwright', objects: [] },
{ _guid: 'Selectors', objects: [] },
{ _guid: 'Electron', objects: [] },
]
});
await context.close();
await expectScopeState(browser, GOLDEN_PRECONDITION);
});
it('should scope CDPSession handles', (test, { browserName }) => {
test.skip(browserName !== 'chromium');
}, async ({browserType, browser}) => {
const GOLDEN_PRECONDITION = {
_guid: '',
objects: [
{ _guid: 'BrowserType', objects: [
{ _guid: 'Browser', objects: [] }
] },
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'Playwright', objects: [] },
{ _guid: 'Selectors', objects: [] },
{ _guid: 'Electron', objects: [] },
]
};
await expectScopeState(browserType, GOLDEN_PRECONDITION);
const session = await (browser as ChromiumBrowser).newBrowserCDPSession();
await expectScopeState(browserType, {
_guid: '',
objects: [
{ _guid: 'BrowserType', objects: [
{ _guid: 'Browser', objects: [
{ _guid: 'CDPSession', objects: [] },
] },
] },
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'Playwright', objects: [] },
{ _guid: 'Selectors', objects: [] },
{ _guid: 'Electron', objects: [] },
]
});
await session.detach();
await expectScopeState(browserType, GOLDEN_PRECONDITION);
});
it('should scope browser handles', async ({browserType, defaultBrowserOptions}) => {
const GOLDEN_PRECONDITION = {
_guid: '',
objects: [
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'Playwright', objects: [] },
{ _guid: 'Selectors', objects: [] },
{ _guid: 'Electron', objects: [] },
]
};
await expectScopeState(browserType, GOLDEN_PRECONDITION);
const browser = await browserType.launch(defaultBrowserOptions);
await browser.newContext();
await expectScopeState(browserType, {
_guid: '',
objects: [
{ _guid: 'BrowserType', objects: [
{
_guid: 'Browser', objects: [
{ _guid: 'BrowserContext', objects: [] }
]
},
]
},
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'BrowserType', objects: [] },
{ _guid: 'Playwright', objects: [] },
{ _guid: 'Selectors', objects: [] },
{ _guid: 'Electron', objects: [] },
]
});
await browser.close();
await expectScopeState(browserType, GOLDEN_PRECONDITION);
});
it('should work with the domain module', async ({ domain, browserType }) => {
const browser = await browserType.launch();
const page = await browser.newPage();
const result = await page.evaluate(() => 1 + 1);
expect(result).toBe(2);
await browser.close();
});
async function expectScopeState(object, golden) {
golden = trimGuids(golden);
const remoteState = trimGuids(await object._channel.debugScopeState());
const localState = trimGuids(object._connection._debugScopeState());
expect(localState).toEqual(golden);
expect(remoteState).toEqual(golden);
}
function compareObjects(a, b) {
if (a._guid !== b._guid)
return a._guid.localeCompare(b._guid);
return a.objects.length - b.objects.length;
}
function trimGuids(object) {
if (Array.isArray(object))
return object.map(trimGuids).sort(compareObjects);
if (typeof object === 'object') {
const result = {};
for (const key in object)
result[key] = trimGuids(object[key]);
return result;
}
if (typeof object === 'string')
return object ? object.match(/[^@]+/)[0] : '';
return object;
}