【问题标题】:Can not pass variable from method to the test - protractor无法将变量从方法传递给测试 - 量角器
【发布时间】:2016-03-10 17:07:37
【问题描述】:

我正在编写一个测试来检查我拥有的黄金数量。我正在使用 typecrypt 和量角器。

我有一个 GetAmoutOfChips 方法:

public static GetAmountOfChips(): PromiseLike<any> {
    let chips: number = 1;
    NavigateTo.myProfile();
    browser.sleep(900);
    let promise = MyProfile.Basic.chipsAmount.getText().then((chipAmount) => {
        chips = parseInt(chipAmount);
        console.log("+++++++++++1" + chips);
    });
    MyProfile.Basic.close.click();
    console.log("+++++++++++2" + chips);
    return promise;
}

我想在测试中使用它。所以我正在做以下事情:

Actions.Basic.GetAmountOfChips().then((chipAmount: number) => {
            chipsBalance = chipAmount;
            console.log("+++++REAL" + chipsBalance);
        });

console.log#1 返回我想要使用的值。 console.log#2 返回不明。所以我不能将变量传递给测试。我该怎么做才能将变量从方法传递给测试?

【问题讨论】:

  • 你需要使测试异步。
  • 任何帮助如何做到这一点?

标签: typescript promise protractor


【解决方案1】:

您必须在 then 内完成所有操作。您还可以在此处返回值以创建另一个承诺。

public static GetAmountOfChips(): PromiseLike<any> {
    NavigateTo.myProfile();
    browser.sleep(900);
    let promise = MyProfile.Basic.chipsAmount.getText().then((chipAmount) => {
        // this is executed asynchronously
        MyProfile.Basic.close.click();

        // return a value to create another promise        
        return parseInt(chipAmount);

    });

    return promise;
}

Promise 可能很混乱,您可以了解更多 here

在您的测试中,您还必须在 then 函数中进行检查。

Actions.Basic.GetAmountOfChips().then((chipAmount: number) => {
  // test here not outside 
  expect(chipAmount).toBe(10);
});

【讨论】:

  • 非常感谢!它就像一个魅力!在过去的两天里,我在理解这一点时遇到了问题。
  • @Bakanomercy 你可以这样想,then 函数中的任何事情都可以在任何时间点发生,这意味着任何代码都不应该依赖它在设定的时间点发生。
猜你喜欢
  • 2016-04-20
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多