【问题标题】:Assertion on sorting a table in cypress关于在柏树中排序表的断言
【发布时间】:2019-08-08 13:46:59
【问题描述】:

我正在 Cypress 上尝试一些 UI 测试,并希望继续讨论在 SO 中提到的 here

我有一张这样的表格,但想根据 A 或 B 列进行排序。

我遵循了链接中提到的解决方案,但断言失败 - 预期 [Array(3)] 与 [Array(3)] 完全相等

function getCellTextAsArray(){
let cellContents = []
return new Cypress.Promise(resolve => {
    cy.get('#datatable-tabletools').find('tbody').children()

        .each(($el, $index) => {
            //some logic to select the elements you want
            //like $index % 4 == 0
            if($index>=0) {
                cellContents.push($el.text())
            }
        }).debug()
        .then(() => resolve(cellContents))
})
}

然后调用这个函数为

getCellTextAsArray()
            .then(cellContents => {
                let actual = cellContents.slice()
                cy.wrap(actual)
                    .should('deep.eq', cellContents.sort())})   

抱歉,我是 javascript 新手。

【问题讨论】:

  • .sort() 函数没有比较器,将使用默认的 ascii 排序进行比较。尝试使用console.log(cellContents.sort()) 来查看是否按预期订购。
  • @WashingtonGuedes - 是的,我试过了,这就是问题所在,它没有排序
  • 然后你应该为每种数据类型添加一个比较器函数,如果你想对数字进行排序,你可以这样做:function compareNumbers (a, b) { return (+a) - (+b); } 并使用cellContents.sort(compareNumbers),对于日期你可以使用一些时刻/luxon 库要正确解析输入,对于字符串,您可以使用默认的 ascii 比较器。

标签: javascript jquery ecmascript-6 cypress


【解决方案1】:
cy.get('#example>tbody > tr > :nth-child(1)') // this is the table column 'A'
    .then(function(A)
    {

        const age_values= A
        .toArray()
    
        .map($e1=> parseInt($e1.textContent))// to integer from constant
        
        expect(age_values).to.be.sorted({descending:true})//use chai-assertion

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2019-11-09
    • 2021-01-27
    • 1970-01-01
    • 2021-10-29
    • 2020-12-17
    • 1970-01-01
    相关资源
    最近更新 更多