在Cypress截图中添加可见的时间戳

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

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()
  },
})

huangapple
  • 本文由 发表于 2023年6月15日 00:21:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475670.html
匿名

发表评论

匿名网友

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

确定