获取一个字符串并将其转换为变量并分配给柏树中的变量

get a string and convert it to a variable and assign to a variable in cypress

您好,我想获取这个值,将其转换为数字并分配为变量。然后我可以做计算。有人可以帮忙吗?

试过这个不行。

const marks = cy.get('.ant-typography \> span').invoke('text').then(parseInt).should('be.gt', 10)

cy.log('The value is '+ marks)

.ant-typography > span // 这是定位器

请阅读 Cypress 命令 https://docs.cypress.io/guides/core-concepts/introduction-to-Cypress#Commands-Are-Asynchronous 具体针对您的情况,请观看 https://www.youtube.com/watch?v=-aptS3yvqcc

您可以像这样使用别名来保存变量中的值:

cy.get('.ant-typography > span').invoke('text').as('marks')

cy.get('@marks').then((marks) => {
  cy.log('The value is ' + marks) //logs marks
  cy.wrap(+marks).should('be.gt', 10)
})

您需要使用.then()访问变量

cy.get('.ant-typography > span')
  .invoke('text')
  .then(parseInt)
  .should('be.gt', 10)
  .then(marks => {
    cy.log('The value is '+ marks)
  })

我可以 return 这个“标记”值以便我可以在 .then() 方法之外使用它吗?

查看 Gleb 参考文档,一旦你有一个异步命令(即使只是一个 .get()),你几乎无法使用 .then() 来访问从它们派生的值。

即使是别名也无济于事 - 您需要 .then() 才能获得它的价值。