如何从 TestCafe 中的 window 对象获取构造函数?
How to get a constructor function from the window object in 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
});
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 中的目标静态方法并 return 它的结果。
cosnt getStaticData = ClientFunction(() => {
const data = window.Sortable.staticMethod();
return JSON.serializable(data);
});
在我的 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 中的目标静态方法并 return 它的结果。
cosnt getStaticData = ClientFunction(() => {
const data = window.Sortable.staticMethod();
return JSON.serializable(data);
});