Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Is it possible to share the browser between globalSetup and globalTeardown? #16800

Closed
liamdocherty10 opened this issue Aug 24, 2022 · 2 comments

Comments

@liamdocherty10
Copy link

liamdocherty10 commented Aug 24, 2022

We are currently using globalSetup in order to log in as admin user and setup data, then globalTeardown to delete the data at the end.

At the moment, both global functions are launching a browser and logging in as the admin user.

This works, but is there a way to share the same browser/ browser context instance between setup and teardown?

This would improve our efficiency since there is nothing in our tests that invalidates the session still active in the setup browser.

E.g.

  • Config file
const config: PlaywrightTestConfig = {
  globalSetup: require.resolve("./tests/global-setup"),
  globalTeardown: require.resolve("./tests/global-teardown"),
  // Remaining config
}
  • Global Setup
async function globalSetup(): Promise<void> {
  const browser: Browser = await chromium.launch();
  const context: BrowserContext = await browser.newContext();
  const page: Page = await context.newPage();
  // Log in
  // Create data
}
  • Global Teardown
async function globalTeardown(): Promise<void> {
  const browser: Browser = await chromium.launch();
  const context: BrowserContext = await browser.newContext();
  const page: Page = await context.newPage();
  // Log in
  // Delete data
}
@liamdocherty10 liamdocherty10 changed the title [Question] Is it possible to share the browser between globalSetup and globalTeardown [Question] Is it possible to share the browser between globalSetup and globalTeardown? Aug 24, 2022
@yury-s
Copy link
Member

yury-s commented Aug 24, 2022

globalSetup and globalTeardown both run in the same process, so you can store the browser in a shared variable during setup and read it in global teardown:

// globalSetup.ts
import { chromium, FullConfig } from '@playwright/test';
import { globalState } from './shared';

async function globalSetup(config: FullConfig) {
  globalState.browser = await chromium.launch();
  await globalState.browser.newPage();
}

export default globalSetup;


// globalTeardown.ts
import { globalState } from './shared';

async function globalTeardown() {
  await globalState.browser?.close();
}

export default globalTeardown;


// shared.ts
import { type Browser } from '@playwright/test';

export const globalState: ({browser: Browser | undefined}) = {
  browser: undefined
};

#14895 tracks ongoing improvements in global setup/teardown.

@yury-s yury-s closed this as completed Aug 24, 2022
@liamdocherty10
Copy link
Author

This has worked for us, thank you @yury-s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants