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

How to insert String variable into the String inside cy.get()

I am using Cypress with Typescript and es6:

enter image description here

and I have defined const inside a test:

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

const folderName = "Test"

I tried to insert that folderName into the String inside cy.get() using String Substitution according to this documentation.

cy.get('[name="folder-name-${folderName}"]').next().click({force: true})

But apparently I am doing something wrong:

enter image description here

I managed to have it working by doing this:

cy.get('[name="folder-name-' + folderName + '"]').next().click({force: true})

But I think there are cleaner ways to do it. What am I missing here?

>Solution :

You’re looking for Template literals (Template string).
It is used with backticks.

Your code should be:

cy.get(`[name="folder-name-${folderName}"]`).next().click({force: true})
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