我们可以用 Promise 替换 Async/Await

sri*_*nth -1 javascript testing promise async-await testcafe

我们可以在下面的代码中使用 Promise 而不是 async/await 吗?


fixture`MyFixture`.page`http://devexpress.github.io/testcafe/example`;

test("My first test", async t => {
  await t
    .typeText("#developer-name", "srikanth chitla")
    .setTestSpeed(0.1)
    .click("#submit-button");
});
Run Code Online (Sandbox Code Playgroud)

And*_*lym 5

是的,您可以使用 Promise 链代替async/await,但这种方式更加冗长:


fixture`MyFixture`
    .page`http://devexpress.github.io/testcafe/example`;

test("My first test", t => {
  return t
    .typeText("#developer-name", "srikanth chitla")
    .then(() => t.setTestSpeed(0.1))
    .then(() => t.click("#submit-button"));
});
Run Code Online (Sandbox Code Playgroud)

如果您需要使用async/await本机不支持的 Node.js 版本运行测试,请不要担心:TestCafe在底层使用Babel将 ES6 功能转换为可由任何 Node.js 版本执行的代码。