Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Define text value of class as global var, so it can be yield later in Cypress

I want to save text value of class defined inside object as global variable, so it can be yielded outside that object.

What I mean on example – I got:

cy.get(".cat").eq(0).then(text => { let catTitleText = text; cy.wrap(catTitleText).as('catTitleText')});

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

And I want to use this let catTitleText outside object for URL comparison, so like this:

cy.url().should('eq', Cypress.config().urlLive+'faq.html?category='+catTitleText+[...]

But Cypress will throw an error that catTitleText is not defined, which makes sense.

The only solution I’ve found needs invoking it, like this for example:

cy.get('@catTitleText').then((text) => { function })

But reason I don’t want to do this way, is that I want to have multiple variables used inside cy.url() function, so my thinking is to define each variable globally, so those can be invoked in the same manner in cy.url().

Maybe it can be done using invoking but different way in cy.url(), so multiple objects can be yield inside? Sorry if that description is chaotic or I mistaken something, I tried my best to describe it properly.

>Solution :

You can use the Cypress.env() for saving the text and then it can be used throughout your project.

cy.get('.cat')
  .eq(0)
  .invoke('text')
  .then((text) => {
    Cypress.env('catTitleText', text) //Saving the cat title
  })

//Validate URL using Cypress.env
cy.url().should(
  'eq',
  Cypress.config().urlLive + 'faq.html?category=' + Cypress.env('catTitleText')
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading