Cypress校验href列表不含creditcheck字符串报undefined错误如何处理
报错原因
- 你当前的遍历对象是
<ul>元素,调用.children()拿到的是<li>子节点,<li>本身没有href属性,调用invoke('attr','href')返回的结果是undefined should('not.include')仅支持校验数组、字符串、集合等可遍历类型,传入undefined会触发类型不匹配的断言错误
正确实现方案
直接定位到所有需要校验的<a>标签再执行校验即可,不需要分层遍历ul和li,示例代码如下:
describe('check hrefs', () => { it('verify all tab hrefs do not contain creditcheck string', () => { cy.login() cy.visit('url3') cy.get('tbody > :nth-child(1) > .col-id-large').click({force: true}) // 一次性选中ul下所有a标签,批量校验href属性 cy.get('#clientinfotabs > ul a').should($aElements => { $aElements.each((index, aDom) => { const href = aDom.getAttribute('href') expect(href).to.not.include('creditcheck') }) }) }) })
如果确实需要分层遍历,也可以调整原有代码的遍历层级:
cy.get('#clientinfotabs > ul li') // 遍历li节点而不是ul节点 .each($li => { const $a = $li.children('a') expect($a.attr('href')).to.not.include('creditcheck') })
内容的提问来源于stack exchange,提问作者cypher_null
相关产品推荐
相关产品推荐

