kak*_*ret -1 sorting alphabetical cypress
我有一个包含这样的列的表(简化):
\n| ... | ... | Venter p\xc3\xa5 s\xc3\xb8ker | ... |\n| ... | ... | Ikke p\xc3\xa5begynt | ... |\nRun Code Online (Sandbox Code Playgroud)\n该表有 21 行。
\n相关列可以按字母顺序、降序或升序排序。
\n我想做的是让 Cypress 验证排序、升序和降序。
\n到目前为止我所得到的:
\ncy.get([data-e2e-selector=tabell]).get('[data-e2e-selector=kolonne]')\n .then(items => {\n const unsortedItems = items.map((index, html) => Cypress.$(html).text()).get();\n const sortedItems = unsortedItems.slice().sort()\n expect(unsortedItems, 'Items are sorted').to.deep.equal(sortedItems);\n });\nRun Code Online (Sandbox Code Playgroud)\n我想做的是从列中获取所有项目并将它们存储在列表中。然后创建一个包含相同项目但已排序的列表。然后比较两者。我已经成功地对日期戳(数字)做了类似的事情,但按字母顺序排序似乎让我困惑。
\n上面的代码会导致此错误(升序或降序相同):
\nassert expected Items are sorted: to deeply equal [ Array(21) ]\nRun Code Online (Sandbox Code Playgroud)\n为了验证我是否确实将元素放入列表中 - 如果我比较 unsortedItems[] 和sortedItems[] 列表的第一个或最后一个项目,它们是相同的:
\nexpect(unsortedItems, 'Items are sorted (' + unsortedItems[20] + ' - ' + sortedItems[20] + ')').to.deep.equal(sortedItems);\nRun Code Online (Sandbox Code Playgroud)\n...
\nassert expected Items are sorted ( Ikke p\xc3\xa5begynt - Ikke p\xc3\xa5begynt ): to deeply equal [ Array(21) ]\n\nassertexpected Items are sorted ( Venter p\xc3\xa5 s\xc3\xb8ker - Venter p\xc3\xa5 s\xc3\xb8ker ): to deeply equal [ Array(21) ]\nRun Code Online (Sandbox Code Playgroud)\n另外,检查列表的长度可以得出正确的大小,21。
\n所以,看来我实际上已经得到了正确的内容(slice().sort() 按预期工作),但当我尝试 deep.equal 时,我没有做得正确。
\n有任何想法吗?
\n小智 5
从技术上讲,只要.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])
})
})
Run Code Online (Sandbox Code Playgroud)
可运行:
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)Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |