“。to.be.sorted()”断言不适用于赛普拉斯

问题描述 投票:0回答:1

嗨,我是柏树的新手。请帮助:为什么我不能将列表文本生成到数组?我尝试使用invoke,但它导致文本不在数组中。我已经尝试了以下方法。

it('can sort by aToz', () => {
  cy.get('h4.card-title')
  .then($titles => {
    const title = $titles
      .toArray()
      .map($el => $el.innerText)
    // assertion comes from chai-sorted
    expect(title).to.be.sorted()
  })
})


  it('checks sort by A to Z', () => {
    cy.get('#sort').select('A to Z')
    cy.wait(4000)
    function getTableData() {
        let cellContents = [];
        return new Cypress.Promise(resolve => {
          cy.get('h4.card-title')
            .each(($el, $index) => {
                cellContents.push($el.text());

             }) .then(() => resolve(cellContents))
            })
      }
      getTableData().then(cellContents => {
        expect(cellContents).to.be.sorted()
      })

但是两者都会产生这个:

(12) [h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title]
0: h4.card-title
1: h4.card-title
2: h4.card-title
3: h4.card-title
4: h4.card-title
5: h4.card-title
6: h4.card-title
7: h4.card-title
8: h4.card-title
9: h4.card-title
10: h4.card-title
11: h4.card-title
length: 12
__proto__: Array(0)

Here is my HTML

它正确记录。但是为什么会有这个错误?

Invalid Chai property: sorted

cypress chai
1个回答
0
投票

chai-sorted依赖性不是赛普拉斯提供的。

要使用chai-sorted,您需要:

  • npm i chai-sorted
  • support/index.js中添加:

    const chaiSorted = require('chai-sorted');
    chai.use(chaiSorted);
    

'chai-sorted'可以使用:expect(<list of string>).to.be.sorted()

© www.soinside.com 2019 - 2024. All rights reserved.