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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/**
* Copyright 2018 Google Inc. All rights reserved.
*
* 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 expect from 'expect';
import {
getTestState,
setupTestBrowserHooks,
setupTestPageAndContextHooks,
} from './mocha-utils'; // eslint-disable-line import/extensions
describe('JSHandle', function () {
setupTestBrowserHooks();
setupTestPageAndContextHooks();
describe('Page.evaluateHandle', function () {
it('should work', async () => {
const { page } = getTestState();
const windowHandle = await page.evaluateHandle(() => window);
expect(windowHandle).toBeTruthy();
});
it('should accept object handle as an argument', async () => {
const { page } = getTestState();
const navigatorHandle = await page.evaluateHandle(() => navigator);
const text = await page.evaluate(
(e: Navigator) => e.userAgent,
navigatorHandle
);
expect(text).toContain('Mozilla');
});
it('should accept object handle to primitive types', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => 5);
const isFive = await page.evaluate((e) => Object.is(e, 5), aHandle);
expect(isFive).toBeTruthy();
});
it('should warn on nested object handles', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => document.body);
let error = null;
await page
// @ts-expect-error we are deliberately passing a bad type here (nested object)
.evaluateHandle((opts) => opts.elem.querySelector('p'), {
elem: aHandle,
})
.catch((error_) => (error = error_));
expect(error.message).toContain('Are you passing a nested JSHandle?');
});
it('should accept object handle to unserializable value', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => Infinity);
expect(await page.evaluate((e) => Object.is(e, Infinity), aHandle)).toBe(
true
);
});
it('should use the same JS wrappers', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => {
globalThis.FOO = 123;
return window;
});
expect(await page.evaluate((e: { FOO: number }) => e.FOO, aHandle)).toBe(
123
);
});
it('should work with primitives', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => {
globalThis.FOO = 123;
return window;
});
expect(await page.evaluate((e: { FOO: number }) => e.FOO, aHandle)).toBe(
123
);
});
});
describe('JSHandle.getProperty', function () {
it('should work', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => ({
one: 1,
two: 2,
three: 3,
}));
const twoHandle = await aHandle.getProperty('two');
expect(await twoHandle.jsonValue()).toEqual(2);
});
});
describe('JSHandle.jsonValue', function () {
it('should work', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => ({ foo: 'bar' }));
const json = await aHandle.jsonValue();
expect(json).toEqual({ foo: 'bar' });
});
it('should not work with dates', async () => {
const { page } = getTestState();
const dateHandle = await page.evaluateHandle(
() => new Date('2017-09-26T00:00:00.000Z')
);
const json = await dateHandle.jsonValue();
expect(json).toEqual({});
});
it('should throw for circular objects', async () => {
const { page, isChrome } = getTestState();
const windowHandle = await page.evaluateHandle('window');
let error = null;
await windowHandle.jsonValue().catch((error_) => (error = error_));
if (isChrome)
expect(error.message).toContain('Object reference chain is too long');
else expect(error.message).toContain('Object is not serializable');
});
});
describe('JSHandle.getProperties', function () {
it('should work', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => ({
foo: 'bar',
}));
const properties = await aHandle.getProperties();
const foo = properties.get('foo');
expect(foo).toBeTruthy();
expect(await foo.jsonValue()).toBe('bar');
});
it('should return even non-own properties', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => {
class A {
a: string;
constructor() {
this.a = '1';
}
}
class B extends A {
b: string;
constructor() {
super();
this.b = '2';
}
}
return new B();
});
const properties = await aHandle.getProperties();
expect(await properties.get('a').jsonValue()).toBe('1');
expect(await properties.get('b').jsonValue()).toBe('2');
});
});
describe('JSHandle.asElement', function () {
it('should work', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => document.body);
const element = aHandle.asElement();
expect(element).toBeTruthy();
});
it('should return null for non-elements', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => 2);
const element = aHandle.asElement();
expect(element).toBeFalsy();
});
it('should return ElementHandle for TextNodes', async () => {
const { page } = getTestState();
await page.setContent('<div>ee!</div>');
const aHandle = await page.evaluateHandle(
() => document.querySelector('div').firstChild
);
const element = aHandle.asElement();
expect(element).toBeTruthy();
expect(
await page.evaluate(
(e: HTMLElement) => e.nodeType === Node.TEXT_NODE,
element
)
);
});
});
describe('JSHandle.toString', function () {
it('should work for primitives', async () => {
const { page } = getTestState();
const numberHandle = await page.evaluateHandle(() => 2);
expect(numberHandle.toString()).toBe('JSHandle:2');
const stringHandle = await page.evaluateHandle(() => 'a');
expect(stringHandle.toString()).toBe('JSHandle:a');
});
it('should work for complicated objects', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => window);
expect(aHandle.toString()).toBe('JSHandle@object');
});
it('should work with different subtypes', async () => {
const { page } = getTestState();
expect((await page.evaluateHandle('(function(){})')).toString()).toBe(
'JSHandle@function'
);
expect((await page.evaluateHandle('12')).toString()).toBe('JSHandle:12');
expect((await page.evaluateHandle('true')).toString()).toBe(
'JSHandle:true'
);
expect((await page.evaluateHandle('undefined')).toString()).toBe(
'JSHandle:undefined'
);
expect((await page.evaluateHandle('"foo"')).toString()).toBe(
'JSHandle:foo'
);
expect((await page.evaluateHandle('Symbol()')).toString()).toBe(
'JSHandle@symbol'
);
expect((await page.evaluateHandle('new Map()')).toString()).toBe(
'JSHandle@map'
);
expect((await page.evaluateHandle('new Set()')).toString()).toBe(
'JSHandle@set'
);
expect((await page.evaluateHandle('[]')).toString()).toBe(
'JSHandle@array'
);
expect((await page.evaluateHandle('null')).toString()).toBe(
'JSHandle:null'
);
expect((await page.evaluateHandle('/foo/')).toString()).toBe(
'JSHandle@regexp'
);
expect((await page.evaluateHandle('document.body')).toString()).toBe(
'JSHandle@node'
);
expect((await page.evaluateHandle('new Date()')).toString()).toBe(
'JSHandle@date'
);
expect((await page.evaluateHandle('new WeakMap()')).toString()).toBe(
'JSHandle@weakmap'
);
expect((await page.evaluateHandle('new WeakSet()')).toString()).toBe(
'JSHandle@weakset'
);
expect((await page.evaluateHandle('new Error()')).toString()).toBe(
'JSHandle@error'
);
expect((await page.evaluateHandle('new Int32Array()')).toString()).toBe(
'JSHandle@typedarray'
);
expect((await page.evaluateHandle('new Proxy({}, {})')).toString()).toBe(
'JSHandle@proxy'
);
});
});
});
|