Rayrun
← Back to Discord Forum

Cannot read properties of undefined (reading 'goto') on a function called inside global-setup.ts

I get an error: TypeError: Cannot read properties of undefined (reading 'goto') when I call a function inside global-setup.ts . I found out that there is a need to use this but still doesn't work: https://stackoverflow.com/questions/71330522/typeerror-cannot-read-property-goto-of-undefined playwright.config.ts

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

export default defineConfig({
  globalSetup: require.resolve('./playwright/global-setup'),

global-setup.ts

import { Page } from "@playwright/test";
import { getCookies } from './utils/auth';

async function globalSetup(page: Page ) {
    const accessToken = await getCookies(page, 'access-token');
}

export default globalSetup;

auth.ts

export async function getCookies(page: Page, cookieName: string) { 
  await page.goto('/'); // this errors out!!!!!!!
  // Get all cookies
  const allCookies = await page.context().cookies();

  // Find the specific cookie by name
  const tokenCookie = allCookies.find(cookie => cookie.name === cookieName);

  if (tokenCookie) {
    console.log(`Token name & value: ${tokenCookie.name} & ${tokenCookie.value}`);
    // You can save or use the token cookie as needed
  } else {
    console.log('Token cookie not found.');
  }

  return tokenCookie?.value;
}

This thread is trying to answer question "How can I resolve the 'TypeError: Cannot read properties of undefined (reading 'goto')' error when calling a function inside 'global-setup.ts' and where do I pass 'page' into the 'globalSetup' function?"

1 reply

async function globalSetup(page: Page )

where do you pass page into this function?

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.