Skip to content

Test Observability support for nightwatch-cucumber #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"eslint:recommended"
],
"parserOptions": {
"ecmaVersion": 2020,
"ecmaVersion": 2022,
"sourceType": "module",
"ecmaFeatures": {
"jsx": false
Expand Down Expand Up @@ -46,6 +46,7 @@
"no-extra-semi": 0,
"comma-dangle": 2,
"no-underscore-dangle": 0,
"no-global-assign": 0,
"no-lone-blocks": 0,
"array-bracket-spacing": 2,
"object-curly-spacing": 2,
Expand Down
205 changes: 195 additions & 10 deletions nightwatch/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ const {CUSTOM_REPORTER_CALLBACK_TIMEOUT} = require('../src/utils/constants');
const CrashReporter = require('../src/utils/crashReporter');
const helper = require('../src/utils/helper');
const Logger = require('../src/utils/logger');
const {v4: uuidv4} = require('uuid');

const localTunnel = new LocalTunnel();
const testObservability = new TestObservability();

const nightwatchRerun = process.env.NIGHTWATCH_RERUN_FAILED;
const nightwatchRerunFile = process.env.NIGHTWATCH_RERUN_REPORT_FILE;
const _tests = {};

const registerListeners = () => {
process.removeAllListeners(`bs:addLog:${process.pid}`);
process.on(`bs:addLog:${process.pid}`, sendTestLog);
};

const sendTestLog = (log) => {
testObservability.appendTestItemLog(log, _tests['uniqueId']);
};

module.exports = {

Expand Down Expand Up @@ -45,10 +56,183 @@ module.exports = {
done(results);
},

onEvent({eventName, hook_type, ...args}) {
if (typeof browser !== 'undefined' && eventName === 'TestRunStarted') {
browser.execute(`browserstack_executor: {"action": "annotate", "arguments": {"type":"Annotation","data":"ObservabilitySync:${Date.now()}","level": "debug"}}`);
}
registerEventHandlers(eventBroadcaster) {

eventBroadcaster.on('TestFinished', (args) => {
if (!helper.isTestObservabilitySession()) {
return;
}
try {
if (typeof browser !== 'undefined') {
browser.execute(`browserstack_executor: {"action": "annotate", "arguments": {"type":"Annotation","data":"ObservabilitySync:${Date.now()}","level": "debug"}}`);
}
} catch (error) {
CrashReporter.uploadCrashReport(error.message, error.stack);
Logger.error(`Something went wrong in processing report file for test observability - ${error.message} with stacktrace ${error.stack}`);
}
});

eventBroadcaster.on('TestCaseStarted', async (args) => {
if (!helper.isTestObservabilitySession()) {
return;
}
try {
const reportData = args.report;
const testCaseId = reportData.testCaseStarted.testCaseId;
const pickleId = reportData.testCases.find((testCase) => testCase.id === testCaseId).pickleId;
const pickleData = reportData.pickle.find((pickle) => pickle.id === pickleId);
const gherkinDocument = reportData?.gherkinDocument.find((document) => document.uri === pickleData.uri);
const featureData = gherkinDocument.feature;
const uniqueId = uuidv4();
_tests['uniqueId'] = uniqueId;
const testMetaData = {
uuid: uniqueId,
startedAt: new Date().toISOString()
};
if (pickleData) {
testMetaData.scenario = {
name: pickleData.name
};
}

if (gherkinDocument && featureData) {
testMetaData.feature = {
path: gherkinDocument.uri,
name: featureData.name,
description: featureData.description
};
}
_tests[uniqueId] = testMetaData;
await testObservability.sendTestRunEventForCucumber(reportData, gherkinDocument, pickleData, 'TestRunStarted', testMetaData);
} catch (error) {
CrashReporter.uploadCrashReport(error.message, error.stack);
Logger.error(`Something went wrong in processing report file for test observability - ${error.message} with stacktrace ${error.stack}`);
}
});

eventBroadcaster.on('TestCaseFinished', async (args) => {
if (!helper.isTestObservabilitySession()) {
return;
}
try {
const reportData = args.report;
const testCaseId = reportData.testCaseStarted.testCaseId;
const pickleId = reportData.testCases.find((testCase) => testCase.id === testCaseId).pickleId;
const pickleData = reportData.pickle.find((pickle) => pickle.id === pickleId);
const gherkinDocument = reportData?.gherkinDocument.find((document) => document.uri === pickleData.uri);
const uniqueId = _tests['uniqueId'];
const testMetaData = _tests[uniqueId];
if (testMetaData) {
delete _tests[uniqueId];
testMetaData.finishedAt = new Date().toISOString();
await testObservability.sendTestRunEventForCucumber(reportData, gherkinDocument, pickleData, 'TestRunFinished', testMetaData);
}
} catch (error) {
CrashReporter.uploadCrashReport(error.message, error.stack);
Logger.error(`Something went wrong in processing report file for test observability - ${error.message} with stacktrace ${error.stack}`);
}
});

eventBroadcaster.on('TestStepStarted', (args) => {
if (!helper.isTestObservabilitySession()) {
return;
}
try {
const reportData = args.report;
const testCaseId = reportData.testCaseStarted.testCaseId;
const pickleId = reportData.testCases.find((testCase) => testCase.id === testCaseId).pickleId;
const pickleData = reportData.pickle.find((pickle) => pickle.id === pickleId);
const testSteps = reportData.testCases.find((testCase) => testCase.id === testCaseId).testSteps;
const testStepId = reportData.testStepStarted.testStepId;
const pickleStepId = testSteps.find((testStep) => testStep.id === testStepId).pickleStepId;
if (pickleStepId && _tests['testStepId'] !== testStepId) {
const uniqueId = _tests['uniqueId'];
_tests['testStepId'] = testStepId;
const pickleStepData = pickleData.steps.find((pickle) => pickle.id === pickleStepId);
const testMetaData = _tests[uniqueId] || {steps: []};
if (testMetaData && !testMetaData.steps) {
testMetaData.steps = [];
}
testMetaData.steps?.push({
id: pickleStepData.id,
text: pickleStepData.text,
started_at: new Date().toISOString()
});
_tests[uniqueId] = testMetaData;
}
} catch (error) {
CrashReporter.uploadCrashReport(error.message, error.stack);
Logger.error(`Something went wrong in processing report file for test observability - ${error.message} with stacktrace ${error.stack}`);
}
});

eventBroadcaster.on('TestStepFinished', async (args) => {
if (!helper.isTestObservabilitySession()) {
return;
}
try {
const reportData = args.report;
const testCaseId = reportData.testCaseStarted.testCaseId;
const testStepFinished = reportData.testStepFinished;
const pickleId = reportData.testCases.find((testCase) => testCase.id === testCaseId).pickleId;
const pickleData = reportData.pickle.find((pickle) => pickle.id === pickleId);
const testSteps = reportData.testCases.find((testCase) => testCase.id === testCaseId).testSteps;
const testStepId = reportData.testStepFinished.testStepId;
const pickleStepId = testSteps.find((testStep) => testStep.id === testStepId).pickleStepId;
if (pickleStepId && _tests['testStepId']) {
const uniqueId = _tests['uniqueId'];
const pickleStepData = pickleData.steps.find((pickle) => pickle.id === pickleStepId);
const testMetaData = _tests[uniqueId] || {steps: []};
if (!testMetaData.steps) {
testMetaData.steps = [{
id: pickleStepData.id,
text: pickleStepData.text,
finished_at: new Date().toISOString(),
result: testStepFinished.testStepResult?.status,
duration: testStepFinished.testStepResult?.duration?.seconds,
failure: testStepFinished.testStepResult?.exception?.message,
failureType: testStepFinished.testStepResult?.exception?.type
}];
} else {
testMetaData.steps.forEach((step) => {
if (step.id === pickleStepData.id) {
step.finished_at = new Date().toISOString();
step.result = testStepFinished.testStepResult?.status;
step.duration = testStepFinished.testStepResult?.duration?.seconds;
step.failure = testStepFinished.testStepResult?.exception?.message;
step.failureType = testStepFinished.testStepResult?.exception?.type;
}
});
}
_tests[uniqueId] = testMetaData;
delete _tests['testStepId'];
if (testStepFinished.httpOutput && testStepFinished.httpOutput.length > 0) {
for (const [index, output] of testStepFinished.httpOutput.entries()) {
if (index % 2 === 0) {
await testObservability.createHttpLogEvent(output, testStepFinished.httpOutput[index + 1], uniqueId);
}
}
}
}
} catch (error) {
CrashReporter.uploadCrashReport(error.message, error.stack);
Logger.error(`Something went wrong in processing report file for test observability - ${error.message} with stacktrace ${error.stack}`);
}
});

eventBroadcaster.on('ScreenshotCreated', async (args) => {
if (!helper.isTestObservabilitySession()) {
return;
}
try {
if (args.path && _tests['uniqueId']) {
await testObservability.createScreenshotLogEvent(_tests['uniqueId'], args.path, Date.now());
}
} catch (error) {
CrashReporter.uploadCrashReport(error.message, error.stack);
Logger.error(`Something went wrong in processing screenshot for test observability - ${error.message} with stacktrace ${error.stack}`);
}
});
},

async before(settings) {
Expand All @@ -73,6 +257,7 @@ module.exports = {
try {
testObservability.configure(settings);
if (helper.isTestObservabilitySession()) {
registerListeners();
settings.globals['customReporterCallbackTimeout'] = CUSTOM_REPORTER_CALLBACK_TIMEOUT;
if (testObservability._user && testObservability._key) {
await testObservability.launchTestSession();
Expand All @@ -91,19 +276,19 @@ module.exports = {
async after() {
localTunnel.stop();
if (helper.isTestObservabilitySession()) {
process.env.NIGHTWATCH_RERUN_FAILED = nightwatchRerun;
process.env.NIGHTWATCH_RERUN_REPORT_FILE = nightwatchRerunFile;
if (process.env.BROWSERSTACK_RERUN === 'true' && process.env.BROWSERSTACK_RERUN_TESTS) {
await helper.deleteRerunFile();
}
try {
await testObservability.stopBuildUpstream();
if (process.env.BS_TESTOPS_BUILD_HASHED_ID) {
Logger.info(`\nVisit https://observability.browserstack.com/builds/${process.env.BS_TESTOPS_BUILD_HASHED_ID} to view build report, insights, and many more debugging information all at one place!\n`);
}
await testObservability.stopBuildUpstream();
} catch (error) {
Logger.error(`Something went wrong in stopping build session for test observability - ${error}`);
}
process.env.NIGHTWATCH_RERUN_FAILED = nightwatchRerun;
process.env.NIGHTWATCH_RERUN_REPORT_FILE = nightwatchRerunFile;
if (process.env.BROWSERSTACK_RERUN === 'true' && process.env.BROWSERSTACK_RERUN_TESTS) {
await helper.deleteRerunFile();
}
}
},

Expand Down
Loading