Skip to content

Commit

Permalink
Fix sync tests getting skipped in parallel mode. (#3791)
Browse files Browse the repository at this point in the history
  • Loading branch information
garg3133 committed Jul 18, 2023
1 parent 943dcfa commit 0088762
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 15 deletions.
5 changes: 3 additions & 2 deletions lib/reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions lib/runner/concurrency/worker-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions lib/testsuite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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, '_');
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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()
});
}));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
});
});
6 changes: 3 additions & 3 deletions test/extra/commands/other/otherCommand.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
command: function() {
return this;
module.exports = class OtherCommand {
command() {
return this.api.pause(10);
}
};
20 changes: 20 additions & 0 deletions test/extra/parallelism-customCommandsSync.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
49 changes: 49 additions & 0 deletions test/src/apidemos/custom-commands/testCustomCommandSync.js
Original file line number Diff line number Diff line change
@@ -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
}));
});
});

0 comments on commit 0088762

Please sign in to comment.