英文:
Cypress delete screenshots of failure if they are 30 days old
问题
我的配置已设置为 trashAssetsBeforeRuns: FALSE
,但是cypress/screenshots
会随着时间而增加。
这主要是为了Jenkins,因为Cypress运行命令一旦开始重新通过,就会擦除所有先前的失败截图。这样我就不知道先前是什么失败了。
我知道可以使用Jenkins本身来实现,但是是否有一种方法可以配置Cypress来删除超过1个月的cypress/screenshots
内容?
谢谢。
英文:
my config is set to trashAssetsBeforeRuns: FALSE
however the cypress/screenshots
gets packed over time.
This is mainly for Jenkins because the Cypress Run command wipes all previous screenshots of failures once they start to pass again. That way I don't know what was failing previously.
I know it can be done using Jenkins itself but, is there a way to configure Cypress to delete the content of cypress/screenshots
that are over 1 month old?
Thank you.
答案1
得分: 1
你可以创建一个自定义脚本或使用cronjob来为您执行此操作
通过使用JS,您可以使用NodeJS编写代码:
const fs = require('fs');
const path = require('path');
const screenshotsDirectory = 'cypress/screenshots';
const maxAgeInDays = 30;
const deleteOldScreenshots = async () => {
try {
const files = await fs.promises.readdir(screenshotsDirectory);
const currentDate = new Date();
const maxAgeDate = currentDate.setDate(currentDate.getDate() - maxAgeInDays);
files.forEach(async (file) => {
const filePath = path.join(screenshotsDirectory, file);
const fileStat = await fs.promises.stat(filePath);
if (fileStat.isFile() && fileStat.ctime < maxAgeDate) {
await fs.promises.unlink(filePath);
console.log(`Deleted old screenshot: ${file}`);
}
});
} catch (error) {
console.error('Error occurred while deleting old screenshots:', error);
}
};
deleteOldScreenshots();
当然,您需要安装fs
模块。
英文:
You can create a custom script or use cronjob to do that for you
By using JS you can do a code using NodeJS:
const fs = require('fs');
const path = require('path');
const screenshotsDirectory = 'cypress/screenshots';
const maxAgeInDays = 30;
const deleteOldScreenshots = async () => {
try {
const files = await fs.promises.readdir(screenshotsDirectory);
const currentDate = new Date();
const maxAgeDate = currentDate.setDate(currentDate.getDate() - maxAgeInDays);
files.forEach(async (file) => {
const filePath = path.join(screenshotsDirectory, file);
const fileStat = await fs.promises.stat(filePath);
if (fileStat.isFile() && fileStat.ctime < maxAgeDate) {
await fs.promises.unlink(filePath);
console.log(`Deleted old screenshot: ${file}`);
}
});
} catch (error) {
console.error('Error occurred while deleting old screenshots:', error);
}
};
deleteOldScreenshots();
Sure you need to install fs
module
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论