summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/test/src/jshandle.spec.ts
blob: 3d307a0523da35a777cc1799cc0a0531a0c5328d (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
 * 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.js';

describe('JSHandle', function () {
  setupTestBrowserHooks();
  setupTestPageAndContextHooks();

  describe('Page.evaluateHandle', function () {
    it('should work', async () => {
      const {page} = getTestState();

      const windowHandle = await page.evaluateHandle(() => {
        return window;
      });
      expect(windowHandle).toBeTruthy();
    });
    it('should return the RemoteObject', async () => {
      const {page} = getTestState();

      const windowHandle = await page.evaluateHandle(() => {
        return window;
      });
      expect(windowHandle.remoteObject()).toBeTruthy();
    });
    it('should accept object handle as an argument', async () => {
      const {page} = getTestState();

      const navigatorHandle = await page.evaluateHandle(() => {
        return navigator;
      });
      const text = await page.evaluate(e => {
        return e.userAgent;
      }, navigatorHandle);
      expect(text).toContain('Mozilla');
    });
    it('should accept object handle to primitive types', async () => {
      const {page} = getTestState();

      const aHandle = await page.evaluateHandle(() => {
        return 5;
      });
      const isFive = await page.evaluate(e => {
        return Object.is(e, 5);
      }, aHandle);
      expect(isFive).toBeTruthy();
    });
    it('should warn about recursive objects', async () => {
      const {page} = getTestState();

      const test: {obj?: unknown} = {};
      test.obj = test;
      let error!: Error;
      await page
        .evaluateHandle(opts => {
          return opts;
        }, test)
        .catch(error_ => {
          return (error = error_);
        });
      expect(error.message).toContain('Recursive objects are not allowed.');
    });
    it('should accept object handle to unserializable value', async () => {
      const {page} = getTestState();

      const aHandle = await page.evaluateHandle(() => {
        return Infinity;
      });
      expect(
        await page.evaluate(e => {
          return Object.is(e, Infinity);
        }, aHandle)
      ).toBe(true);
    });
    it('should use the same JS wrappers', async () => {
      const {page} = getTestState();

      const aHandle = await page.evaluateHandle(() => {
        (globalThis as any).FOO = 123;
        return window;
      });
      expect(
        await page.evaluate(e => {
          return (e as any).FOO;
        }, aHandle)
      ).toBe(123);
    });
  });

  describe('JSHandle.getProperty', function () {
    it('should work', async () => {
      const {page} = getTestState();

      const aHandle = await page.evaluateHandle(() => {
        return {
          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(() => {
        return {foo: 'bar'};
      });
      const json = await aHandle.jsonValue();
      expect(json).toEqual({foo: 'bar'});
    });

    it('works with jsonValues that are not objects', async () => {
      const {page} = getTestState();

      const aHandle = await page.evaluateHandle(() => {
        return ['a', 'b'];
      });
      const json = await aHandle.jsonValue();
      expect(json).toEqual(['a', 'b']);
    });

    it('works with jsonValues that are primitives', async () => {
      const {page} = getTestState();

      const aHandle = await page.evaluateHandle(() => {
        return 'foo';
      });
      expect(await aHandle.jsonValue()).toEqual('foo');

      const bHandle = await page.evaluateHandle(() => {
        return undefined;
      });
      expect(await bHandle.jsonValue()).toEqual(undefined);
    });

    it('should not work with dates', async () => {
      const {page} = getTestState();

      const dateHandle = await page.evaluateHandle(() => {
        return 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} = getTestState();

      const handle = await page.evaluateHandle(() => {
        const t: {t?: unknown; g: number} = {g: 1};
        t.t = t;
        return t;
      });
      let error!: Error;
      await handle.jsonValue().catch(error_ => {
        return (error = error_);
      });
      expect(error.message).toContain('Could not serialize referenced object');
    });
  });

  describe('JSHandle.getProperties', function () {
    it('should work', async () => {
      const {page} = getTestState();

      const aHandle = await page.evaluateHandle(() => {
        return {
          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(() => {
        return 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(() => {
        return 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(() => {
        return document.querySelector('div')!.firstChild;
      });
      const element = aHandle.asElement();
      expect(element).toBeTruthy();
      expect(
        await page.evaluate(e => {
          return e?.nodeType === Node.TEXT_NODE;
        }, element)
      );
    });
  });

  describe('JSHandle.toString', function () {
    it('should work for primitives', async () => {
      const {page} = getTestState();

      const numberHandle = await page.evaluateHandle(() => {
        return 2;
      });
      expect(numberHandle.toString()).toBe('JSHandle:2');
      const stringHandle = await page.evaluateHandle(() => {
        return 'a';
      });
      expect(stringHandle.toString()).toBe('JSHandle:a');
    });
    it('should work for complicated objects', async () => {
      const {page} = getTestState();

      const aHandle = await page.evaluateHandle(() => {
        return 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'
      );
    });
  });
});