首页 > 解决方案 > 使用 Testcafe 访问自定义窗口属性

问题描述

所以在我们的应用程序中,我们有一个名为 reactMap 的自定义窗口属性。因此,当您加载我们的站点并转到控制台并输入“window.reactMap.loaded()”时,它将返回 true 或 false。但是,当我尝试在 TestCafe 测试中添加以下内容时:

const mapLoaded = ClientFunction(() => window.reactMap.loaded());

它只是抱怨Property 'reactMap' does not exist on type 'Window'. 我如何在 TestCafe 测试中运行那段代码?谢谢

标签: javascriptautomated-testse2e-testingweb-testingtestcafe

解决方案


这意味着您ClientFunction在应用程序初始化window.reactMap属性之前执行。尝试如下修改您的测试代码:

const waitForProperty = ClientFunction(() => {
     return new Promise(function (resolve, reject){
         var intervalId = null;
         var timeoutId = null;
         var checkCondition = function () {
              return window.reactMap;
         }

         timeoutId = window.setTimeout(function () {
             window.clearInterval(intervalId);            

             if (checkCondition())
                resolve();
             else
                reject();
         }, 10000);
         intervalId = window.createInterval(function (){
            if (checkCondition()) {
                window.clearInterval(intervalId);
                resolve();
            }
         }, 1000);
     });
});


await waitForProperty();

const mapLoaded = await ClientFunction(() => window.reactMap.loaded())();


推荐阅读