赛普拉斯:检查 href 列表,断言它们不包含某个字符串

Cypress: Check list of hrefs, assert they do NOT contain a certain string

我有一个需要检查的 href 列表。我只需要确保其中 none 个包含字符串“creditcheck”

html 看起来像这样:

<div class="yiiTab clearfix content-right-side" id="clientinfotabs">
    <ul class="tabs left-side-tabs">
        <li>
            <a reloadonclick="" loadurl="/index.php?r=foo/bar;id=12345&amp;language=de" href="#foofoobar" class="active">Test<span class="tab-item-count">8</span></a></li>
        <li>
        <li>
        <li>
        <li>
          .
          .
          .
    </ul>

大意是这样的:

  1. 获取 ul
  2. 遍历每个 li 项目
  3. 使用 .children()
  4. 获取 'a'
  5. 调用 href 并与字符串进行比较

我尝试遍历列表并使用多种方法检查 href。这是我最近的尝试:

describe('check hrefs', () => {
    it('check hrefs', () => {

        cy.login()
        cy.visit('url3')
        cy.get('tbody > :nth-child(1) > .col-id-large').click({force: true})
        cy.get('#clientinfotabs > ul')
            .each(($el) => {
                cy.get($el).children().invoke('attr','href').should('not.include','creditcheck');
            })
    })
})

当我 运行 这个脚本时,Cypress returns 就是这样:

AssertionError
Timed out retrying after 10000ms: object tested must be an array, a map, an object, a set, a string, or a weakset, but undefined given
cypress/integration/test2.spec.js:10:62
   8 |         cy.get('#clientinfotabs > ul')
   9 |             .each(($el) => {
> 10 |                 cy.get($el).children().invoke('attr','href').should('not.include','creditcheck');
     |                                                              ^
  11 |             })
  12 |     })
  13 | })

所以你真的很亲密。您必须使用 cy.wrap() 而不是 cy.get()。您也可以使用选择器 #clientinfotabs > ul > li.

直接遍历 li
describe('check hrefs', () => {
  it('check hrefs', () => {
    cy.login()
    cy.visit('url3')
    cy.get('tbody > :nth-child(1) > .col-id-large').click({force: true})
    cy.get('#clientinfotabs > ul >li').each(($el) => {
      cy.wrap($el).invoke('attr', 'href').should('not.include', 'creditcheck')
    })
  })
})