From ab83803c839a4f33764d5678a9c48dc2665567c7 Mon Sep 17 00:00:00 2001 From: Ravi Sawlani Date: Wed, 1 Feb 2023 19:08:11 +0530 Subject: [PATCH 1/3] 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(); From b0d3a33bd46648c28858401d565273ce1df5030c Mon Sep 17 00:00:00 2001 From: Ravi Sawlani Date: Sun, 5 Feb 2023 20:53:47 +0530 Subject: [PATCH 2/3] don't use the same session when end_session_on_fail flag is enabled --- lib/testsuite/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/testsuite/index.js b/lib/testsuite/index.js index 4c3197017f..243a04a9df 100644 --- a/lib/testsuite/index.js +++ b/lib/testsuite/index.js @@ -1,11 +1,13 @@ const AssertionError = require('assertion-error'); const {By, Key, locateWith, withTagName} = require('selenium-webdriver'); + const Reporter = require('../reporter'); const Context = require('./context.js'); const TestHooks = require('./hooks.js'); const TestCase = require('./testcase.js'); const Runnable = require('./runnable.js'); +const Transport = require('../transport/selenium-webdriver'); const NightwatchAssertError = require('../assertion').AssertionError; const SuiteRetries = require('./retries.js'); const NightwatchClient = require('../core/client.js'); @@ -662,6 +664,11 @@ class TestSuite { } if (!this.endSessionOnFail && reason === 'FAILED') { + + // Keep the session open; avoid reusing of same session + Transport.driver = null; + Transport.driverService = null; + return potentialError; } From 47e86c26216a1f904da23baccaa6e8e15672ec10 Mon Sep 17 00:00:00 2001 From: Ravi Sawlani Date: Fri, 10 Feb 2023 18:22:13 +0530 Subject: [PATCH 3/3] added test --- test/src/runner/testRunnerEndSessionOnFail.js | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 test/src/runner/testRunnerEndSessionOnFail.js diff --git a/test/src/runner/testRunnerEndSessionOnFail.js b/test/src/runner/testRunnerEndSessionOnFail.js new file mode 100644 index 0000000000..b036706397 --- /dev/null +++ b/test/src/runner/testRunnerEndSessionOnFail.js @@ -0,0 +1,56 @@ +const path = require('path'); +const assert = require('assert'); +const nock = require('nock'); +const nocks = require('../../lib/nocks'); +const common = require('../../common.js'); +const CommandGlobals = require('../../lib/globals/commands.js'); +const {settings} = common; +const {runTests} = common.require('index.js'); + +describe('test runner with end_session_on_fail flag', function() { + + before(function(done) { + nocks.enable().cleanAll(); + CommandGlobals.beforeEach.call(this, done); + + }); + + after(function(done) { + nocks.disable(); + CommandGlobals.afterEach.call(this, done); + }); + + + it('session is not ended with end_on_sessiion_fail: false', function() { + let endSessionCalled = false; + const testFile = path.join(__dirname, '../../sampletests/withfailures/sample.js'); + + nocks.createSession() + .url() + .elementNotFound('#weblogin'); + + + nock('http://localhost:10195') + .delete('/wd/hub/session/1352110219202') + .reply(204, ()=> { + endSessionCalled = true; + + return ''; + }); + + return runTests(testFile, settings({ + enable_fail_fast: true, + end_session_on_fail: false, + globals: { + waitForConditionPollInterval: 5, + waitForConditionTimeout: 5, + retryAssertionTimeout: 1 + } + })).catch(_ => { + assert.strictEqual(endSessionCalled, false); + }); + + + }); + +});