【问题标题】:Asynchronicity, and variable overrides in Cypress赛普拉斯中的异步性和变量覆盖
【发布时间】:2021-07-13 09:12:02
【问题描述】:

我的问题是,您如何覆盖超出此范围的异步函数中的变量? 我读到here 说问题是缺少回调。添加回调后,更改它的范围之外的变量(但变量本身仍然在正确的范围内)返回“未定义”。我做错了什么?

测试:

const savedVariableCallback = (variable) =>
  console.log(`Variable saved ${variable}`);

describe(() => {
      ...
      it("Sample input type", () => {
         let fixValue;
         cy.fixture("example.json").then(({ email }) => {
            actionsPage
            .selectSampleInput()
            .then((input) => {
               checkAmountOfElements(input, 1);
               checkVisiblity(input);
            })
            .type(email)
            .then((input) => {
               checkIfValue(input, email);
               fixValue = "Nice work";
               savedVariableCallback(fixValue);
            });
         });
         cy.log(`fixValue is: ${fixValue}`);
     });
})

我希望第一个日志显示Variable saved Nice work,第二个日志显示fixValue is: Nice work 变量。但是现在,我进入第一个日志Variable saved Nice work,但在第二个日志中我得到undefined。 我希望在 it() 方法范围内可以访问该变量。

【问题讨论】:

    标签: javascript asynchronous callback cypress


    【解决方案1】:

    编辑:由于参考不起作用,我建议使用allias 接近它

    const savedVariableCallback = (variable) =>
        console.log(`Variable saved ${variable}`);
    
    describe(() => {
    ...
        it("Sample input type", () => {
            cy.fixture("example.json").then(({ email }) => {
                actionsPage
                    .selectSampleInput()
                    .then((input) => {
                        checkAmountOfElements(input, 1);
                        checkVisiblity(input);
                    })
                    .type(email)
                    .then((input) => {
                        checkIfValue(input, email);
    
                        let fixValue = "Nice work";
                        savedVariableCallback(fixValue);
                        cy.wrap(fixValue).as('fixValue')
                    });
            });
            cy.get('@fixValue')
                .then(fixValue => {
                    cy.log(`fixValue is: ${fixValue.value}`);
                })
        });
    })
    

    【讨论】:

    • 嗯,在那一秒cy.log() 中我仍然不确定。它和你一样改变了。
    • 在执行函数 savedVariableCallback 时是否发生了变化?
    • 不,它控制台记录适当的变量Variable saved Nice work
    • 如果其他人分享这样的方式,我会很高兴。赛普拉斯就像对正常变量过敏:)
    • “Cypress 对正常变量过敏” - 很有诗意!
    【解决方案2】:

    如果你改变了

    cy.log(`fixValue is: ${fixValue}`)
    

    console.log(`fixValue is: ${fixValue}`)
    

    你可以看到记录的顺序

    • fixValue 为:未定义
    • 变量保存得很好

    所以cy.log() 在命令添加到队列时获取fixValue 的值,即在cy.fixture() 运行之前,即使它实际上在cy.fixture() 之后运行。

    您可以通过添加另一个.then() 将其推迟到cy.fixture() 完成

    cy.fixture("example.json").then(data => {
      ...
    })
    .then(() => {
      // enqueued after the fixture code
      cy.log(`fixValue is: ${fixValue}`)     // now it sees the changed value
    })
    

    当然,所有需要使用fixValue 的下游都必须在.then() 回调中。

    你也可以推迟对价值的“抢夺”

    cy.then(() => cy.log(`fixValue is: ${fixValue}`))
    

    或将cy.fixture() 拆分为before(),这将在测试开始之前解析变量

    let fixValue;
    
    before(() => {
      cy.fixture("example.json").then(data => {
        ...
        fixValue = ...
      })
    })
    
    it('tests', () => {
      cy.log(`fixValue is: ${fixValue}`)     // sees the changed value
    })
    

    【讨论】:

    • 但是如果我想在另一个测试/部分测试中使用变量而不将所有内容嵌套在一个then 中怎么办?我知道从 ISTQB 的角度来看这是一个糟糕的模式。我给出的是一个简单的例子。在现实生活中的应用程序中,我们在其中一个测试的输入中生成数据(因此之前不能使用),但随后我们在整个测试文件中使用它。相同的原则 - 我从请求中得到一些东西,想将它保存到一个变量中,但是我不能使用它,因为在 then 内部调用了保存变量
    • 哈哈,这就是异步世界的生活。在您在那里提到的大多数情况下,您不会有问题。
    猜你喜欢
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2022-07-22
    • 2017-01-28
    • 1970-01-01
    相关资源
    最近更新 更多