英文:
Cypress: table contains specific row
问题
Cypress能够检查表格中是否包含行a、b、c吗?
(奖励问题:如果我有更多列,但我不关心它们的内容,我只想检查是否有一行包含3列的期望内容,Cypress能做到吗?)
英文:
With a table looking like this:
TH1 | TH2 | TH3 |
---|---|---|
a | b | a |
a | b | c |
b | b | c |
can Cypress check that the table contains the row a, b, c ?
(bonus question: what if I have some more columns, but I don't care about their content, I just want to check that I have a row where 3 columns have the expected content)
答案1
得分: 2
我会使用一个辅助函数将行映射到数组
function rowCells(row) {
return Cypress._.map(row.children, (cell) => cell.innerText)
}
cy.get('table tbody tr')
.then(rows => Cypress._.map(rows, rowCells))
.should('deep.eq', [['a','b','a'],['a','b','c'],['b','b','c']])
.its('1') // 2nd row
.should('deep.eq', ['a','b','c'])
或者如果您想解析数组,可以使用.then()
回调
cy.get('table tbody tr')
.then(rows => Cypress._.map(rows, rowCells))
.then(tableData => {
expect(tableData[1]).to.deep.eq(['a','b','c'])
})
仅获取前3列
function rowCells(row) {
const cells = [...row.children].slice(0,3) // 在这里定义要获取的列
return Cypress._.map(cells, (cell) => cell.innerText)
}
英文:
I would use a helper function to map rows to arrays
function rowCells(row) {
return Cypress._.map(row.children, (cell) => cell.innerText)
}
cy.get('table tbody tr')
.then(rows => Cypress._.map(rows, rowCells))
.should('deep.eq', [['a','b','a'],['a','b','c'],['b','b','c']])
.its('1') // 2nd row
.should('deep.eq', ['a','b','c'])
or if you want to dissect the array, use a .then()
callback
cy.get('table tbody tr')
.then(rows => Cypress._.map(rows, rowCells))
.then(tableData => {
expect(tableData[1])to.deep.eq(['a','b','c'])
})
Take only first 3 columns
function rowCells(row) {
const cells = [...row.children].slice(0,3) // define which columns here
return Cypress._.map(cells, (cell) => cell.innerText)
}
答案2
得分: 0
你可以想象成这样:
const results: string[] = [];
cy.get('tr')
.each((line: JQuery) => {
const row = []
line.find('td').foreach(column=>row.push(column.textContent.trim()))
result.push(row)
});
});
.then(()=>{
expect(result)... // 对结果进行断言以查找所需文本
})
英文:
You can imagine something like:
const results: string[] = [];
cy.get('tr')
.each((line: JQuery) => {
const row = []
line.find('td').foreach(column=>row.push(column.textContent.trim()))
result.push(row)
});
});
.then(()=>{
expect(result)... // Assertion on result to find the desired text
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论