Home > Software engineering >  TypeScript cannot find name of window property in Playwright evaluate callback
TypeScript cannot find name of window property in Playwright evaluate callback

Time:10-16

I am using Typescript with Playwright. I have this code to evaluate JavaScript in the browser:

const CSRFToken = await this.page.evaluate(() => {
  return ACC.config.CSRFToken;
});

But I get an error: Cannot find name 'ACC'.ts(2304).

ACC.config.CSRFToken is something that exists on my page in the script tag of the DOM.

How can I resolve this?

CodePudding user response:

You can declare a variable ACC in your typescript file and use that variable like following.

declare const ACC: any;
const CSRFToken = await this.page.evaluate(() => {
    return ACC.config.CSRFToken;
});

CodePudding user response:

One approach that works nicely for this particular case is to pass a string as the function body:

const CSRFToken = await this.page.evaluate("ACC.config.CSRFToken");
  • Related