英文:
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()
},
})
英文:
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()
},
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论