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

标签 javascript automated-tests e2e-testing web-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
});

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/

相关文章:

javascript - javascript中多个匹配项的正则表达式组

javascript - jQuery slideToggle - 仅 slideUp Action 的回调函数

javascript - 如何*何时*延迟数组

node.js - 一个文件中的多个运行者仅产生第一个运行者的结果

selenium - 如何减少执行 Selenium 测试用例的时间?

python - pytest -> 如何在类下的测试方法中使用 fixture 返回值

node.js - 如何使 "npm run start"退出并显示代码 0?

php - Javascript 函数和 PHP session 变量

testing - TestCafe:指定选择器的问题:跨度抓取文本

javascript - Test Cafe - 如何修改数据或将数据注入(inject)响应?