1

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?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
mismas
  • 1,236
  • 5
  • 27
  • 55
  • Is `ACC` a property on the `window` object? If so, perhaps you're looking for this: https://stackoverflow.com/questions/12709074/how-do-you-explicitly-set-a-new-property-on-window-in-typescript – Nicholas Tower Oct 15 '22 at 14:45
  • @NicholasTower it is in my script tag of the page, not sure if that qualifies as a property of the window object. Do you think it is part of the window object? – mismas Oct 15 '22 at 15:35

2 Answers2

2

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;
});
Vishnu Vinod
  • 603
  • 4
  • 15
0

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");
ggorlen
  • 44,755
  • 7
  • 76
  • 106