提问人:Jasper Slokker 提问时间:11/7/2023 最后编辑:Chrome ArtieJasper Slokker 更新时间:11/8/2023 访问量:40
使用步骤定义验证表,该表也包含一个表
Verify table with step definition also containing a table
问:
我有这个步骤定义:
Then I verify that Ontvanger has received Type message with Onderwerp and Status
| berichtOntvanger | berichtType | berichtOnderwerp | berichtStatus |
| test1Rij1 | test2Rij1 | test3Rij1 | test4Rij1 |
可以有一行或多行。
现在,我必须使用应用程序中的表来验证这一点。 但从现在开始我就被困住了:
verifyBerichtenTableRow(table) {
const tableContents = table.hashes();
tableContents.forEach((element) => {
cy.get('.berichten-centrum .p-datatable-table')
.find('tr')
.each(($row, $rowIndex) => {
if ($rowIndex >= 1) {
cy.wrap($row)
.find('td')
.each(($cell, $columnIndex) => {
cy.wrap($cell.eq($columnIndex)).should('have.text', element.DatumTijd);
cy.wrap($cell.eq($columnIndex)).should('have.text', element.Ontvanger);
cy.wrap($cell.eq($columnIndex)).should('have.text', element.Type);
cy.wrap($cell.eq($columnIndex)).should('have.text', element.Onderwerp);
cy.wrap($cell.eq($columnIndex)).should('have.text', element.stand);
});
}
});
});
}
该测试连续找到六个“td”,这是正确的,但从不遍历这六个。仅检查第一个 td。有人有想法吗?谢谢
答:
3赞
Aladin Spaz
11/8/2023
#1
问题似乎出在部分。此时,您有一个单元格,因为正在从 给出的集合中获取一个个体。$cell.eq()
.each(($cell
<td>
.find('td')
因此,您只需要:
.find('td')
.each(($cell, $columnIndex) => {
cy.wrap($cell).should('have.text', <element-column-here>)
})
但是现在,您需要弄清楚会发生什么。<element-column-here>
它看起来像是一个具有与列名对应的属性的对象,element
// element shape
{
"DatumTijd": "2023-11-05",
"Ontvanger": "John",
"Type": "chicken",
"Onderwerp": "How to cook"
}
所以你应该能够使用Object.values[element][$columnIndex]
.find('td')
.each(($cell, $columnIndex) => {
cy.wrap($cell).should('have.text', Object.values[element][$columnIndex])
})
使用单元格列表
另一种方式,改为.each($cell)
.then($cells)
现在,您正在使用完整的单元格列表,并可用于索引单个单元格。.eq(n)
cy.wrap($row)
.find('td')
.then($cells => {
cy.wrap($cells.eq(0)).should('have.text', element.DatumTijd)
cy.wrap($cells.eq(1)).should('have.text', element.Ontvanger)
cy.wrap($cells.eq(2)).should('have.text', element.Type)
...
评论
0赞
Jasper Slokker
11/8/2023
谢谢,then() 真的很有帮助,现在它循环遍历前端表。现在唯一剩下的就是遍历 Cypress step def 表,换句话说,当我有不止一行要检查时。
1赞
Aladin Spaz
11/9/2023
嘿 Jasper,它应该适用于多行 - 给出 DataTable 中的所有行。table.hashes()
1赞
Aladin Spaz
11/9/2023
如果问题来自 ,也许按照 ChromeArtie 的建议添加选择器?cy.get(table)
tbody
0赞
Jasper Slokker
11/8/2023
#2
感谢 @Aladin Spaz,功能现在如下:
verifyBerichtenTableRow(table) {
const tableContents = table.hashes();
tableContents.forEach((element) => {
cy.get(this.#berichtenTable)
.find('tr')
.each(($row, $rowIndex) => {
if ($rowIndex >= 1) {
cy.wrap($row)
.find('td')
.then(($cell) => {
cy.wrap($cell.eq(0)).should('have.text', element.DatumTijd);
cy.wrap($cell.eq(1)).should('have.text', element.Ontvanger);
cy.wrap($cell.eq(2)).should('have.text', element.Type)
cy.wrap($cell.eq(3)).should('have.text', element.Onderwerp)
});
}
});
});
}
它现在检查不同的 TD 及其值。但它并没有贯穿赛普拉斯的表格。它只检查第一行。有人有解决方案吗?
评论
1赞
Chrome Artie
11/8/2023
如果我在文件中添加两行(即标题 + 两个数据行),则运行两次。你能解释一下为什么它不为你检查第二行吗?每次都应该不同。feature
tableContents.forEach((element) =>
element
1赞
Chrome Artie
11/8/2023
在 HTML 中,你得到的是 而不是 ?thead 只有一行。也许是一个更好的选择器?<thead>
<tbody>
cy.get('.berichten-centrum .p-datatable-table tbody')
评论