首页 > 解决方案 > 赛普拉斯:检查href列表,断言它们不包含某个字符串

问题描述

我有一个需要检查的 href 列表。我只需要确保它们都不包含字符串“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() 获取“a”
  4. 调用 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');
            })
    })
})

这就是我运行此脚本时赛普拉斯返回的内容:

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 | })

标签: iterationcypresshrefassertion

解决方案


所以你真的很亲近。而不是cy.get()你必须使用cy.wrap(). 你也可以使用 selector 直接循环 li #clientinfotabs > ul > 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')
    })
  })
})

推荐阅读