如何将字符串变量插入 cy.get() 中的字符串

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

我正在将 Cypress 与 Typescript 和 es6 一起使用:

并且我在测试中定义了 const:

const folderName = "Test"

我尝试根据此 documentation.

使用字符串替换将 folderName 插入到 cy.get() 内的字符串中
cy.get('[name="folder-name-${folderName}"]').next().click({force: true})

但显然我做错了什么:

我通过这样做设法使它工作:

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

但我认为有更简洁的方法可以做到这一点。我在这里错过了什么?

您正在寻找 Template literals (Template string)。 它与反引号一起使用。

您的代码应该是:

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