From 0088762e1de30c45fadcc35aab96836ae6cecaa5 Mon Sep 17 00:00:00 2001 From: Priyansh Garg <39924567+garg3133@users.noreply.github.com> Date: Wed, 19 Jul 2023 00:15:27 +0530 Subject: [PATCH] Fix sync tests getting skipped in parallel mode. (#3791) --- lib/reporter/index.js | 5 +- lib/runner/concurrency/worker-task.js | 8 +++ lib/testsuite/index.js | 20 ++++---- .../testUsingES6AsyncCustomCommands.js | 11 +++++ test/extra/commands/other/otherCommand.js | 6 +-- .../extra/parallelism-customCommandsSync.json | 20 ++++++++ .../custom-commands/testCustomCommandSync.js | 49 +++++++++++++++++++ 7 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 test/apidemos/custom-commands-parallel/testUsingES6AsyncCustomCommands.js create mode 100644 test/extra/parallelism-customCommandsSync.json create mode 100644 test/src/apidemos/custom-commands/testCustomCommandSync.js diff --git a/lib/reporter/index.js b/lib/reporter/index.js index 5ae14d97b9..3411961e63 100644 --- a/lib/reporter/index.js +++ b/lib/reporter/index.js @@ -195,10 +195,11 @@ class Reporter extends SimplifiedReporter { result.status === -1 ); - // Use only necessary values + // Use only necessary values if (result) { const {status, message, showDiff, name, abortOnFailure, stack, beautifiedStack} = result; - commandResult ={ + + commandResult = { status, message, showDiff, diff --git a/lib/runner/concurrency/worker-task.js b/lib/runner/concurrency/worker-task.js index 427a99c641..9041b91b84 100644 --- a/lib/runner/concurrency/worker-task.js +++ b/lib/runner/concurrency/worker-task.js @@ -2,6 +2,7 @@ const boxen = require('boxen'); const {MessageChannel} = require('worker_threads'); const {Logger, symbols} = require('../../utils'); const EventEmitter = require('events'); +const {SafeJSON, isString} = require('../../utils'); let prevIndex = 0; @@ -72,6 +73,13 @@ class WorkerTask extends EventEmitter { const {port1, port2} = new MessageChannel(); port2.onmessage = ({data: result}) => { + if (isString(result)) { + try { + result = JSON.parse(result); + // eslint-disable-next-line no-empty + } catch (e) {} + } + switch (result.type) { case 'testsuite_finished': result.itemKey = this.label, diff --git a/lib/testsuite/index.js b/lib/testsuite/index.js index 20d119d4b2..e8e7c85a65 100644 --- a/lib/testsuite/index.js +++ b/lib/testsuite/index.js @@ -58,13 +58,13 @@ class TestSuite { } get skipTestcasesOnFail() { - let localDefinedValue = this.context.getSkipTestcasesOnFail(); + const localDefinedValue = this.context.getSkipTestcasesOnFail(); if (localDefinedValue !== undefined) { return localDefinedValue; } - let settingsValueUndefined = this.settings.skip_testcases_on_fail === undefined; + const settingsValueUndefined = this.settings.skip_testcases_on_fail === undefined; if (settingsValueUndefined && this.context.unitTestingMode) { // false by default when running unit tests return false; @@ -75,7 +75,7 @@ class TestSuite { } get endSessionOnFail() { - let definedValue = this.context.getEndSessionOnFail(); + const definedValue = this.context.getEndSessionOnFail(); return definedValue === undefined ? this.settings.end_session_on_fail : definedValue; } @@ -399,10 +399,10 @@ class TestSuite { return this; } - let capabilities = data.capabilities || {}; - let browserName = (capabilities.browserName && capabilities.browserName.toUpperCase()) || ''; - let browserVersion = capabilities.version || capabilities.browserVersion || ''; - let platformVersion = capabilities.platform || capabilities.platformVersion || ''; + const capabilities = data.capabilities || {}; + const browserName = (capabilities.browserName && capabilities.browserName.toUpperCase()) || ''; + const browserVersion = capabilities.version || capabilities.browserVersion || ''; + const platformVersion = capabilities.platform || capabilities.platformVersion || ''; if (!this.context.unitTestingMode) { this.settings.report_prefix = this.__reportPrefix = `${browserName}_${browserVersion}_${platformVersion}_`.replace(/ /g, '_'); @@ -637,7 +637,7 @@ class TestSuite { return this.retrySuite(); } - let failures = errorOrFailures || failedResult || !this.reporter.allTestsPassed; + const failures = errorOrFailures || failedResult || !this.reporter.allTestsPassed; return this.stopSession(failures); }); @@ -668,11 +668,11 @@ class TestSuite { httpOutput: Logger.collectOutput() })); } else if (process.port && typeof process.port.postMessage === 'function') { - process.port.postMessage({ + process.port.postMessage(SafeJSON.stringify({ type: 'testsuite_finished', results: this.reporter.exportResults(), httpOutput: Logger.collectOutput() - }); + })); } } diff --git a/test/apidemos/custom-commands-parallel/testUsingES6AsyncCustomCommands.js b/test/apidemos/custom-commands-parallel/testUsingES6AsyncCustomCommands.js new file mode 100644 index 0000000000..3b60eefe69 --- /dev/null +++ b/test/apidemos/custom-commands-parallel/testUsingES6AsyncCustomCommands.js @@ -0,0 +1,11 @@ +describe('Test Using Sync Custom Command returning NightwatchAPI', function() { + before(browser => { + browser.url('http://localhost'); + }); + + it('sampleTest', browser => { + browser + .otherCommand() + .end(); + }); +}); diff --git a/test/extra/commands/other/otherCommand.js b/test/extra/commands/other/otherCommand.js index 1de68f562d..b49cef30e4 100644 --- a/test/extra/commands/other/otherCommand.js +++ b/test/extra/commands/other/otherCommand.js @@ -1,5 +1,5 @@ -module.exports = { - command: function() { - return this; +module.exports = class OtherCommand { + command() { + return this.api.pause(10); } }; diff --git a/test/extra/parallelism-customCommandsSync.json b/test/extra/parallelism-customCommandsSync.json new file mode 100644 index 0000000000..a80ae994a8 --- /dev/null +++ b/test/extra/parallelism-customCommandsSync.json @@ -0,0 +1,20 @@ +{ + "src_folders" : ["./test/apidemos/custom-commands-parallel"], + "test_workers": true, + "custom_commands_path": ["./test/extra/commands/other"], + "persist_globals": true, + "output_folder" : false, + "output" : false, + "silent": false, + "selenium" : { + "start_process" : false, + "port": 10195 + }, + "test_settings": { + "default" : { + "desiredCapabilities" : { + "browserName" : "firefox" + } + } + } +} diff --git a/test/src/apidemos/custom-commands/testCustomCommandSync.js b/test/src/apidemos/custom-commands/testCustomCommandSync.js new file mode 100644 index 0000000000..2b2208ff06 --- /dev/null +++ b/test/src/apidemos/custom-commands/testCustomCommandSync.js @@ -0,0 +1,49 @@ +const path = require('path'); +const assert = require('assert'); +const MockServer = require('../../../lib/mockserver.js'); +const Mocks = require('../../../lib/command-mocks.js'); +const common = require('../../../common.js'); +const {settings} = common; +const NightwatchClient = common.require('index.js'); + +describe('sync custom commands', function() { + beforeEach(function(done) { + this.server = MockServer.init(); + this.server.on('listening', () => { + done(); + }); + }); + + afterEach(function(done) { + this.server.close(function() { + done(); + }); + }); + + it('test sync custom command returning NightwatchAPI as result', function() { + const testsPath = path.join(__dirname, '../../../apidemos/custom-commands-parallel'); + Mocks.createNewW3CSession({ + testName: 'Test Sync Custom Commands returning NightwatchAPI' + }); + + const globals = { + waitForConditionPollInterval: 50, + waitForConditionTimeout: 120, + retryAssertionTimeout: 1000, + + reporter(results) { + assert.strictEqual(Object.keys(results.modules).length, 1); + if (results.lastError) { + throw results.lastError; + } + } + }; + + return NightwatchClient.runTests({ + env: 'default', + config: 'test/extra/parallelism-customCommandsSync.json' + }, Object.assign({}, { + globals + })); + }); +});