skip to Main Content
    cy.get('#name').type('Mani')

    var Name =cy.get('#name').text

    cy.log(Name)

    cy.log(Name+ 'Hello,  this practice page and share your knowledge')

I stored the name in the ‘Name’ variable and display it properly in cy.log(Name). But when I am trying to concatenate with variable(Name) it’s getting unefined.

I want to be display like "John Hello…."

2

Answers


  1. Try this

    cy.get('#name')
      .invoke('val')
      .then((val) => {
        const msg = `${val} Hello ...`;
        cy.log(msg);
        console.log(val + ' Hello'); // if you must
        debugger; // use your cypress browser dev tools console to experiment
      })
    
    Login or Signup to reply.
  2. Please read the documentation at Variables and Aliases.

    The standard way to handle variables in Cypress tests is via an alias. The syntax you need is cy.get(something).as('myVar')

    In your example

    cy.get('#name').type('Mani')
    
    cy.get('#name').invoke('val').as('value')
    
    cy.get('@value').then((value) => {
      cy.log(value + 'Hello')
    })
    
    // in an automated test, use should() to check the value
    
    cy.get('@value').should('eq', 'Mani')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search