Reputation: 317
I get element with class and find its children. I need to get count of children element and assigned to variable currentCount
.
let curentCount = cy.get(`.DayContainer:contains('${todaysDate}')`)
.first()
.children(".Day:not(.Day--disabled)")
.its('length')
cy.log(curentCount)
After there I make some actions and need to compare element counts in the end.
cy.get(`.DayContainer:contains('${todaysDate}')`)
.first()
.children(".Day:not(.Day--disabled)")
.its('length')
.should('eq', curentCount)
cy.log(curentCount)
But I usually received:
Object: {5}
And I cannot compare these values.
Upvotes: 1
Views: 1387
Reputation: 2431
The issue is that curentCount
is a Cypress object that represents the length of the children elements. To get the actual value of curentCount
, you need to use the .then()
method to extract the value from the Cypress object.
Here's an updated code snippet that should work:
let curentCount
cy.get(`.DayContainer:contains('${todaysDate}')`)
.first()
.children(".Day:not(.Day--disabled)")
.its('length')
.then((length) => {
curentCount = length
cy.log(curentCount)
})
// Later in your code...
cy.get(`.DayContainer:contains('${todaysDate}')`)
.first()
.children(".Day:not(.Day--disabled)")
.its('length')
.should('eq', curentCount)
.then((length) => {
cy.log(length)
})
This code sets curentCount
by extracting the length value from the Cypress object using .then()
. Then, later in the code, it compares the length of the children elements to curentCount and logs the length value.
Upvotes: 0
Reputation: 1
This example work fine for me :
// 'input' is the element that I want count
cy.get('input').then(($cnt) => {
const count = $cnt.length;
console.log(count);
// put the rest of the code here
});
Upvotes: 0
Reputation:
The way Cypress works is to pass values down the chain, but when you try to get let curentCount =...
it gives you a Chainer object.
How about saving to an alias
cy.get(`.DayContainer:contains('${todaysDate}')`)
.first()
.children(".Day:not(.Day--disabled)")
.its('length')
.as('initialCount')
// do your actions
cy.get(`.DayContainer:contains('${todaysDate}')`)
.first()
.children(".Day:not(.Day--disabled)")
.its('length')
.as('nextCount')
// compare
cy.get('@initialCount').then(initial => {
cy.get('@nextCount'). then(next => {
expect(initial).to.eq(next)
})
})
It still works the way you had it, but be aware that the variable is a Chainer and value must be accessed in a chain,
const currentCountChainer = cy.get(`.DayContainer:contains('${todaysDate}')`)
.first()
.children(".Day:not(.Day--disabled)")
.its('length')
// do your actions
cy.get(`.DayContainer:contains('${todaysDate}')`)
.first()
.children(".Day:not(.Day--disabled)")
.its('length')
.then(nextCount => {
currentCountChainer.should('eq', nextCount) // carry on the previous chain
})
This is not so conventional, but should work.
Upvotes: 3
Reputation: 18586
For saving the count
cy.get(`.DayContainer:contains('${todaysDate}')`)
.first()
.children(".Day:not(.Day--disabled)")
.its('length')
.as('count')
To validate the count -
cy.get('@count').then((count) => {
cy.get(`.DayContainer:contains('${todaysDate}')`)
.first()
.children(".Day:not(.Day--disabled)")
.its('length')
.should('eq', count)
})
Upvotes: 0