Skip to content
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

Temp check #13

Open
wants to merge 3 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
241 changes: 229 additions & 12 deletions nightwatch/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,27 @@ 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 {debug} = require('../src/utils/helper');
const util = require('util');

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 _testsData = {};
const _testCasesData = {};

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 All @@ -35,7 +50,7 @@ module.exports = {
promises.push(testObservability.processTestReportFile(JSON.parse(JSON.stringify(modulesWithEnv[testSetting][testFile]))));
}
}

await Promise.all(promises);
done();
} catch (error) {
Expand All @@ -45,10 +60,211 @@ 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 {
_testCasesData[args.envelope.id] = {
...args.envelope
};
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;
_tests['uniqueId'] = testCaseId;

const testMetaData = {
uuid: uniqueId,
startedAt: new Date().toISOString(),
sentCBTInfo: !!reportData.sessionCapbailities
};
if (pickleData) {
testMetaData.scenario = {
name: pickleData.name
};
}

if (gherkinDocument && featureData) {
testMetaData.feature = {
path: gherkinDocument.uri,
name: featureData.name,
description: featureData.description
};
}
// _tests[uniqueId] = testMetaData;
_tests[testCaseId] = 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 testCaseId = _testCasesData[args.envelope.testCaseStartedId].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 uniqueId = _tests['uniqueId'];
const testMetaData = _tests[testCaseId];
if (testMetaData) {
delete _tests[testCaseId];
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; // openurl
const testCaseId = _testCasesData[args.envelope.testCaseStartedId].testCaseId;
debug(` ${process.pid} [TestStepStarted] testcaseid is ${testCaseId}`);
const pickleId = reportData.testCases.find((testCase) => testCase.id === testCaseId).pickleId;
const pickleData = reportData.pickle.find((pickle) => pickle.id === pickleId);
debug(` ${process.pid} [TestStepStarted] pickle id is ${pickleId} pickle data is ${util.inspect(pickleData)}`);

const testSteps = reportData.testCases.find((testCase) => testCase.id === testCaseId).testSteps;
const testStepId = reportData.testStepStarted.testStepId;
debug(` ${process.pid} [TestStepStarted] teststepid is ${testStepId}`);
debug(` ${process.pid} [TestStepStarted] teststeps are ${util.inspect(testSteps)}`);

const pickleStepId = testSteps.find((testStep) => testStep.id === testStepId).pickleStepId;
if (pickleStepId && _tests['testStepId'] !== testStepId) {
// const uniqueId = _tests[''];
_tests['testStepId'] = testStepId;
const pickleStepData = pickleData.steps.find((pickle) => pickle.id === pickleStepId);
const testMetaData = _tests[testCaseId] || {steps: []};
if (testMetaData && !testMetaData.steps) {
testMetaData.steps = [];
}
testMetaData.steps?.push({
id: pickleStepData.id,
text: pickleStepData.text,
started_at: new Date().toISOString()
});
_tests[testCaseId] = 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 testCaseId = args.envelope.testCaseStartedId;
const testCaseId = _testCasesData[args.envelope.testCaseStartedId].testCaseId;

// const testCaseId = args.envelope.testCaseId;
debug(` ${process.pid} [TestStepFinished] testcaseid is ${testCaseId}`);

const testStepFinished = reportData.testStepFinished;
const pickleId = reportData.testCases.find((testCase) => testCase.id === testCaseId).pickleId;
const pickleData = reportData.pickle.find((pickle) => pickle.id === pickleId);
debug(` ${process.pid} [TestStepFinished] pickle id is ${pickleId} pickle data is ${util.inspect(pickleData)}`);

const testSteps = reportData.testCases.find((testCase) => testCase.id === testCaseId).testSteps;
const testStepId = reportData.testStepFinished.testStepId;
debug(` ${process.pid} [TestStepFinished] teststepid is ${testStepId}`);
debug(` ${process.pid} [TestStepFinished] teststeps are ${util.inspect(testSteps)}`);

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[testCaseId] || {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[testCaseId] = 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], testMetaData.uuid);
}
}
}
}
// } 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 +289,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 @@ -81,7 +298,7 @@ module.exports = {
const specs = process.env.BROWSERSTACK_RERUN_TESTS.split(',');
await helper.handleNightwatchRerun(specs);
}
}
}
} catch (error) {
Logger.error(`Could not configure or launch test observability - ${error}`);
}
Expand All @@ -91,19 +308,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