我们可以覆盖 testcafe 的 TestController 的 'expect' 方法吗

Can we overwrite 'expect' method of testcafe's TestController

我正在寻找一种方法来覆盖 TestController 的 expect 方法。我的想法是使用 t.expect 方法的现有测试,我想在这些情况下执行额外的步骤。 我想出了下面的示例代码,但 testcafe 运行时失败并出现以下错误 TypeError: Cannot read property '_expect$' of undefined

尝试覆盖的示例代码:

import { Selector } from "testcafe";

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

test("My first test", async (t) => {
  t = modify(t);
  await t.typeText("#developer-name", "John Smith").click("#submit-button");

  // Use the assertion to check if the actual header text is equal to the expected one
  await t
    .expect(Selector("#article-header").innerText)
    .eql("Thank you, John Smith!");
});

function modify(t) {
  let prevExpect = t.expect;
  t.expect = (param) => {
    console.log("modified expecte has been used");
    return prevExpect(param);
  };
  return t;
}

此外,当使用 t.click(Selector(...).expect(...) 时,它不会使用我覆盖的预期。如何让它在调用链中也起作用?

从技术上讲,可以覆盖 expect 方法,但请注意,这种方法可能会导致工作不正确和意外错误。 您需要按如下方式修改 modify 函数:

function modify (t) {
    let prevExpect = t.expect;

    t.expect       = (param) => {
        console.log("modified expect has been used");

        return prevExpect.call(t, param);
    };
    return t;
}

关于t.click(Selector(...).expect(...)问题,你调用了Selector的expect方法,但是Selector没有expect方法。 您需要在 Selector:

之后添加 )

await t.click(Selector(...)).expect(...)