diff --git a/src/bidiMapper/modules/script/Realm.ts b/src/bidiMapper/modules/script/Realm.ts index bafa648c42..2cffe7f5b0 100644 --- a/src/bidiMapper/modules/script/Realm.ts +++ b/src/bidiMapper/modules/script/Realm.ts @@ -112,15 +112,18 @@ export abstract class Realm { internalIdMap.set(weakLocalObjectReference, uuidv4()); } - (deepSerializedValue as any).internalId = internalIdMap.get( - weakLocalObjectReference, - ); + ( + deepSerializedValue as Protocol.Runtime.DeepSerializedValue & { + internalId?: string; + } + ).internalId = internalIdMap.get(weakLocalObjectReference); delete deepSerializedValue['weakLocalObjectReference']; } if ( - (deepSerializedValue as any).type === 'node' && - Object.hasOwn(deepSerializedValue?.value, 'frameId') + deepSerializedValue.type === 'node' && + deepSerializedValue.value && + Object.hasOwn(deepSerializedValue.value, 'frameId') ) { // `frameId` is not needed in BiDi as it is not yet specified. delete deepSerializedValue.value['frameId']; @@ -128,7 +131,7 @@ export abstract class Realm { // Platform object is a special case. It should have only `{type: object}` // without `value` field. - if ((deepSerializedValue as any).type === 'platformobject') { + if ((deepSerializedValue.type as string) === 'platformobject') { return {type: 'object'}; } diff --git a/tests/script/test_serialization.py b/tests/script/test_serialization.py index 27500eff4c..0131fed290 100644 --- a/tests/script/test_serialization.py +++ b/tests/script/test_serialization.py @@ -18,7 +18,8 @@ import pytest from anys import ANY_STR from test_helpers import (ANY_SHARED_ID, ANY_UUID, execute_command, goto_url, - read_JSON_message, send_JSON_command, subscribe) + read_JSON_message, send_JSON_command, + stabilize_key_values, subscribe) def _strip_handle(obj): @@ -394,8 +395,8 @@ async def test_serialization_internal_id(websocket, context_id): "method": "script.evaluate", "params": { "expression": "(()=>{" - " const foo={a: []};" - " const bar=[1,2];" + " const foo={a: [], document};" + " const bar=[1,2, document];" " const result={1: foo, 2: foo, 3: bar, 4: bar};" " result.self=result;" " return result;" @@ -408,26 +409,35 @@ async def test_serialization_internal_id(websocket, context_id): } }) - internal_id_1 = result["result"]["internalId"] - internal_id_2 = result["result"]["value"][0][1]["internalId"] - internal_id_3 = result["result"]["value"][2][1]["internalId"] + stabilize_key_values(result, ["internalId", "sharedId"]) assert result["result"] == { "type": "object", "handle": ANY_STR, - "internalId": internal_id_1, + "internalId": "stable_4", "value": [[ '1', { "type": "object", "value": [["a", { "type": "array", "value": [] - }]], - "internalId": internal_id_2 + }], + [ + 'document', { + 'internalId': 'stable_1', + 'sharedId': 'stable_0', + 'type': 'node', + 'value': { + 'childNodeCount': 1, + 'nodeType': 9, + } + } + ]], + "internalId": "stable_2", } ], ['2', { "type": "object", - "internalId": internal_id_2 + "internalId": "stable_2", }], [ '3', { @@ -438,16 +448,18 @@ async def test_serialization_internal_id(websocket, context_id): }, { "type": "number", "value": 2 + }, { + 'internalId': 'stable_1', + 'type': 'node', }], - "internalId": internal_id_3 + "internalId": "stable_3", } ], ['4', { "type": "array", - "internalId": internal_id_3 - }], - ["self", { + "internalId": "stable_3", + }], ["self", { "type": "object", - "internalId": internal_id_1 + "internalId": "stable_4", }]], }