Tejas Uperiya
Tejas Uperiya

Reputation: 41

.text() method returns [object Object] instead of text in cypress test

// should store text in "person" variable
const person = cy.get(' div.global-user-nav-menu > div > div > div > div.info-container > div.display-name').text() 

I want to use these "person" variables as a function argument

SpaceView.assignTo(person)

Upvotes: 2

Views: 5426

Answers (1)

Srinu Kodi
Srinu Kodi

Reputation: 512

Cypress architecture works on promise chaining and you cannot break the chain and return values out of it... You have to chain the return values and act on it. It doesn't work on like selenium getText() method :) So your below statement will not work.

const personName = cy.get(`div.global-user-nav-menu > div > div > div > div.info-container > div.display-name`).text();
SpaceView.assignTo(personName);

So you have to construct as below, in order to make it work

cy.get(`div.global-user-nav-menu > div > div > div > div.info-container > div.display-name`).then(element => {
      SpaceView.assignTo(element.text());
    });

(or)

    return cy.get(`div.global-user-nav-menu > div > div > div > div.info-container > div.display-name`).then(ele => {
      return ele.text()
    }).then(personName => {
      SpaceView.assignTo(personName);
    });

Upvotes: 6

Related Questions