parent
b871d5dbb6
commit
2d37430838
@ -191,7 +191,7 @@ export class JSHandle<HandleObjectType = unknown> {
|
||||
|
||||
/** Fetches a single property from the referenced object.
|
||||
*/
|
||||
async getProperty(propertyName: string): Promise<JSHandle | undefined> {
|
||||
async getProperty(propertyName: string): Promise<JSHandle> {
|
||||
const objectHandle = await this.evaluateHandle(
|
||||
(object: Element, propertyName: string) => {
|
||||
const result = { __proto__: null };
|
||||
@ -201,7 +201,8 @@ export class JSHandle<HandleObjectType = unknown> {
|
||||
propertyName
|
||||
);
|
||||
const properties = await objectHandle.getProperties();
|
||||
const result = properties.get(propertyName) || null;
|
||||
const result = properties.get(propertyName);
|
||||
assert(result instanceof JSHandle);
|
||||
await objectHandle.dispose();
|
||||
return result;
|
||||
}
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
import expect from 'expect';
|
||||
import { JSHandle } from '../lib/cjs/puppeteer/common/JSHandle.js';
|
||||
import {
|
||||
getTestState,
|
||||
setupTestBrowserHooks,
|
||||
@ -96,6 +97,19 @@ describe('JSHandle', function () {
|
||||
const twoHandle = await aHandle.getProperty('two');
|
||||
expect(await twoHandle.jsonValue()).toEqual(2);
|
||||
});
|
||||
|
||||
it('should return a JSHandle even if the property does not exist', async () => {
|
||||
const { page } = getTestState();
|
||||
|
||||
const aHandle = await page.evaluateHandle(() => ({
|
||||
one: 1,
|
||||
two: 2,
|
||||
three: 3,
|
||||
}));
|
||||
const undefinedHandle = await aHandle.getProperty('doesnotexist');
|
||||
expect(undefinedHandle).toBeInstanceOf(JSHandle);
|
||||
expect(await undefinedHandle.jsonValue()).toBe(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('JSHandle.jsonValue', function () {
|
||||
|
Loading…
Reference in New Issue
Block a user