Cypress如何检查表中的值是升序还是降序。

huangapple go评论54阅读模式
英文:

Cypress how to check if the values in the table is in ascending order or descending order

问题

  1. 如何验证表格中的值是否按升序或降序排序?
  2. 如何使用两个函数isAscending和isDescending?
    我已经使用以下代码将每个元素存储在数组中:
let Arr1 = new Array()
cy.get('selector')
  .each(($el) => {Arr1.push($el.text()) })
cy.log(Arr1)

用于检查排序顺序的代码如下,但不起作用:

cy.wrap(Arr1).should('equal', Arr1.sort()); 
// 即使数组是降序也会通过
function isAscending(arr) {
  return arr.every(function (x, i) { 
    return i === 0 || x >= arr[i - 1];  
  })
}
英文:

I have the table which contains any values like names, addresses and dates etc.
Need to check if the values in the table should be in ascending or descending order after performing sorting option

1.Here how I can verify if the values in the table is in ascending or descending order
2.How I can use the two functions isAscending and isDescending
I have stored the each elements in the array by using the below code

let Arr1 = new Array()
cy.get('selector')
  .each(($el) => {Arr1.push($el.text()) })
cy.log(Arr1)

For checking sorting order I have tried below code but it does not works

cy.wrap(Arr1).should('equal', Arr1.sort()); 
// here if the array is in descending order also it's passing
function isAscending(arr) {
  return arr.every(function (x, i) { 
    return i === 0 || x >= arr[i - 1];  
  })
}

答案1

得分: 3

The isAscending() function can be applied in a .should() with satisfy matcher.

See chaijs - satisfy

For example, the equivalent Cypress should expressions would be

function isAscending(arr) {
  return arr.every(function (x, i) { 
    return i === 0 || x >= arr[i - 1];  
  }); 
}

let Arr1 = new Array()
cy.get('selector')
  .each(($el) => {Arr1.push($el.text()) })
  .then(() => {
    cy.wrap(Arr1).should('satisfy', isAscending)
  })
英文:

The isAscending() function can be applied in a .should() with satisfy matcher.

See chaijs - satisfy

> js
> expect(1).to.satisfy(function(num) {
> return num > 0;
> })
>

For example the equivalent Cypress should expressions would be

function isAscending(arr) {
  return arr.every(function (x, i) { 
    return i === 0 || x >= arr[i - 1];  
  }); 
}

let Arr1 = new Array()
cy.get('selector')
  .each(($el) => {Arr1.push($el.text()) })
  .then(() => {
    cy.wrap(Arr1).should('satisfy', isAscending)
  })

答案2

得分: 2

等待代码运行后再测试或记录数组。

Cypress不会立即(同步地)提供结果。在您的代码中,您忽略了异步操作符.then()

let Arr1 = new Array() 
cy.get('selector')
  .each(($el) => {Arr1.push($el.text()) }) 
  .then(() => {
    cy.log(Arr1)
    cy.wrap(Arr1).should('equal', Arr1.sort())
  })
英文:

Wait for the code to run before testing or logging the array.

Cypress does not immediately (synchronously) give you the result. In your code you have missed the asynchronous operator .then().

let Arr1 = new Array() 
cy.get('selector')
  .each(($el) => {Arr1.push($el.text()) }) 
  .then(() => {
    cy.log(Arr1)
    cy.wrap(Arr1).should('equal', Arr1.sort())
  })

答案3

得分: 2

你不能使用 eq 来测试数组,那只适用于单个值。你必须使用 deep.eq

页面:

<ascending>
  <span>a</span>
  <span>b</span>
  <span>c</span>
</ascending>

<descending>
  <span>c</span>
  <span>b</span>
  <span>a</span>
</descending>

测试:

it('ascending - passing', () => {
  let arr1 = new Array() 
  cy.get('ascending span')
    .each($el => arr1.push($el.text() ) 
    .then(() => {
      cy.log(arr1)
      cy.wrap(arr1).should('deep.equal', [...arr1].sort())
    })
})

it('descending - passing', () => {
  let arr2 = a new Array() 
  cy.get('descending span')
    .each($el => arr2 push($el.text() )
    .then(() => {
      cy.log(arr2)
      cy.wrap(arr2).should('deep.equal', [...arr2].sort().reverse())
    })
})

it('descending - failing', () => {
  let arr3 = new Array() 
  cy.get('descending span')
    .each($el => arr3.push($el.text() ) 
    .then(() => {
      cy.log(arr3)
      cy.wrap(arr3).should('deep.equal', [...arr3].sort()) 
    })
})

结果:

Cypress 测试日志

[Cypress如何检查表中的值是升序还是降序。]

英文:

You cannot test an array with eq, that is only for single values. You must use deep.eq.

The page:

<ascending>
  <span>a</span>
  <span>b</span>
  <span>c</span>
</ascending>

<descending>
  <span>c</span>
  <span>b</span>
  <span>a</span>
</descending>

The test:

it('ascending - passing', () => {
  let arr1 = new Array() 
  cy.get('ascending span')
    .each($el => arr1.push($el.text() ) 
    .then(() => {
      cy.log(arr1)
      cy.wrap(arr1).should('deep.equal', [...arr1].sort())
    })
})

it('descending - passing', () => {
  let arr2 = new Array() 
  cy.get('descending span')
    .each($el => arr2.push($el.text() )
    .then(() => {
      cy.log(arr2)
      cy.wrap(arr2).should('deep.equal', [...arr2].sort().reverse())
    })
})

it('descending - failing', () => {
  let arr3 = new Array() 
  cy.get('descending span')
    .each($el => arr3.push($el.text() ) 
    .then(() => {
      cy.log(arr3)
      cy.wrap(arr3).should('deep.equal', [...arr3].sort()) 
    })
})

The result:

Cypress test log

[Cypress如何检查表中的值是升序还是降序。]

huangapple
  • 本文由 发表于 2023年3月31日 23:52:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定