英文:
Cypress how to check if the values in the table is in ascending order or descending order
问题
- 如何验证表格中的值是否按升序或降序排序?
- 如何使用两个函数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 测试日志
[]
英文:
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
[]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论