| 11 | let uniqueTargetId = 0; |
| 12 | |
| 13 | export function createTarget({ |
| 14 | id, |
| 15 | name, |
| 16 | type = SDK.Target.Type.FRAME, |
| 17 | parentTarget, |
| 18 | subtype, |
| 19 | url = 'http://example.com', |
| 20 | connection, |
| 21 | targetManager = SDK.TargetManager.TargetManager.instance(), |
| 22 | }: { |
| 23 | id?: Protocol.Target.TargetID, |
| 24 | name?: string, |
| 25 | type?: SDK.Target.Type, |
| 26 | parentTarget?: SDK.Target.Target, |
| 27 | subtype?: string, |
| 28 | url?: string, |
| 29 | connection?: ProtocolClient.CDPConnection.CDPConnection, |
| 30 | targetManager?: SDK.TargetManager.TargetManager, |
| 31 | } = {}) { |
| 32 | if (!id) { |
| 33 | if (!uniqueTargetId++) { |
| 34 | id = 'test' as Protocol.Target.TargetID; |
| 35 | } else { |
| 36 | id = ('test' + uniqueTargetId) as Protocol.Target.TargetID; |
| 37 | } |
| 38 | } |
| 39 | if (!ProtocolClient.ConnectionTransport.ConnectionTransport.getFactory()) { |
| 40 | // We are not running with `describeWithMockConnection` so create a fresh mock connection. |
| 41 | // Because child targets inherit the SessionRouter/CDPConnection from the parent, we'll throw if a |
| 42 | // connection is passed together with a parent target as it would have no effect. |
| 43 | if (parentTarget && connection) { |
| 44 | throw new Error( |
| 45 | 'Can\'t create child targets with it\'s own connection. Child targets share the connection with their parent.'); |
| 46 | } |
| 47 | if (!connection && !parentTarget) { |
| 48 | connection = new MockCDPConnection([]); |
| 49 | } |
| 50 | } |
| 51 | return targetManager.createTarget( |
| 52 | id, name ?? id, type, parentTarget ? parentTarget : null, /* sessionId=*/ parentTarget ? id : undefined, |
| 53 | /* suspended=*/ false, connection, {targetId: id, url, subtype} as Protocol.Target.TargetInfo); |
| 54 | } |