diff --git a/README.md b/README.md index d36074d..a3f44d4 100644 --- a/README.md +++ b/README.md @@ -770,7 +770,7 @@ Emitted when the instance closes the WebSocket connection. This may happen for example when the user opens DevTools or when the tab is closed. -#### client.send(method, [params], [callback]) +#### client.send(method, [params], [sessionId], [callback]) Issue a command to the remote instance. @@ -778,6 +778,8 @@ Issue a command to the remote instance. `params` is an object containing the payload. +`sessionId` is a string representing the session identifier. + `callback` is executed when the remote instance sends a response to this command, it gets the following arguments: @@ -803,12 +805,12 @@ For example: client.send('Page.navigate', {url: 'https://github.com'}, console.log); ``` -#### client.``.``([params], [callback]) +#### client.``.``([params], [sessionId], [callback]) Just a shorthand for: ```js -client.send('.', params, callback); +client.send('.', params, sessionId, callback); ``` For example: diff --git a/lib/api.js b/lib/api.js index ba828fe..b1dd2a2 100644 --- a/lib/api.js +++ b/lib/api.js @@ -28,8 +28,8 @@ function decorate(to, category, object) { } function addCommand(chrome, domainName, command) { - const handler = (params, callback) => { - return chrome.send(`${domainName}.${command.name}`, params, callback); + const handler = (params, sessionId, callback) => { + return chrome.send(`${domainName}.${command.name}`, params, sessionId, callback); }; decorate(handler, 'command', command); chrome[domainName][command.name] = handler; diff --git a/lib/chrome.js b/lib/chrome.js index 4afaf90..9895a88 100644 --- a/lib/chrome.js +++ b/lib/chrome.js @@ -73,20 +73,21 @@ class Chrome extends EventEmitter { return util.inspect(this, options); } - send(method, params, callback) { - if (typeof params === 'function') { - callback = params; - params = undefined; - } + send(method, params, sessionId, callback) { + // handle optional arguments + const optionals = Array.from(arguments).slice(1); + params = optionals.find(x => typeof x === 'object'); + sessionId = optionals.find(x => typeof x === 'string'); + callback = optionals.find(x => typeof x === 'function'); // return a promise when a callback is not provided if (typeof callback === 'function') { - this._enqueueCommand(method, params, callback); + this._enqueueCommand(method, params, sessionId, callback); return undefined; } else { return new Promise((fulfill, reject) => { - this._enqueueCommand(method, params, (error, response) => { + this._enqueueCommand(method, params, sessionId, (error, response) => { if (error) { - const request = {method, params}; + const request = {method, params, sessionId}; reject( error instanceof Error ? error // low-level WebSocket error @@ -272,10 +273,12 @@ class Chrome extends EventEmitter { } // send a command to the remote endpoint and register a callback for the reply - _enqueueCommand(method, params, callback) { + _enqueueCommand(method, params, sessionId, callback) { const id = this._nextCommandId++; const message = { - id, method, + id, + method, + sessionId, params: params || {} }; this._ws.send(JSON.stringify(message), (err) => { diff --git a/test/send.js b/test/send.js index 39b5380..f63921a 100644 --- a/test/send.js +++ b/test/send.js @@ -140,4 +140,26 @@ describe('sending a command', () => { }); }); }); + describe('passing a sessionId', () => { + it('should interact with the correct target', async () => { + // fetch and connect to the browser target + const version = await Chrome.Version(); + const chrome = await Chrome({ + target: version.webSocketDebuggerUrl + }); + // attach to the target + const {targetInfos} = await chrome.Target.getTargets(); + const {sessionId} = await chrome.Target.attachToTarget({ + targetId: targetInfos[0].targetId, + flatten: true + }); + // send a command using the sessionId + const info = await chrome.Target.getTargetInfo(sessionId); + assert(info); + assert(info.targetInfo); + assert.equal(info.targetInfo.type, 'page'); + assert.equal(info.targetInfo.attached, true); + return chrome.close(); + }); + }); });