gpt4 book ai didi

cypress - 检查 Cypress 中的多个文本项

转载 作者:行者123 更新时间:2023-12-05 01:50:22 25 4
gpt4 key购买 nike

我有一个表格,我正在测试列标题是否正确。问题是 .contains() 只返回一个元素。

我可以为每个实例重复代码,但它看起来很冗长。感觉我一定在 Cypress 命令中遗漏了一些可以做得更好的东西。

cy.get('th').eq(0).contains('Batman')
cy.get('th').eq(1).contains('Robin')
cy.get('th').eq(1).contains('The Flash')
/// etc
<table>
<caption>Superheros and sidekicks</caption>
<colgroup>
<col>
<col span="2" class="batman">
<col span="2" class="flash">
</colgroup>
<tr>
<td> </td>
<th scope="col">Batman</th>
<th scope="col">Robin</th>
<th scope="col">The Flash</th>
<th scope="col">Kid Flash</th>
</tr>
<tr>
<th scope="row">Skill</th>
<td>Smarts</td>
<td>Dex, acrobat</td>
<td>Super speed</td>
<td>Super speed</td>
</tr>
</table>

最佳答案

您可以通过将元素映射到内部文本来整理测试。

注意,用 tr:eq(0) 指定第一行

const expectedHeaders = ['Batman', 'Robin', 'The Flash', 'Kid Flash']
cy.get('tr:eq(0) th')
.then($th => [...$th].map(th => th.innerText)) // map to texts
.should('deep.eq', expectedHeaders)

关于cypress - 检查 Cypress 中的多个文本项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73215260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com