【问题标题】:How to get a constructor function from the window object in TestCafe?如何从TestCafe中的window对象获取构造函数?
【发布时间】:2019-06-25 23:12:26
【问题描述】:

在我的 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)
});

【问题讨论】:

    标签: javascript automated-tests e2e-testing web-testing testcafe


    【解决方案1】:

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

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

    cosnt getStaticData = ClientFunction(() => {
       const data = window.Sortable.staticMethod();
    
       return JSON.serializable(data);
    });
    
    

    【讨论】:

    • 这正是我所做的。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 2010-11-23
    • 2018-02-23
    • 2020-01-29
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多