mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-12 11:50:22 +03:00
fix(evaluate): fallback to toJSON if it exists when serializing (#15188)
* fix(evaluate): fallback to toJSON if it exists when serializing * fix test in ff * window.performance test
This commit is contained in:
parent
8c7f8eda9f
commit
e34fa4feeb
@ -42,7 +42,11 @@ export function source() {
|
||||
}
|
||||
|
||||
function isError(obj: any): obj is Error {
|
||||
try {
|
||||
return obj instanceof Error || (obj && obj.__proto__ && obj.__proto__.name === 'Error');
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function parseEvaluationResultValue(value: SerializedValue, handles: any[] = [], refs: Map<number, object> = new Map()): any {
|
||||
@ -177,6 +181,11 @@ export function source() {
|
||||
else
|
||||
o.push({ k: name, v: serialize(item, handleSerializer, visitorInfo) });
|
||||
}
|
||||
|
||||
// If Object.keys().length === 0 we fall back to toJSON if it exists
|
||||
if (o.length === 0 && value.toJSON && typeof value.toJSON === 'function')
|
||||
return innerSerialize(value.toJSON(), handleSerializer, visitorInfo);
|
||||
|
||||
return { o, id };
|
||||
}
|
||||
}
|
||||
|
@ -330,6 +330,53 @@ it('should properly serialize null fields', async ({ page }) => {
|
||||
expect(await page.evaluate(() => ({ a: null }))).toEqual({ a: null });
|
||||
});
|
||||
|
||||
it('should properly serialize PerformanceMeasure object', async ({ page }) => {
|
||||
expect(await page.evaluate(() => {
|
||||
window.performance.mark('start');
|
||||
window.performance.mark('end');
|
||||
window.performance.measure('my-measure', 'start', 'end');
|
||||
return performance.getEntriesByType('measure');
|
||||
})).toEqual([{
|
||||
duration: expect.any(Number),
|
||||
entryType: 'measure',
|
||||
name: 'my-measure',
|
||||
startTime: expect.any(Number),
|
||||
}]);
|
||||
});
|
||||
|
||||
it('shuld properly serialize window.performance object', async ({ page }) => {
|
||||
expect(await page.evaluate(() => performance)).toEqual({
|
||||
'navigation': {
|
||||
'redirectCount': 0,
|
||||
'type': 0
|
||||
},
|
||||
'timeOrigin': expect.any(Number),
|
||||
'timing': {
|
||||
'connectEnd': expect.any(Number),
|
||||
'connectStart': expect.any(Number),
|
||||
'domComplete': expect.any(Number),
|
||||
'domContentLoadedEventEnd': expect.any(Number),
|
||||
'domContentLoadedEventStart': expect.any(Number),
|
||||
'domInteractive': expect.any(Number),
|
||||
'domLoading': expect.any(Number),
|
||||
'domainLookupEnd': expect.any(Number),
|
||||
'domainLookupStart': expect.any(Number),
|
||||
'fetchStart': expect.any(Number),
|
||||
'loadEventEnd': expect.any(Number),
|
||||
'loadEventStart': expect.any(Number),
|
||||
'navigationStart': expect.any(Number),
|
||||
'redirectEnd': expect.any(Number),
|
||||
'redirectStart': expect.any(Number),
|
||||
'requestStart': expect.any(Number),
|
||||
'responseEnd': expect.any(Number),
|
||||
'responseStart': expect.any(Number),
|
||||
'secureConnectionStart': expect.any(Number),
|
||||
'unloadEventEnd': expect.any(Number),
|
||||
'unloadEventStart': expect.any(Number),
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should return undefined for non-serializable objects', async ({ page }) => {
|
||||
expect(await page.evaluate(() => function() {})).toBe(undefined);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user