Access custom window properties with Testcafe

Viewed 3587

So in our application, we have a custom window property called reactMap. So when you load our site and go to the console and type in "window.reactMap.loaded()" it will return true or false. But when I tried to do this in a TestCafe test by adding:

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

it just complains that Property 'reactMap' does not exist on type 'Window'. How do I run that bit of code in a TestCafe test? thanks

1 Answers

It means you execute ClientFunction before your application initializes the window.reactMap property. Try to modify your test code as follows:

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())();

Related