如何从TestCafe中的window对象获取构造函数?

Owe*_*n M 4 javascript automated-tests web-testing e2e-testing testcafe

在我的testcafe测试中,我需要获取我正在使用的库的构造函数,以便对其调用静态方法。

但是,我无法使用给定的ClientFunction和Eval方法来执行此操作。我该如何获取构造函数?

我尝试了以下方法:

// Does not work, because the docs say it only allows using ClientFunction for obtaining "serializable" values

let getSortable = new ClientFunction(() => window.Sortable);
test('test', async t => {
    let Sortable = await getSortable();
    console.log(Sortable); // Logs undefined
});
Run Code Online (Sandbox Code Playgroud)
test('test', async t => {
    let Sortable = await t.eval(() => window.Sortable);
    console.log(Sortable); // Logs undefined (not sure why)
});
Run Code Online (Sandbox Code Playgroud)

mlo*_*sev 5

我需要为正在使用的库获取构造函数,以便在其上调用静态方法。

那是不可能的。您不能在执行测试的Node.js环境中从浏览器的JavaScript环境中调用函数。要完成您的方案,请在ClientFunction中调用目标静态方法,并从中返回结果。

cosnt getStaticData = ClientFunction(() => {
   const data = window.Sortable.staticMethod();

   return JSON.serializable(data);
});

Run Code Online (Sandbox Code Playgroud)