2020-08-04 02:30:37 +03:00
|
|
|
/**
|
|
|
|
* Copyright 2018 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.
|
|
|
|
*/
|
2020-08-20 07:32:12 +03:00
|
|
|
|
2021-05-06 17:08:22 +03:00
|
|
|
import { test as it, expect } from './pageTest';
|
2021-10-11 17:52:17 +03:00
|
|
|
import type { ElementHandle } from 'playwright-core';
|
2020-08-04 02:30:37 +03:00
|
|
|
|
2021-12-30 05:51:28 +03:00
|
|
|
it('should work #smoke', async ({ page }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
const aHandle = await page.evaluateHandle(() => ({
|
|
|
|
one: 1,
|
|
|
|
two: 2,
|
|
|
|
three: 3
|
|
|
|
}));
|
|
|
|
const twoHandle = await aHandle.getProperty('two');
|
|
|
|
expect(await twoHandle.jsonValue()).toEqual(2);
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should work with undefined, null, and empty', async ({ page }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
const aHandle = await page.evaluateHandle(() => ({
|
|
|
|
undefined: undefined,
|
|
|
|
null: null,
|
|
|
|
}));
|
|
|
|
const undefinedHandle = await aHandle.getProperty('undefined');
|
|
|
|
expect(String(await undefinedHandle.jsonValue())).toEqual('undefined');
|
|
|
|
const nullHandle = await aHandle.getProperty('null');
|
|
|
|
expect(await nullHandle.jsonValue()).toEqual(null);
|
|
|
|
const emptyhandle = await aHandle.getProperty('empty');
|
|
|
|
expect(String(await emptyhandle.jsonValue())).toEqual('undefined');
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should work with unserializable values', async ({ page }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
const aHandle = await page.evaluateHandle(() => ({
|
|
|
|
infinity: Infinity,
|
|
|
|
nInfinity: -Infinity,
|
|
|
|
nan: NaN,
|
|
|
|
nzero: -0
|
|
|
|
}));
|
|
|
|
const infinityHandle = await aHandle.getProperty('infinity');
|
|
|
|
expect(await infinityHandle.jsonValue()).toEqual(Infinity);
|
|
|
|
const nInfinityHandle = await aHandle.getProperty('nInfinity');
|
|
|
|
expect(await nInfinityHandle.jsonValue()).toEqual(-Infinity);
|
|
|
|
const nanHandle = await aHandle.getProperty('nan');
|
|
|
|
expect(String(await nanHandle.jsonValue())).toEqual('NaN');
|
|
|
|
const nzeroHandle = await aHandle.getProperty('nzero');
|
|
|
|
expect(await nzeroHandle.jsonValue()).toEqual(-0);
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('getProperties should work', async ({ page }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('getProperties should return empty map for non-objects', async ({ page }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
const aHandle = await page.evaluateHandle(() => 123);
|
|
|
|
const properties = await aHandle.getProperties();
|
|
|
|
expect(properties.size).toBe(0);
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('getProperties should return even non-own properties', async ({ page }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
const aHandle = await page.evaluateHandle(() => {
|
|
|
|
class A {
|
2020-08-12 01:50:53 +03:00
|
|
|
a: string;
|
2020-08-04 02:30:37 +03:00
|
|
|
constructor() {
|
|
|
|
this.a = '1';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
2020-08-12 01:50:53 +03:00
|
|
|
b: string;
|
2020-08-04 02:30:37 +03:00
|
|
|
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');
|
|
|
|
});
|
2021-01-05 00:54:55 +03:00
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('getProperties should work with elements', async ({ page }) => {
|
2021-01-05 00:54:55 +03:00
|
|
|
await page.setContent(`<div>Hello</div>`);
|
|
|
|
const handle = await page.evaluateHandle(() => ({ body: document.body }));
|
|
|
|
const properties = await handle.getProperties();
|
|
|
|
const body = properties.get('body') as ElementHandle;
|
|
|
|
expect(body).toBeTruthy();
|
|
|
|
expect(await body.textContent()).toBe('Hello');
|
|
|
|
});
|