如何更新 cypress 中的别名

Bru*_*dit 2 javascript cypress

我不确定我是否在 cypress 中正确使用了它。我想要做的是更新我的别名@priceValue,以便稍后可以使用更新后的别名。

这就是我逻辑上的意思:

1:获取文本并为其指定别名priceValue 2:检查价格值以确保它包含字符串,然后(这就是问题)-> 通过转换将别名更新为分数。字符串转分数

如何更新别名,使其现在成为转换后的分数?

priceElements.priceButton().first().invoke("text").as("priceValue");

cy.get("@priceValue").then((priceValue) => {
        expect(priceValue).contains("/");
        math.fraction(priceValue);
})
Run Code Online (Sandbox Code Playgroud)

Mic*_*nes 8

不需要新的别名,只需更改旧的别名即可。

priceElements.priceButton().first().invoke("text")
  .should('contain', '/')
  .as("priceValue")

cy.get("@priceValue")
  .then(priceValue => math.fraction(priceValue))  // modify
  .as("priceValue")                              // re-save

cy.get("@priceValue")
  .invoke('toString')
  .should('not.contain', '/')                    // different value
Run Code Online (Sandbox Code Playgroud)

  • 简单的解决方案。感谢分享! (3认同)