cypress-longi/cypress.config.js
2025-03-03 18:10:29 +08:00

166 lines
6.4 KiB
JavaScript

module.exports = {
e2e: {
viewportWidth: 1920,
viewportHeight: 1080,
numTestsKeptInMemory: 0,
experimentalMemoryManagement: true,
video: false,
screenshotOnRunFailure: false,
defaultCommandTimeout: 10000,
pageLoadTimeout: 30000,
retries: {
runMode: 1,
openMode: 0
},
setupNodeEvents(on, config) {
const fs = require('fs');
const path = require('path');
on('before:browser:launch', (browser = {}, launchOptions) => {
if (browser.name === 'chrome' || browser.name === 'electron') {
launchOptions.args.push(
'--js-flags="--expose-gc"',
'--disable-dev-shm-usage',
'--disable-background-timer-throttling',
'--disable-renderer-backgrounding',
'--disable-backgrounding-occluded-windows',
'--memory-pressure-off',
'--disable-site-isolation-trials',
'--js-flags="--max-old-space-size=4096"',
'--disable-gpu',
'--disable-notifications',
'--disable-extensions',
'--no-sandbox',
'--disable-software-rasterizer',
'--disable-logging',
'--disable-web-security',
'--disable-translate',
'--disable-features=site-per-process',
'--disable-features=TranslateUI',
'--disable-features=GlobalMediaControls'
);
}
return launchOptions;
});
on('task', {
clearMemory() {
try {
if (global.gc) {
global.gc();
}
return null;
} catch (e) {
console.error('GC failed:', e);
return null;
}
},
createReportDir(dirPath) {
try {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, {recursive: true});
}
return null;
} catch (e) {
console.error('Create report directory failed:', e);
return null;
}
},
readAllReports(environment) {
try {
const reportsDir = path.join(__dirname, 'cypress', 'reports');
const reports = [];
const files = fs.readdirSync(reportsDir);
files.forEach(file => {
if (file.startsWith(`${environment}-menu-test-report-batch-`) && file.endsWith('.json')) {
const filePath = path.join(reportsDir, file);
const content = fs.readFileSync(filePath, 'utf8');
reports.push(JSON.parse(content));
}
});
return reports;
} catch (e) {
console.error('Read reports failed:', e);
return [];
}
},
cleanupReports(environment) {
try {
const reportsDir = path.join(__dirname, 'cypress', 'reports');
const files = fs.readdirSync(reportsDir);
files.forEach(file => {
if (file.startsWith(`${environment}-menu-test-report-batch-`) && file.endsWith('.json')) {
fs.unlinkSync(path.join(reportsDir, file));
}
});
return null;
} catch (e) {
console.error('Cleanup reports failed:', e);
return null;
}
},
saveReport({data, filename}) {
try {
const reportsDir = path.join(__dirname, 'cypress', 'reports');
if (!fs.existsSync(reportsDir)) {
fs.mkdirSync(reportsDir, {recursive: true});
}
fs.writeFileSync(
path.join(reportsDir, filename),
JSON.stringify(data, null, 0)
);
return null;
} catch (e) {
console.error('Save report failed:', e);
return null;
}
},
getLatestDataFile() {
const fixturesDir = path.join(__dirname, 'cypress/fixtures');
try {
// 读取fixtures目录
const files = fs.readdirSync(fixturesDir);
// 过滤出数据文件
const dataFiles = files.filter(file =>
file.includes('-data-') && file.endsWith('.json')
);
if (dataFiles.length === 0) {
return null; // 没有找到数据文件
}
// 按文件名倒序排序
dataFiles.sort().reverse();
const latestFile = dataFiles[0];
// 读取文件内容
const fileContent = JSON.parse(
fs.readFileSync(path.join(fixturesDir, latestFile), 'utf8')
);
return {
filename: latestFile,
content: fileContent
};
} catch (error) {
console.error('读取数据文件时出错:', error);
return null;
}
}
});
return config;
},
},
};