首页 > 解决方案 > 如何在柏树中更新别名

问题描述

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

这就是我在逻辑上的意思:

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

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

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

cy.get("@priceValue").then((priceValue) => {
        expect(priceValue).contains("/");
        math.fraction(priceValue);
})

标签: javascriptcypress

解决方案


您也可以只更改它,无需新别名

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

推荐阅读