Cypress删除30天前的失败截图。

huangapple go评论61阅读模式
英文:

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(&#39;fs&#39;);
const path = require(&#39;path&#39;);

const screenshotsDirectory = &#39;cypress/screenshots&#39;;
const maxAgeInDays = 30;

const deleteOldScreenshots = async () =&gt; {
  try {
    const files = await fs.promises.readdir(screenshotsDirectory);

    const currentDate = new Date();
    const maxAgeDate = currentDate.setDate(currentDate.getDate() - maxAgeInDays);

    files.forEach(async (file) =&gt; {
      const filePath = path.join(screenshotsDirectory, file);
      const fileStat = await fs.promises.stat(filePath);

      if (fileStat.isFile() &amp;&amp; fileStat.ctime &lt; maxAgeDate) {
        await fs.promises.unlink(filePath);
        console.log(`Deleted old screenshot: ${file}`);
      }
    });
  } catch (error) {
    console.error(&#39;Error occurred while deleting old screenshots:&#39;, error);
  }
};

deleteOldScreenshots();

Sure you need to install fs module

huangapple
  • 本文由 发表于 2023年6月26日 17:36:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定