I want to store a value, then perform an action and assert that the value has not changed. I do have a code that works but I would like to have input if there is a more elegant solution.
The basic idea is:
- Get the number or results displayed (‘counts’), store it in a .then() function
- Change the use
- Get the number of results displayed (‘new_counts’), store it in a new .then function
- Compare counts and new_counts in the 2nd .then() function
JavaScript
x
19
19
1
describe('Store and compare a value', () => {
2
it('store and compare', () => {
3
4
cy.login()
5
cy.visit('url2')
6
cy.get('.total-count-results').invoke('text')
7
.then((text) => {
8
const counts = text
9
cy.get('.medium.col100 > .filterwrapper > input').type('Test Dummy',{force: true})
10
cy.get('.medium.col100 > .filterwrapper > input').type('{enter}')
11
cy.get('.total-count-results').invoke('text')
12
.then((text) => {
13
const new_counts = text
14
expect(new_counts).to.eq(counts)
15
})
16
})
17
})
18
})
19
That is the best I could come up with to handle asynchronicity.
Advertisement
Answer
You can use aliases for this and do something like this:
JavaScript
1
18
18
1
describe('Store and compare a value', () => {
2
it('store and compare', () => {
3
cy.login()
4
cy.visit('url2')
5
cy.get('.total-count-results').invoke('text').as('counts')
6
cy.get('.medium.col100 > .filterwrapper > input').type('Test Dummy', {
7
force: true,
8
})
9
cy.get('.medium.col100 > .filterwrapper > input').type('{enter}')
10
cy.get('.total-count-results').invoke('text').as('new_counts')
11
cy.get('@counts').then((counts) => {
12
cy.get('@new_counts').then((new_counts) => {
13
expect(new_counts).to.eq(counts)
14
})
15
})
16
})
17
})
18