如何检查柏树的评论?
How to check for comments with cypress?
不是使用 cypress 的典型用例,但我想使用 cypress 检查 HTML 代码中是否存在(某些)注释。
最好的方法是什么?
要在 <body>
内获得像 <!-- my-comment -->
这样的评论
function filterNone() {
return NodeFilter.FILTER_ACCEPT;
}
function getAllComments(rootElem) {
var comments = [];
var iterator = document.createNodeIterator(rootElem, NodeFilter.SHOW_COMMENT, filterNone, false);
var curNode;
while (curNode = iterator.nextNode()) {
comments.push(curNode.nodeValue);
}
return comments;
}
cy.get('body').then($body => {
return cy.wrap(getAllComments($body[0]))
})
.should('deep.eq', [' my-comment '])
更简洁的方式
cy.document().then(doc => {
const comments = [...doc.body.childNodes]
.filter(node => node.nodeName === '#comment')
.map(commentNode => commentNode.data)
cy.wrap(comments)
.should('deep.eq', [' my-comment '])
})
不是使用 cypress 的典型用例,但我想使用 cypress 检查 HTML 代码中是否存在(某些)注释。 最好的方法是什么?
要在 <body>
<!-- my-comment -->
这样的评论
function filterNone() {
return NodeFilter.FILTER_ACCEPT;
}
function getAllComments(rootElem) {
var comments = [];
var iterator = document.createNodeIterator(rootElem, NodeFilter.SHOW_COMMENT, filterNone, false);
var curNode;
while (curNode = iterator.nextNode()) {
comments.push(curNode.nodeValue);
}
return comments;
}
cy.get('body').then($body => {
return cy.wrap(getAllComments($body[0]))
})
.should('deep.eq', [' my-comment '])
更简洁的方式
cy.document().then(doc => {
const comments = [...doc.body.childNodes]
.filter(node => node.nodeName === '#comment')
.map(commentNode => commentNode.data)
cy.wrap(comments)
.should('deep.eq', [' my-comment '])
})