我正在尝试用 cypress 测试分页栏。
我想断言仅在此栏中包含数字的按钮数量,并忽略其他按钮(上一页、下一页...)
这些按钮看起来像这样:
<button class="...">33</button>
Run Code Online (Sandbox Code Playgroud)
我首先尝试了这个测试:
cy.get('.pagination')
.find('button')
.contains(/\d+/)
.should('have.length.gte', 2)
Run Code Online (Sandbox Code Playgroud)
但这给了我一个警告,即 contains 只会返回一个元素,使得“长度”测试毫无用处。
我还尝试了许多基于过滤器的组合,即“:contains”jquery关键字,但没有一个有效:
.filter(`:contains('/\d+\')`)
// >> finds nothing
.filter((elt) => { return elt.contains(rx) })
// >> throws 'elt.contains is not a function'
.filter((elt) => { return rx.test(elt.text()) })
// >> throws 'elt.text is not a function'
.filter(() => { return rx.test(Cypress.$(this).text()) })
// filter everything and return nothing, even the buttons containing the text '1'
Run Code Online (Sandbox Code Playgroud)
.filter()回调有参数(index, elt) => {},这意味着你可以像这样使用它
cy.get('.pagination')
.find('button')
.filter((index, elt) => { return elt.innerText.match(/\d+/) })
.should('have.length.gte', 2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4895 次 |
| 最近记录: |