skip to Main Content

I have a weird issue where certain parts of code do not seem to execute. This test reads first column of last row in the table, edits this record, reads the value again, and checks if values have been changed.

/// <reference types="cypress" />

it("edit", () => {
    var lastValBef = 'uninitialized'
    var lastValAft = 'uninitialized'
    cy
    .visit("https://localhost:3000/")
    .get('tr').last().find('td').eq(0).then(elm => {
        lastValBef = elm.text()
      })
    .get('table').find('tr:last-child td:last-child a[href*="Edit"]').click()
    .get('input[id*="Title"]').type("changed")
    .get('input[class*="btn-default"]').click()
    .get('tr').last().find('td').eq(0).then(elm => {
        lastValAft = elm.text()
      })
    .wrap(lastValBef).should('not.equal', lastValAft)
})

Test goes properly and value gets changed, but test fails with time out trying to compare uninitialized with uninitialized, which implies that .then section gets ignored entirely. I have no idea what to do, everything looks to be in order.

2

Answers


  1. Due to the asynchronous nature of then both callbacks are not executed. The possible simplest solution I can think of is with the nesting callbacks within then.

    But since cypress runs its commands serially I think the following may work, please check that out cuz I’m unsure about it.

        var lastValBef = 'uninitialized'
        var lastValAft = 'uninitialized'
    
        cy
        .visit("https://localhost:3000/")
        .get('tr').last().find('td').eq(0).then(elm => {
            lastValBef = elm.text()
          })
        
        cy
        .get('table').find('tr:last-child td:last-child a[href*="Edit"]').click()
        .get('input[id*="Title"]').type("changed")
        .get('input[class*="btn-default"]').click()
        .get('tr').last().find('td').eq(0).then(elm => {
            lastValAft = elm.text()
          })
        
        cy.wrap(lastValBef).should('not.equal', lastValAft)
    
    

    There’s great article about asynchronous nature of cypress https://www.toolsqa.com/cypress/cypress-asynchronous-nature/

    Edit:
    If that won’t work out, please try something like

        cy
        .visit("https://localhost:3000/")
        .get('tr').last().find('td').eq(0).then(firstElm => {
            const lastValBef = firstElm.text()
            
            cy
              .get('table').find('tr:last-child td:last-child 
    a[href*="Edit"]').click()
              .get('input[id*="Title"]').type("changed")
              .get('input[class*="btn-default"]').click()
              .get('tr').last().find('td').eq(0).then(secondElm => {
                const lastValAft = secondElm.text()
    
                cy.wrap(lastValBef).should('not.equal', lastValAft)
            })
        })
    
    
    Login or Signup to reply.
  2. In Cypress, you can’t access variables outside of a closure: You’re assigning the variables correctly, but you need to make sure they’re inside of a new scope like in avemike’s posted example, when doing the comparison.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search