英文:
Add a Visible Timestamp to Cypress Screenshots
问题
Cypress 只能在浏览器窗口内进行屏幕截图,不包括时间戳。似乎没有提供此功能。是否有一种方法可以在屏幕截图上包含日期/时间戳?
英文:
Since Cypress is only able to take screenshots within the browser window, it doesn't include a timestamp. It appears this functionality available.
Is there some way to include a date/time stamp on the screen capture?
答案1
得分: 3
Cypress提供了屏幕截图钩子,你可以在这里找到文档 使用onBeforeScreenshot和onAfterScreenshot改变DOM。
你可以像这样提供一个时间戳
let timestamp;
Cypress.Screenshot.defaults({
onBeforeScreenshot($el) {
timestamp = document.createElement('span')
timestamp.innerText = new Date().toLocaleString('de-DE')
$el[0].appendChild(timestamp)
},
onAfterScreenshot($el, props) {
timestamp.remove()
},
})
英文:
Cypress provides screenshot hooks, you can find the documentation here Change the DOM using onBeforeScreenshot and onAfterScreenshot.
You can provide a timestamp like this
let timestamp;
Cypress.Screenshot.defaults({
onBeforeScreenshot($el) {
timestamp = document.createElement('span')
timestamp.innerText = new Date().toLocaleString("de-DE")
$el[0].appendChild(timestamp)
},
onAfterScreenshot($el, props) {
timestamp.remove()
},
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论