Skip to content

Commit

Permalink
Fixed #3990 – CDP connection not getting reset across sessions when u…
Browse files Browse the repository at this point in the history
…sing Selenium. (#3994)
  • Loading branch information
garg3133 committed Jan 24, 2024
1 parent 98c4c44 commit 5670d57
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 15 deletions.
4 changes: 4 additions & 0 deletions lib/core/client.js
Expand Up @@ -13,6 +13,7 @@ const ElementGlobal = require('../api/_loaders/element-global.js');
const Factory = require('../transport/factory.js');
const {isAndroid, isIos} = require('../utils/mobile');
const namespacedApi = require('../core/namespaced-api.js');
const cdp = require('../transport/selenium-webdriver/cdp.js');

const {LocateStrategy, Locator} = Element;
const {Logger, isUndefined, isDefined, isObject, isFunction, isSafari, isChrome} = Utils;
Expand Down Expand Up @@ -757,6 +758,9 @@ class NightwatchClient extends EventEmitter {

Logger.info(`Received session with ID: ${data.sessionId}\n`);

// Reset cdp connection every time a new webdriver session is created.
cdp.resetConnection();

this.emit('nightwatch:session.create', data);

return data;
Expand Down
3 changes: 1 addition & 2 deletions lib/transport/selenium-webdriver/index.js
Expand Up @@ -8,7 +8,6 @@ const {IosSessionNotCreatedError, AndroidConnectionError} = require('../../utils
const httpClient = require('./httpclient.js');
const Session = require('./session.js');
const BaseTransport = require('../');
const CDP = require('./cdp.js');
const {colors} = Logger;
const {isErrorResponse, checkLegacyResponse, throwDecodedError, WebDriverError} = error;
const {IosSessionErrors} = require('../errors');
Expand Down Expand Up @@ -189,7 +188,7 @@ class Transport extends BaseTransport {

async sessionFinished(reason) {
this.emit('session:finished', reason);
CDP.resetConnection();

await this.closeDriver();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/transport/selenium-webdriver/method-mappings.js
Expand Up @@ -1062,7 +1062,7 @@ module.exports = class MethodMappings {
///////////////////////////////////////////////////////////////////////////

async registerAuth(username, password) {
const cdpConnection = await cdp.getConnection(this.driver);
const cdpConnection = await cdp.getConnection(this.driver, true);
await this.driver.register(username, password, cdpConnection);

return {
Expand Down
12 changes: 8 additions & 4 deletions test/lib/command-mocks.js
Expand Up @@ -453,7 +453,8 @@ module.exports = {
sessionId = '13521-10219-202',
headless = true,
deleteSession = true,
url = '/wd/hub/session'
url = '/wd/hub/session',
times = 0
}) {
const browserName = 'chrome';
const headlessOpt = headless ? 'headless=new' : '';
Expand All @@ -472,7 +473,7 @@ module.exports = {
postdata: JSON.stringify({
capabilities: {firstMatch: [{}], alwaysMatch: {browserName, ...options}}
}),

times,
response: JSON.stringify({
value: {
sessionId,
Expand All @@ -486,16 +487,19 @@ module.exports = {
}, !persist);

if (!deleteSession) {
return;
return this;
}

MockServer.addMock({
url: `/session/${sessionId}`,
url: `${url}/${sessionId}`,
method: 'DELETE',
times,
response: {
value: null
}
}, !persist);

return this;
},

createNewW3CSession({
Expand Down
74 changes: 66 additions & 8 deletions test/src/apidemos/cdp/testCdpCommands.js
Expand Up @@ -26,16 +26,16 @@ describe('cdp commands test', function() {
});
});

it('reset cdp connection after each session', function() {
it('reset cdp connection after each session is created', function() {
const testsPath = [path.join(__dirname, '../../../apidemos/cdp')];
let resetConnectionCalled = false;
let resetConnectionCalled = 0;

mockery.registerMock('./cdp.js', {
mockery.registerMock('../transport/selenium-webdriver/cdp.js', {
getConnection: function(...args) {
return Promise.resolve();
},
resetConnection: function() {
resetConnectionCalled = true;
resetConnectionCalled += 1;
}
});

Expand All @@ -49,17 +49,16 @@ describe('cdp commands test', function() {
})
.navigateTo({url: 'http://localhost', persist: true});



const globals = {
calls: 0,
waitForConditionPollInterval: 50,
waitForConditionTimeout: 120,
retryAssertionTimeout: 1000,


reporter(results) {
assert.strictEqual(resetConnectionCalled, true);
// cdp connection is reset once for each session (two test suites).
assert.strictEqual(resetConnectionCalled, 2);
if (results.lastError) {
throw results.lastError;
}
Expand All @@ -77,5 +76,64 @@ describe('cdp commands test', function() {
}));
});


it('reset cdp connection after each selenium session is created', function() {
const testsPath = [path.join(__dirname, '../../../apidemos/cdp')];
let resetConnectionCalled = 0;

Mocks
.createChromeSession({
headless: false,
times: 2
});

MockServer.addMock({
url: '/wd/hub/session/13521-10219-202/url',
method: 'POST',
postdata: JSON.stringify({
url: 'http://localhost'
}),
response: {
value: null
},
times: 2
});

mockery.registerMock('../transport/selenium-webdriver/cdp.js', {
getConnection: function(...args) {
return Promise.resolve();
},
resetConnection: function() {
resetConnectionCalled += 1;
}
});

const globals = {
calls: 0,
waitForConditionPollInterval: 50,
waitForConditionTimeout: 120,
retryAssertionTimeout: 1000,

reporter(results) {
// cdp connection is reset once for each session (two test suites).
assert.strictEqual(resetConnectionCalled, 2);
if (results.lastError) {
throw results.lastError;
}
}
};

return NightwatchClient.runTests(testsPath, settings({
desiredCapabilities: {
browserName: 'chrome'
},
selenium: {
host: 'localhost',
port: 10195,
start_process: false
},
output: false,
skip_testcases_on_fail: false,
globals
}));
});
});

0 comments on commit 5670d57

Please sign in to comment.