量角器.ts:如何使用.then(),. catch()进行无故障处理,无论是否出现故障?

时间:2017-07-14 00:42:43

标签: typescript jasmine protractor

我正在尝试通过可能出现或可能不出现的对话框。这是一个功能:

当我在Jasmine / Protractor中的一个块中运行它时,catch会运行..." FALSE" ...

Jasmine完成测试,然后失败并显示"错误:超时 - 在jasmine.DEFAULT_TIMEOUT_INTERVAL"

指定的超时时间内未调用异步回调

我只能假设这是因为Jasmine / Protractor有点太聪明了。我正在寻找相当于Java Webdriver的等价物 - 尝试/抓住等待并继续愉快地继续前进。

请注意,当按钮出现时,测试没有问题。 " TRUE"

let okButton: ElementFinder = element(by.buttonText("OK"));
await browser.wait(EC.visibilityOf(okButton)).then(() => {
    console.log("TRUE");
    okButton.click();
}).catch((error) => {
    console.log("FALSE");
})

2 个答案:

答案 0 :(得分:0)

您需要使用done回调:

e.g。

it('handles a promise', done => {
   promise
      .then(result => expect(result).toEqual(expected))
      .catch(error => expect(error).toBeUndefined())
      .finally(done)
})

答案 1 :(得分:0)

看起来应该是这样的

describe("async function", function() {
  it("should not fail", async function(): Promise < any > {
    const okButton: ElementFinder = element(by.buttonText("OK"));

    try {
      await browser.wait(EC.visibilityOf(okButton));
      console.log("TRUE");
      await okButton.click();
    } catch (e) {
      console.log("FALSE");
    }
  });
});

另请参阅hereprotractor docs