【问题标题】:How to insert String variable into the String inside cy.get()如何将字符串变量插入到 cy.get() 中的字符串中
【发布时间】:2021-12-18 20:20:48
【问题描述】:

我正在将 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})

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

【问题讨论】:

    标签: typescript ecmascript-6 automated-tests cypress


    【解决方案1】:

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

    你的代码应该是:

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

    【讨论】:

      猜你喜欢
      • 2019-02-08
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 2017-03-02
      相关资源
      最近更新 更多