在我的 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
});
test('test', async t => {
let Sortable = await t.eval(() => window.Sortable);
console.log(Sortable); // Logs undefined (not sure why)
});
最佳答案
I need to get the constructor function for a library I am using in order to call a static method on it.
这是不可能的。您不能在执行测试的 Node.js 环境中从浏览器的 JavaScript 环境调用函数。
要完成您的方案,请在 ClientFunction 中调用目标静态方法并从中返回结果。
cosnt getStaticData = ClientFunction(() => {
const data = window.Sortable.staticMethod();
return JSON.serializable(data);
});
关于javascript - 如何从TestCafe中的window对象获取构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56763148/