我有一个包含这样的列的表格(简化):
| ... | ... | Venter på søker | ... |
| ... | ... | Ikke påbegynt | ... |
该表有 21 行。
相关列可以按字母顺序、降序或升序排序。
我想做的是让 Cypress 验证排序、升序和降序。
到目前为止我所得到的:
cy.get([data-e2e-selector=tabell]).get('[data-e2e-selector=kolonne]')
.then(items => {
const unsortedItems = items.map((index, html) => Cypress.$(html).text()).get();
const sortedItems = unsortedItems.slice().sort()
expect(unsortedItems, 'Items are sorted').to.deep.equal(sortedItems);
});
我想做的是从列中获取所有项目并将它们存储在列表中。然后创建一个包含相同项目但已排序的列表。然后比较两者。我已经成功地对日期戳(数字)做了类似的事情,但按字母顺序排序似乎让我无法理解。
上述代码会导致此错误(升序或降序相同):
assert expected Items are sorted: to deeply equal [ Array(21) ]
为了验证我是否确实将元素放入列表中 - 如果我比较 unsortedItems[] 和sortedItems[] 列表的第一个或最后一个项目,它们是相同的:
expect(unsortedItems, 'Items are sorted (' + unsortedItems[20] + ' - ' + sortedItems[20] + ')').to.deep.equal(sortedItems);
...
assert expected Items are sorted ( Ikke påbegynt - Ikke påbegynt ): to deeply equal [ Array(21) ]
assertexpected Items are sorted ( Venter på søker - Venter på søker ): to deeply equal [ Array(21) ]
此外,检查列表的长度会给出正确的大小,21。
所以,看来我实际上已经得到了正确的内容(slice().sort() 按预期工作),但是当我尝试 deep.equal 时我没有做得正确。
有什么想法吗?
从技术上讲,这应该可行,前提是将查询中的第二个
.get()
更改为.find()
。
cy.get([data-e2e-selector=tabell]) // pick table
.find('[data-e2e-selector=kolonne]') // pick column
.then(items => {
const unsortedItems = items.map((index, html) => Cypress.$(html).text()).get();
const sortedItems = unsortedItems.slice().sort()
unsortedItems.forEach((uns,i) => {
expect(uns).to.eq(sorted[i])
})
})
可运行:
const items1 = $('table').find('td:nth-child(1)')
const unsorted1 = items1.map((index, html) => $(html).text()).get()
const sorted1 = unsorted1.slice().sort()
const firstColIsSorted = unsorted1.every((us, i) => us === sorted1[i])
console.log('firstColIsSorted', firstColIsSorted)
const items2 = $('table').find('td:nth-child(2)')
const unsorted2 = items2.map((index, html) => $(html).text()).get()
const sorted2 = unsorted1.slice().sort()
const secondColIsSorted = unsorted2.every((us, i) => us === sorted2[i])
console.log('secondColIsSorted', secondColIsSorted)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>a</td>
<td>2</td>
</tr>
<tr>
<td>b</td>
<td>3</td>
</tr>
<tr>
<td>c</td>
<td>1</td>
</tr>
</tbody>
</table>