【问题标题】:TestCafe: awaiting ClientFunction to resolve in expect method leads to unexpected behaviorTestCafe:在期望方法中等待 ClientFunction 解决会导致意外行为
【发布时间】:2021-07-07 13:08:08
【问题描述】:

我目前面临一个问题,即在断言查询链中等待 TestCafe ClientFunction 会导致以下代码演示的行为:

const getPageUrl = ClientFunction(() => window.location.href.toString());

// This test works fine
test("Test that works as expected", async t => {
  await t
    .click(pageElements.someNaviButton) // navigates me to the site with url extension /some/url
    .expect(getPageUrl()).contains("/some/url")
});

// This test works fine as well
test("Another test that works as expected", async t => {
  await t.click(pageElements.someNaviButton) // navigates me to the site with url extension /some/url
  const myPageUrl = await getWindowLocation();
  await t.expect(myPageUrl).contains("/some/url")
});

// This test fails
test("Test that does not work as expected", async t => {
  await t
    .click(pageElements.someNaviButton)
    .expect(await getPageUrl()).contains("/some/url")
});

根据TestCafe's documentation,必须等待异步ClientFunctions。这使我感到恼火:我希望第二次测试通过,它确实通过了。在第三个测试的expect方法中等待封装ClientFunction的方法似乎有问题,但这是为什么呢?

可能感兴趣的其他信息:我使用的是 TestCafe 版本 1.14.0。

【问题讨论】:

    标签: javascript testing async-await e2e-testing testcafe


    【解决方案1】:

    请考虑测试操作链是单个表达式。所以,在第三个测试中,getPageUrl 是在链执行之前计算出来的。那时,测试仍在起始页面上。 第一个测试通过了,因为 getPageUrl 是通过 TestCafe Smart Assertion Query Mechanism 懒惰地计算出来的。这是编写此类测试的最合适的方法。 第二个测试通过了,因为您破坏了测试操作链。

    【讨论】:

      最近更新 更多