mirror of
https://github.com/AppFlowy-IO/AppFlowy-Web.git
synced 2025-11-30 11:27:55 +08:00
chore: page more action
This commit is contained in:
159
cypress/support/console-logger.ts
Normal file
159
cypress/support/console-logger.ts
Normal file
@@ -0,0 +1,159 @@
|
||||
/// <reference types="cypress" />
|
||||
|
||||
interface ConsoleLog {
|
||||
type: string;
|
||||
args: any[];
|
||||
timestamp: string;
|
||||
url?: string;
|
||||
}
|
||||
|
||||
// Store captured console logs globally
|
||||
let consoleLogs: ConsoleLog[] = [];
|
||||
let isCapturing = false;
|
||||
|
||||
// Helper to stringify arguments for better readability
|
||||
function stringifyArgs(args: any[]): string {
|
||||
return args.map(arg => {
|
||||
try {
|
||||
if (typeof arg === 'object' && arg !== null) {
|
||||
return JSON.stringify(arg, null, 2);
|
||||
}
|
||||
return String(arg);
|
||||
} catch (e) {
|
||||
return '[Object]';
|
||||
}
|
||||
}).join(' ');
|
||||
}
|
||||
|
||||
// Install console interceptors on window
|
||||
function installConsoleInterceptors(win: any) {
|
||||
const methods: (keyof Console)[] = ['log', 'error', 'warn'];
|
||||
|
||||
methods.forEach((method) => {
|
||||
const originalMethod = (win.console[method] as Function).bind(win.console);
|
||||
|
||||
// Override the console method
|
||||
win.console[method] = (...args: any[]) => {
|
||||
if (isCapturing) {
|
||||
// Store the log
|
||||
const logEntry: ConsoleLog = {
|
||||
type: method,
|
||||
args: args,
|
||||
timestamp: new Date().toISOString(),
|
||||
url: win.location.href
|
||||
};
|
||||
consoleLogs.push(logEntry);
|
||||
|
||||
// Immediately output to Node.js console for CI visibility
|
||||
const message = stringifyArgs(args);
|
||||
const logMessage = `[${new Date().toISOString()}] [CONSOLE.${method.toUpperCase()}] ${message}`;
|
||||
|
||||
// Log to Node.js console directly
|
||||
console.log(logMessage);
|
||||
}
|
||||
|
||||
// Call original method
|
||||
return originalMethod(...args);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// Start capturing console logs
|
||||
Cypress.Commands.add('startConsoleCapture', () => {
|
||||
consoleLogs = [];
|
||||
isCapturing = true;
|
||||
|
||||
// Install on current window
|
||||
cy.window({ log: false }).then((win) => {
|
||||
installConsoleInterceptors(win);
|
||||
});
|
||||
|
||||
// Install on all future windows (navigation, reload, etc.)
|
||||
Cypress.on('window:before:load', (win) => {
|
||||
installConsoleInterceptors(win);
|
||||
});
|
||||
|
||||
cy.log('Console capture started');
|
||||
});
|
||||
|
||||
// Stop capturing console logs
|
||||
Cypress.Commands.add('stopConsoleCapture', () => {
|
||||
isCapturing = false;
|
||||
cy.log('Console capture stopped');
|
||||
});
|
||||
|
||||
// Get all captured logs
|
||||
Cypress.Commands.add('getConsoleLogs', () => {
|
||||
return cy.wrap(consoleLogs, { log: false });
|
||||
});
|
||||
|
||||
// Print captured logs summary
|
||||
Cypress.Commands.add('printConsoleLogsSummary', () => {
|
||||
const summary = {
|
||||
total: consoleLogs.length,
|
||||
errors: consoleLogs.filter(l => l.type === 'error').length,
|
||||
warnings: consoleLogs.filter(l => l.type === 'warn').length,
|
||||
logs: consoleLogs.filter(l => l.type === 'log').length,
|
||||
info: consoleLogs.filter(l => l.type === 'info').length,
|
||||
debug: consoleLogs.filter(l => l.type === 'debug').length
|
||||
};
|
||||
|
||||
cy.task('log', '=== Console Logs Summary ===');
|
||||
cy.task('log', `Total logs captured: ${summary.total}`);
|
||||
cy.task('log', ` - Errors: ${summary.errors}`);
|
||||
cy.task('log', ` - Warnings: ${summary.warnings}`);
|
||||
cy.task('log', ` - Logs: ${summary.logs}`);
|
||||
cy.task('log', ` - Info: ${summary.info}`);
|
||||
cy.task('log', ` - Debug: ${summary.debug}`);
|
||||
|
||||
// Print all logs
|
||||
if (consoleLogs.length > 0) {
|
||||
cy.task('log', '=== Captured Console Logs ===');
|
||||
consoleLogs.forEach((log, index) => {
|
||||
const message = stringifyArgs(log.args);
|
||||
cy.task('log', `[${index + 1}] ${log.timestamp} [${log.type.toUpperCase()}] (${log.url || 'unknown'}): ${message}`);
|
||||
});
|
||||
cy.task('log', '=== End of Console Logs ===');
|
||||
} else {
|
||||
cy.task('log', 'No console logs were captured');
|
||||
}
|
||||
});
|
||||
|
||||
// Clear captured logs
|
||||
Cypress.Commands.add('clearConsoleLogs', () => {
|
||||
consoleLogs = [];
|
||||
});
|
||||
|
||||
// Add TypeScript definitions
|
||||
declare global {
|
||||
namespace Cypress {
|
||||
interface Chainable {
|
||||
/**
|
||||
* Start capturing console logs from the application
|
||||
*/
|
||||
startConsoleCapture(): Chainable<void>;
|
||||
|
||||
/**
|
||||
* Stop capturing console logs
|
||||
*/
|
||||
stopConsoleCapture(): Chainable<void>;
|
||||
|
||||
/**
|
||||
* Get all captured console logs
|
||||
*/
|
||||
getConsoleLogs(): Chainable<ConsoleLog[]>;
|
||||
|
||||
/**
|
||||
* Print a summary of captured console logs
|
||||
*/
|
||||
printConsoleLogsSummary(): Chainable<void>;
|
||||
|
||||
/**
|
||||
* Clear all captured console logs
|
||||
*/
|
||||
clearConsoleLogs(): Chainable<void>;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {};
|
||||
Reference in New Issue
Block a user