首页 > 解决方案 > 在功能测试中链接 Promises/Observable 的最优雅方式

问题描述

这是我正在处理的项目中现有测试的示例:

AppPage.lastnameInput().clear().then(function () {
  AppPage.lastnameInput().sendKeys(lastname).then(function () {
    AppPage.firstnameInput().clear().then(function () {
      AppPage.firstnameInput().sendKeys(firstname).then(function () {
        AppPage.ibanInput().clear().then(function () {
          AppPage.ibanInput().sendKeys(IBAN).then(function () {
            $('body').click().then(function () {
              callback();
            });
          });
        });
      });
    });
  });
});

在我看来,这可能会变平。就像是 。

foo(
  AppPage.lastnameInput().clear(),
  AppPage.lastnameInput().sendKeys(lastname),
  AppPage.firstnameInput().clear(),
  AppPage.firstnameInput().sendKeys(firstname),
  AppPage.ibanInput().clear(),
  AppPage.ibanInput().sendKeys(IBAN),
  $('body').click(),
).then(() => callback())

我试过forkJoin()了,但似乎在执行下一个 Observable 之前它不会等待第一个 Observable 完成。

我可以自己实现这样的功能,但它看起来很通用,所以我想知道是否已经存在更标准的功能。

标签: angulartypescriptprotractorobservable

解决方案


您可以使用该concat()函数按顺序执行多个可观察对象(而不是并行执行forkJoin())。


推荐阅读