Afsal
Afsal

Reputation: 494

Pass multiple arguments to Page.evaluate()

I have the below Playwright-TS code. I am passing email id dynamically to the tester method and I want it inside page.evaluate but email:emailId in the below code errors out as undefined.

  apiData = {
    name: faker.name.firstName(),
    domain: process.env['DOMAIN'],
  };
async tester(page: Page, emailId: string)
    {
      await page.evaluate((apiData) =>
      { 
          const authentication = {
          userId: window.localStorage.getItem('userId')
        };

        const data = 
        {
            email:emailId
        };

        fetch(`${apiData.domain}/api/v1/user`, 
        {
          method: 'POST',
          headers: {
          'content-type': 'application/json',
           cookie: authentication.allCookies,
          },
          body: JSON.stringify(data),
          })
          .then(response => response.json())
          .then(myJson => {
          newUserId = JSON.stringify(myJson.data.id).replace(/\"/g, "");
            
      },this.apiData);
    }

I tried passing it as:

await page.evaluate(([apiData, emailReceived] =>
   { 
    },[this.apiData, emailId]);

But then apiData values throws error - Property 'domain' does not exist on type 'string'.

I tried other solutions but none seems to work for me. How can I implement this?

Upvotes: 1

Views: 822

Answers (1)

hardkoded
hardkoded

Reputation: 21637

You can add the emailId to the apiData:

await page.evaluate(([apiData, emailReceived] =>
  { 
  },
  {
   ...this.apiData, 
   emailId
  });

And then you will be able to use apiData.emailId inside the function you are evaluating.

Upvotes: 1

Related Questions