Search code examples
typescriptgoogle-chromeautomationchromiumplaywright

How to launch Chrome or Chromium using an existing user profile in Playwright


I'm using playwright and TS.
I'm currently executing my test with the default playwright setup (it's incognito if I remember correctly) and I want to execute some tests with my own chrome profile.

I tried to search for user-XXX or profile properties in chromium LaunchOptions object, but no luck.
of course I've checked the docs and nothing.
I've asked some AI tools and no help either.

Something like this:

const browser = await chromium.launch({
      channel: "chrome",

      // Here is what I'm looking for
      user_data_dir: "C:\\Users\\Home\\AppData\\Local\\Google\\Chrome\\User Data"
      // or
      user: ""
      // or
      profile: ""
    });

Or via BrowserContext... anything would help.


Solution

  • I'll add additional code over @ggorlen great answer, please read the comments within the code snippet:

    // Please note that for this test to run you must close chrome first
    test("launch pc chrome with it's user", async ({}) => {
      const userDataDir =
        "C:\\Users\\Home\\AppData\\Local\\Google\\Chrome\\User Data";
    
      const browserContext = await chromium.launchPersistentContext(userDataDir, {
        channel: "chrome",
      });
    
      // launchPersistentContext already creates a page for you,
      // so `const page = await browser.newPage();` is not needed here,
      // instead get the page from browser.pages()
      let page = browserContext.pages().at(0) as Page;
      if (!page) page = await browserContext.newPage();
    
      await page.goto("https://playwright.dev/");
    });