时间戳在Cypress截图中

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

Time stamp in cypress screenshots

问题

我想在截图的左上角添加时间戳。我正在寻找JavaScript代码,结合Cypress方法来实现此功能。

我能够在左下角添加时间戳,但不知何故无法添加到其他位置。

英文:

I want to add time stamp in top left corner of the screenshot . I am looking for JavaScript code mixed with cypress methods to achieve this functionality

I am able to add timestamp in the bottom left corner but somehow I am not able to any other place

答案1

得分: 2

你可以在这里查看答案:在Cypress截图中添加可见的时间戳

确切的定位位置取决于您正在使用的页面。但是,要在左上角而不是左下角进行操作,可以尝试使用insertBefore()而不是appendChild()。以下是示例代码:

let timestamp;
Cypress.Screenshot.defaults({
  onBeforeScreenshot($el) {
    timestamp = document.createElement('span')
    timestamp.innerText = new Date().toLocaleString("de-DE")
    const $body = $el.find('body')
    $el[0].insertBefore(timestamp, $body[0])
  },
  onAfterScreenshot($el, props) {
    timestamp.remove()
  },
})

时间戳在Cypress截图中

英文:

You have seen the answer here Add a Visible Timestamp to Cypress Screenshots.

The exact positioning depends quite a bit on the page you are working with.

But to go to top-left instead of bottom-left, the obvious thing to try is insertBefore() instead of appendChild().

let timestamp;
Cypress.Screenshot.defaults({
  onBeforeScreenshot($el) {
    timestamp = document.createElement('span')
    timestamp.innerText = new Date().toLocaleString("de-DE")
    const $body = $el.find('body')
    $el[0].insertBefore(timestamp, $body[0])
  },
  onAfterScreenshot($el, props) {
    timestamp.remove()
  },
})

时间戳在Cypress截图中

huangapple
  • 本文由 发表于 2023年7月17日 14:23:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701942.html
匿名

发表评论

匿名网友

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

确定