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:

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})

Leave a Reply