Skip to content

Instantly share code, notes, and snippets.

@corradin
Last active March 28, 2023 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corradin/7632ea89e37ba895e828a13445f1544d to your computer and use it in GitHub Desktop.
Save corradin/7632ea89e37ba895e828a13445f1544d to your computer and use it in GitHub Desktop.
import { chromium } from '@playwright/test';
import login from './utils/login';
const username = process.env.MEETUP_USERNAME ?? '';
const password = process.env.MEETUP_PASSWORD ?? '';
async function globalSetup(config: FullConfig): Promise<void> {
const { storageState } = config.projects[0].use;
// TODO: Remove headless after this has been tested
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
await login(page, username, password);
await page.context().storageState({
path: storageState,
});
await browser.close();
}
export default globalSetup;
@tishdemsas
Copy link

Hi @corradin, I was using your examples and reading article you posted. I am running into an issue where it complains of path on line 14 locally. Thanks in advance for all your help!

(property) path?: string
The file path to save the storage state to. If path is a relative path, then it is resolved relative to current working directory. If no path is provided, storage state is still returned, but won't be saved to the disk.

Type 'StorageState' is not assignable to type 'string'.
  Type '{ cookies: { name: string; value: string; domain: string; path: string; expires: number; httpOnly: boolean; secure: boolean; sameSite: "Strict" | "Lax" | "None"; }[]; origins: { origin: string; localStorage: { ...; }[]; }[]; }' is not assignable to type 'string'.ts(2322)
types.d.ts(7707, 5): The expected type comes from property 'path' which is declared here on type '{ path?: string; }'

Config file

import { chromium, FullConfig } from '@playwright/test';
import login from './tests/utils/login';

const username = process.env.PLAYWRIGHT_USERNAME ?? '';
const password = process.env.PLAYWRIGHT_PASSWORD ?? '';

async function globalSetup(config: FullConfig): Promise<void> {
  const { storageState } = config.projects[0].use;
  // TODO: Remove headless after this has been tested
  const browser = await chromium.launch({ headless: false });
  const page = await browser.newPage();
  await login(page, username, password);
  await page.context().storageState({
    path: storageState,
  });
  await browser.close();
}

export default globalSetup;

Screen Shot 2023-03-28 at 1 54 35 PM

@tishdemsas
Copy link

@corradin It doesn't save the storageState to the designated path either which is making my test fail. Thanks for the help!

Global set up updated

import { chromium, FullConfig } from '@playwright/test';
import login from './tests/utils/login';

const username = process.env.PLAYWRIGHT_USERNAME ?? '';
const password = process.env.PLAYWRIGHT_PASSWORD ?? '';

async function globalSetup(config: FullConfig): Promise<void> {
  const { storageState } = config.projects[0].use;
  // TODO: Remove headless after this has been tested
  const browser = await chromium.launch({ headless: true });
  const page = await browser.newPage();
  await login(page, username, password);
  await page.context().storageState({
    path: storageState as string,
  });
  console.log(storageState);
  await browser.close();
}

export default globalSetup;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment