Javascript 重命名cypress屏幕截图
我正在尝试重命名为失败的cypress测试创建的屏幕截图。 我遵循了文档的示例 对于测试,我使用cypress提供的测试套件 只要我使用静态字符串,重命名文件就可以工作,但是当我尝试从详细信息对象(如时间戳或旧路径)访问数据时,重命名失败Javascript 重命名cypress屏幕截图,javascript,cypress,Javascript,Cypress,我正在尝试重命名为失败的cypress测试创建的屏幕截图。 我遵循了文档的示例 对于测试,我使用cypress提供的测试套件 只要我使用静态字符串,重命名文件就可以工作,但是当我尝试从详细信息对象(如时间戳或旧路径)访问数据时,重命名失败 Error: ENOENT: no such file or directory, rename 'C:\projects\playground\cypress\screenshots\examples\actions.spec .js\Actions--
Error: ENOENT: no such file or directory, rename 'C:\projects\playground\cypress\screenshots\examples\actions.spec
.js\Actions-- .type() - type into a DOM element (failed).png'
-> 'C:\projects\playground\cypress\mochareports\screenshots\2020-03-16T08.55:09.929Z.png'
我的代码如下所示
const fs=require('fs'))
//截图名称对于文件系统来说可能太长,这就是为什么我们要截断它们
module.exports=(打开,配置)=>{
on('after:screenshot',(细节)=>{
console.log(details)//将所有细节打印到终端
//const fileName=details.takenAt.replace(“:”,“)+“.png”;//此操作失败
const fileName=details.specName+“.1png”;//此操作失败
log(文件名);
//const fileName=“test.png”;//这很有效
const newPath=“cypress/mochareports/screenshots/”+文件名;
console.log(newPath);
返回新承诺((解决、拒绝)=>{
//fs.rename将文件移动到现有目录“new/path/to”
//并将图像重命名为“screenshot.png”
fs.rename(details.path,newPath,(err)=>{
如果(错误)返回拒绝(错误)
//因为我们重命名并移动了图像,所以使用新路径进行解析
//因此,测试结果是准确的
解析({path:newPath})
})
})
})
}
让我困惑的是,在执行日志中,我的console.log在实际测试日志之前可见:
...
Running: examples\actions.spec.js (1 of 19)
Actions
{
size: 148523,
takenAt: '2020-03-16T08:55:09.929Z',
dimensions: { width: 1280, height: 720 },
multipart: false,
specName: 'examples\\actions.spec.js',
testFailure: true,
path: 'C:\\Projekte\\playground\\cypress\\screenshots\\examples\\actions.spec.js\\Actions -- .type() - type into a DOM element (failed).png',
scaled: true,
blackout: [],
duration: 342
}
2020-03-16T08.55:09.929Z.png
cypress/mochareports/screenshots/2020-03-16T08.55:09.929Z.png
1) .type() - type into a DOM element
√ .focus() - focus on a DOM element (326ms)
...
当我向after:screenshot事件添加函数时,控制台日志不应该发生在测试执行之后吗?问题在于javascript代码
const fileName=details.takenAt.replace(“:”,“)+.png”
失败,因为只替换了第一个冒号,而保留了其余冒号。由于文件名中不允许冒号,重命名失败
const fileName=details.takenAt.replace(“/:/g”,“)+.png”
这实际上替换了所有的冒号,效果很好
此外,将specname设置为filename失败,因为specname的架构为“example/action.spec.js”。这会失败,因为目录示例不存在于cypress/mocharreports/screenshots/中。要解决此问题,需要创建目录或将所需文件名更改为不带斜杠的字符串