Skip to content

Commit

Permalink
Integrate the support for sessionId into send()
Browse files Browse the repository at this point in the history
This closes #439 (feature request) and closes #441 as it provides a
more convenient implementation.
  • Loading branch information
cyrus-and committed Mar 15, 2021
1 parent 44f4012 commit 082ac9d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -770,14 +770,16 @@ 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.

`method` is a string describing the command.

`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:

Expand All @@ -803,12 +805,12 @@ For example:
client.send('Page.navigate', {url: 'https://github.com'}, console.log);
```

#### client.`<domain>`.`<method>`([params], [callback])
#### client.`<domain>`.`<method>`([params], [sessionId], [callback])

Just a shorthand for:

```js
client.send('<domain>.<method>', params, callback);
client.send('<domain>.<method>', params, sessionId, callback);
```

For example:
Expand Down
4 changes: 2 additions & 2 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 13 additions & 10 deletions lib/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down
22 changes: 22 additions & 0 deletions test/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit 082ac9d

Please sign in to comment.