← Back to Discord Forum

Playwright tests on the UI.

I will run some UI tests but facing issues with which user account to use for authorize/authentication. Playwright can run in headless mode. Would this be the same flow using a non-interactive user account a.k.a. service account? Steps I want to follow:

  1. write the playwright tests to run in headless mode
  2. use the service account credentials to login (to the UI in headless mode) with username and password
  3. run the remaining tests in headless mode which will fill out some forms in the UI and submit

Is this do-able?

This thread is trying to answer question "Can Playwright tests be run in headless mode using a service account for authentication?"

2 replies

It depends on your application, but generally speaking 'headless' just means that the browser is not rendered to the screen. An account that would work headed should also work headless.

@.graff sorry didn't see your message before I removed mine. Thank you for your response. How about with an account that would not work headed? The account would only work in headless, non-interactive mode....


Sure, you can run @playwright/test in headless mode and authenticate using a service account. Here's how you do it.

First, you'll want to create an authenticated browser state and save it to a file. This can be done by creating a .auth directory in your project. Remember to add this directory to your .gitignore file to avoid committing sensitive data.

// TypeScript
import { chromium } from '@playwright/test';

async function createAuthState() {
  const browser = await chromium.launch();
  const context = await browser.newContext();
  // Authenticate here
  const state = await context.storageState();
  // Save state to a file
  await fs.writeFile('.auth/state.json', JSON.stringify(state));
  await browser.close();


Once you've set up the authenticated browser state, you can reuse it across multiple tests. This is done by loading the state at the start of each test.

// TypeScript
import { chromium } from '@playwright/test';

async function loadAuthState() {
  const browser = await chromium.launch();
  const context = await browser.newContext({
    storageState: JSON.parse(await fs.readFile('.auth/state.json', 'utf8')),
  const page = await context.newPage();
  // Now you can use the page with the authenticated state
  await browser.close();


This approach works best when your tests don't modify server-side state and can run simultaneously with the same account. If your tests need to modify server-side state or require different accounts for parallel execution, you may need to use different authentication strategies.

Remember, running tests in headless mode with a service account can speed up test execution and improve reproducibility. Just be sure to handle server-side modifications or parallel executions that may require different accounts accordingly. Happy testing with @playwright/test!

Related Discord Threads

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.