Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

478
Views
Cypress Best Practice - Store and compare two values

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
describe('Store and compare a value', () => {
    it('store and compare', () => {

        cy.login()
        cy.visit('url2')
        cy.get('.total-count-results').invoke('text')
            .then((text) => {
                const counts = text 
                cy.get('.medium.col100 > .filterwrapper > input').type('Test Dummy',{force: true})
                cy.get('.medium.col100 > .filterwrapper > input').type('{enter}')
                cy.get('.total-count-results').invoke('text')
                    .then((text) => {
                        const new_counts = text
                        expect(new_counts).to.eq(counts)
                    })
            })
    })
})

That is the best I could come up with to handle asynchronicity.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I don't think aliases are required. Here is my solution that I tested locally. I kept most of the code in Alapan Das' answer so it's easier to compare. To me, it seems more concise and easier to read without aliases.

describe('Store and compare a value', () => {
    it('store and compare', () => {  
        cy.login()
        cy.visit('url2')
        cy.get('.total-count-results').invoke('text')
            .then((previousText) => {
                cy.get('.medium.col100 > .filterwrapper > input').type('Test Dummy',{force: true})
                cy.get('.medium.col100 > .filterwrapper > input').type('{enter}')
                cy.get('.total-count-results').invoke('text').should("eq", previousText)
            })
    })
})
about 4 years ago · Juan Pablo Isaza Report

0

You can use aliases for this and do something like this:

describe('Store and compare a value', () => {
  it('store and compare', () => {
    cy.login()
    cy.visit('url2')
    cy.get('.total-count-results').invoke('text').as('counts')
    cy.get('.medium.col100 > .filterwrapper > input').type('Test Dummy', {
      force: true,
    })
    cy.get('.medium.col100 > .filterwrapper > input').type('{enter}')
    cy.get('.total-count-results').invoke('text').as('new_counts')
    cy.get('@counts').then((counts) => {
      cy.get('@new_counts').then((new_counts) => {
        expect(new_counts).to.eq(counts)
      })
    })
  })
})
about 4 years ago · Juan Pablo Isaza Report

0

A good solution for comparing values like this can be to use a closure variable, as per the example in Cypress' documentation at https://docs.cypress.io/api/commands/should#Compare-text-values-of-two-elements. In your example, something like this:

describe('Store and compare a value', () => {
    it('store and compare', () => {

        let counts // closure variable
        cy.login()
        cy.visit('url2')
        cy.get('.total-count-results').invoke('text')
            .then(text => counts = text)     // set closure variable

        cy.get('.medium.col100 > .filterwrapper > input').type('Test Dummy',{force: true})
        cy.get('.medium.col100 > .filterwrapper > input').type('{enter}')

        cy.get('.total-count-results').invoke('text')
            .then(new_counts => expect(new_counts).to.eq(counts))   // compare closure variable
            //.should("eq",counts)    // alternative to .then clause
    })
})

This avoids having nested '.then' clauses, which can become like callback hell, and is less verbose.

(See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures for background on closures: in short, "a closure gives you access to an outer function's scope from an inner function". The inner functions here are the two .then functions: they access the counts variable from the outer it function's scope.)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!