From c2684788ee7f42303dd241b907cf4690023a6194 Mon Sep 17 00:00:00 2001 From: Ravi Sawlani Date: Wed, 1 Feb 2023 14:05:49 +0530 Subject: [PATCH 1/4] reset cdp connection on session end --- lib/transport/selenium-webdriver/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/transport/selenium-webdriver/index.js b/lib/transport/selenium-webdriver/index.js index a039c2e3fd..cb388dba48 100644 --- a/lib/transport/selenium-webdriver/index.js +++ b/lib/transport/selenium-webdriver/index.js @@ -7,6 +7,7 @@ const {Logger, isObject, IosSessionNotCreatedError, AndroidConnectionError} = re const httpClient = require('./httpclient.js'); const Session = require('./session.js'); const BaseTransport = require('../'); +const CDP = require('./cdp'); const {colors} = Logger; const {isErrorResponse, checkLegacyResponse, throwDecodedError, WebDriverError} = error; @@ -187,7 +188,7 @@ class Transport extends BaseTransport { async sessionFinished(reason) { this.emit('session:finished', reason); - + CDP.resetConnection(); await this.closeDriver(); } From 064d98a72d825e3dc4167a089c51f1f9b1873350 Mon Sep 17 00:00:00 2001 From: Ravi Sawlani Date: Wed, 1 Feb 2023 15:07:35 +0530 Subject: [PATCH 2/4] added api-demo test for cdp connection --- lib/transport/selenium-webdriver/index.js | 2 +- test/apidemos/cdp/registerAuth.js | 6 ++++++ test/apidemos/cdp/registerAuth2.js | 6 ++++++ test/lib/command-mocks.js | 2 +- 4 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 test/apidemos/cdp/registerAuth.js create mode 100644 test/apidemos/cdp/registerAuth2.js diff --git a/lib/transport/selenium-webdriver/index.js b/lib/transport/selenium-webdriver/index.js index cb388dba48..fac23ca2f1 100644 --- a/lib/transport/selenium-webdriver/index.js +++ b/lib/transport/selenium-webdriver/index.js @@ -7,7 +7,7 @@ const {Logger, isObject, IosSessionNotCreatedError, AndroidConnectionError} = re const httpClient = require('./httpclient.js'); const Session = require('./session.js'); const BaseTransport = require('../'); -const CDP = require('./cdp'); +const CDP = require('./cdp.js'); const {colors} = Logger; const {isErrorResponse, checkLegacyResponse, throwDecodedError, WebDriverError} = error; diff --git a/test/apidemos/cdp/registerAuth.js b/test/apidemos/cdp/registerAuth.js new file mode 100644 index 0000000000..b2d096ac4b --- /dev/null +++ b/test/apidemos/cdp/registerAuth.js @@ -0,0 +1,6 @@ +describe('cdp tests', function() { + it('register basic auth', function() { + browser.registerBasicAuth('admin', 'admin') + .navigateTo('http://localhost'); + }); +}); \ No newline at end of file diff --git a/test/apidemos/cdp/registerAuth2.js b/test/apidemos/cdp/registerAuth2.js new file mode 100644 index 0000000000..b2d096ac4b --- /dev/null +++ b/test/apidemos/cdp/registerAuth2.js @@ -0,0 +1,6 @@ +describe('cdp tests', function() { + it('register basic auth', function() { + browser.registerBasicAuth('admin', 'admin') + .navigateTo('http://localhost'); + }); +}); \ No newline at end of file diff --git a/test/lib/command-mocks.js b/test/lib/command-mocks.js index 6ec31edc30..57ba27081f 100644 --- a/test/lib/command-mocks.js +++ b/test/lib/command-mocks.js @@ -448,7 +448,7 @@ module.exports = { sessionId, capabilities: { acceptInsecureCerts: false, - browserName: 'firefox', + browserName, browserVersion: '65.0.1' } } From 297234c60e1f9aecc23ba41b962b0254740716e2 Mon Sep 17 00:00:00 2001 From: Ravi Sawlani Date: Wed, 1 Feb 2023 19:04:12 +0530 Subject: [PATCH 3/4] added test --- test/src/apidemos/cdp/testCdpCommands.js | 81 ++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 test/src/apidemos/cdp/testCdpCommands.js diff --git a/test/src/apidemos/cdp/testCdpCommands.js b/test/src/apidemos/cdp/testCdpCommands.js new file mode 100644 index 0000000000..90e45ca6d1 --- /dev/null +++ b/test/src/apidemos/cdp/testCdpCommands.js @@ -0,0 +1,81 @@ +const path = require('path'); +const assert = require('assert'); +const MockServer = require('../../../lib/mockserver.js'); +const Mocks = require('../../../lib/command-mocks'); + +const mockery = require('mockery'); +const common = require('../../../common.js'); +const {settings} = common; +const NightwatchClient = common.require('index.js'); + +describe('cdp commands test', function() { + beforeEach(function(done) { + mockery.enable({useCleanCache: true, warnOnReplace: false, warnOnUnregistered: false}); + this.server = MockServer.init(); + this.server.on('listening', () => { + done(); + }); + }); + + afterEach(function(done) { + mockery.deregisterAll(); + mockery.resetCache(); + mockery.disable(); + this.server.close(function() { + done(); + }); + }); + + it('reset cdp connection after each session', function() { + const testsPath = [path.join(__dirname, '../../../apidemos/cdp')]; + let resetConnectionCalled = false; + + mockery.registerMock('./cdp.js', { + getConnection: function(...args) { + return Promise.resolve(); + }, + resetConnection: function() { + resetConnectionCalled = true; + } + }); + + Mocks + .createNewW3CSession({ + persist: true, + browserName: 'chrome', + postdata: { + capabilities: {firstMatch: [{}], alwaysMatch: {browserName: 'chrome', 'goog:chromeOptions': {}}} + } + }) + .navigateTo({url: 'http://localhost', persist: true}); + + + + const globals = { + calls: 0, + waitForConditionPollInterval: 50, + waitForConditionTimeout: 120, + retryAssertionTimeout: 1000, + + + reporter(results) { + assert.strictEqual(resetConnectionCalled, true); + if (results.lastError) { + throw results.lastError; + } + } + }; + + return NightwatchClient.runTests(testsPath, settings({ + desiredCapabilities: { + browserName: 'chrome' + }, + selenium_host: null, + output: false, + skip_testcases_on_fail: false, + globals + })); + }); + + +}); From 0b66f7de48d99598861aeb20377381172369979e Mon Sep 17 00:00:00 2001 From: Ravi Sawlani Date: Wed, 1 Feb 2023 19:08:11 +0530 Subject: [PATCH 4/4] avoid closing of session according to end_session_on_fail --- lib/runner/test-runners/default.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/runner/test-runners/default.js b/lib/runner/test-runners/default.js index 5d9289bc71..2b0f3f17bd 100644 --- a/lib/runner/test-runners/default.js +++ b/lib/runner/test-runners/default.js @@ -48,7 +48,9 @@ class DefaultRunner { if (this.client && this.client.sessionId && this.client.startSessionEnabled) { Logger.info(`Attempting to close session ${this.client.sessionId}...`); - return this.currentSuite.terminate(null, null, true); + const failures = !this.currentSuite.reporter.allTestsPassed; + + return this.currentSuite.terminate(failures ? 'FAILED' : '', null, true); } return Promise.resolve();