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

[QT - 265] Added some codes for monitoring the console and network logs in to th… #5952

Closed
wants to merge 11 commits into from
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ jobs:
working-directory: /opt/countly/ui-tests/cypress
run: |
ARTIFACT_ARCHIVE_NAME="$(date '+%Y%m%d-%H.%M')_${GITHUB_REPOSITORY#*/}_CI#${{ github.run_number }}.tar.gz"
mkdir -p screenshots videos
tar zcvf "$ARTIFACT_ARCHIVE_NAME" screenshots videos
mkdir -p screenshots videos logs
tar zcvf "$ARTIFACT_ARCHIVE_NAME" screenshots videos logs
curl -o /tmp/uploader.log -u "${{ secrets.BOX_UPLOAD_AUTH }}" ${{ secrets.BOX_UPLOAD_PATH }} -T "$ARTIFACT_ARCHIVE_NAME"

ui-test-onboarding:
Expand Down Expand Up @@ -389,6 +389,6 @@ jobs:
working-directory: /opt/countly/ui-tests/cypress
run: |
ARTIFACT_ARCHIVE_NAME="$(date '+%Y%m%d-%H.%M')_${GITHUB_REPOSITORY#*/}_CI#${{ github.run_number }}.tar.gz"
mkdir -p screenshots videos
tar zcvf "$ARTIFACT_ARCHIVE_NAME" screenshots videos
mkdir -p screenshots videos logs
tar zcvf "$ARTIFACT_ARCHIVE_NAME" screenshots videos logs
curl -o /tmp/uploader.log -u "${{ secrets.BOX_UPLOAD_AUTH }}" ${{ secrets.BOX_UPLOAD_PATH }} -T "$ARTIFACT_ARCHIVE_NAME"
55 changes: 54 additions & 1 deletion ui-tests/cypress.config.sample.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { defineConfig } = require("cypress");
const fs = require('fs');

const path = require('path');
module.exports = defineConfig({
e2e: {
baseUrl: "http://localhost",
Expand Down Expand Up @@ -40,6 +40,59 @@ module.exports = defineConfig({
}
return launchOptions;
});

on('task', {
saveLogsBySpecName({ specName, logData }) {
const logsDir = path.join(__dirname, 'logs', specName);
const logFilePath = path.join(logsDir, 'network-logs.json');

if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir, { recursive: true });
}

let existingLogs = [];
if (fs.existsSync(logFilePath)) {
const fileContent = fs.readFileSync(logFilePath, 'utf-8');
try {
existingLogs = JSON.parse(fileContent);
}
catch {
existingLogs = [];
}
}

existingLogs.push(logData);
fs.writeFileSync(logFilePath, JSON.stringify(existingLogs, null, 2));

return null;
},

saveConsoleLogs({ specName, logData }) {
const logsDir = path.join(__dirname, 'logs', specName);
const logFilePath = path.join(logsDir, 'console-logs.json');

if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir, { recursive: true });
}

let existingLogs = [];
if (fs.existsSync(logFilePath)) {
const fileContent = fs.readFileSync(logFilePath, 'utf-8');
try {
existingLogs = JSON.parse(fileContent);
}
catch {
existingLogs = [];
}
}

existingLogs.push(logData);
fs.writeFileSync(logFilePath, JSON.stringify(existingLogs, null, 2));

return null;
},
});

},
},
});
Expand Down
58 changes: 57 additions & 1 deletion ui-tests/cypress/support/commands.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,62 @@ Cypress.Commands.add("scrollDataTableToLeft", (element = '.el-table__body-wrappe
cy.get(element).eq(index).scrollTo('left', { ensureScrollable: false });
});


Cypress.Commands.add('saveConsoleAndNetworkLogs', () => {
const specName = Cypress.spec.name.replace('.cy.js', '');
const testName = Cypress.currentTest.title;

cy.intercept('**').as('allRequests');

cy.window().then((win) => {
const originalConsoleLog = win.console.log;
const originalConsoleWarn = win.console.warn;
const originalConsoleError = win.console.error;
const originalConsoleInfo = win.console.info;

const interceptConsole = (originalMethod) => {
return (...args) => {
const logData = {
testName: testName,
timestamp: new Date().toISOString(),
logData: args.map((arg) => arg.toString()).join(' '),
};

cy.task('saveConsoleLogs', { specName, logData });

originalMethod.apply(win.console, args);
};
};

win.console.log = interceptConsole('log', originalConsoleLog);
win.console.warn = interceptConsole('warn', originalConsoleWarn);
win.console.error = interceptConsole('error', originalConsoleError);
win.console.info = interceptConsole('info', originalConsoleInfo);
});

cy.wait('@allRequests').then((interception) => {
const logData = {
testName: testName,
timestamp: new Date().toISOString(),
logData: {
request: interception.request,
response: interception.response,
},
};

cy.task('saveLogsBySpecName', { specName, logData });
});

const additionalLogData = {
testName: testName,
timestamp: new Date().toISOString(),
logData: `${testName} Test executed successfully`,
status: 'passed',
};
cy.task('saveLogsBySpecName', { specName, logData: additionalLogData });
});


Cypress.Commands.add('verifyElement', ({
labelElement,
labelText,
Expand Down Expand Up @@ -345,4 +401,4 @@ Cypress.Commands.add('getElement', (selector, parent = null) => {
else {
return cy.get(selector);
}
});
});
3 changes: 2 additions & 1 deletion ui-tests/package.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"moment": "^2.29.4"
},
"devDependencies": {
"cypress": "^13.6.4"
"cypress": "^13.6.4",
"cypress-cdp": "^1.6.35"
}
}
Loading