Skip to content

[Question] How to disable bringing page to front during launching. #4822

Closed

Description

@shirshak55

Its Headful mode.

Currently I am doing web chat tests where I would like to test multiple context.

If I spawn new context it brings that page to front loosing the focus.

let browser = await firefox.launch()

// Every 1 minute open new context and open page just to test if chat shows history or not.
while(true) {
  let context = await browser.newContext()
  let page = await context.newPage();
  
  // delay code is there not shown here.

}

The issue is that newContext spawns a new window which has focus or something is in page that is calling page.bringToFront() when its launched.

Questions:

  1. I would like to disable these behavior. If possible please let me know.
  2. Is there anyway to minimize the context window after launch. If I need to bring page to front I think i can call page.bringToFront() at the moment.

Its not a new issue I think and there are no answers available afaik.
puppeteer/puppeteer#5484
https://stackoverflow.com/questions/53501564/hide-browser-in-puppeteer

Activity

  1. creage commented on Dec 28, 2020

    @creage

    Well, we have a plenty of chat testing, and we have no issues running multiple browser instances. We have tests up to 4 users chatting at the same time, with no issues.

    What exact problem do you have?

  2. shirshak55 commented on Dec 28, 2020

    @shirshak55
    Author

    Aziz Gazanchiian (@creage) the issue is focus stealing. For 2-3 edge case we tests manually and we spawn like new window after 5 sec interval. When testing manually we are typing and the browser come in front of face which is abit annoying.

  3. pavelfeldman commented on Jan 16, 2021

    @pavelfeldman
    Member

    Playwright emulates all pages as focused, what exactly do you mean by focus stealing?

  4. pavelfeldman commented on Jan 16, 2021

    @pavelfeldman
    Member

    Ah, you mean your scenarios are hybrid and the focus is stolen from the person performing the manual steps? Can I ask why you are typing manually?

  5. shirshak55 commented on Jan 16, 2021

    @shirshak55
    Author

    Pavel Feldman (@pavelfeldman) Yea its like when we are doing some work on one window and if another window is spawned focus is stolen from the person doing manual steps. Sometime we need to do it for accessibility check in headful mode and is mostly from devops team.

  6. pavelfeldman commented on Jan 16, 2021

    @pavelfeldman
    Member

    I don't think we can do anything about it (focusing), but we could try helping automating those a11y checks!

  7. shirshak55 commented on Jan 16, 2021

    @shirshak55
    Author

    alright then I shall close this issue :) I also think its probably firefox or chrome issue

    Thanks.

  8. bstro commented on Nov 17, 2021

    @bstro

    I also experience this; its pretty annoying to have jest running with --watch while developing and have focus stolen away constantly by the playwright. It seems like it has to be possible to have an option to instruct playwright to open Chromium in the background, rather than foreground.

  9. ThatGuySam commented on Aug 7, 2024

    @ThatGuySam

    There's a command I figured out on macOS to start iOS Simulator in the background which reliably disables it from taking focus.

    open -g -a Simulator

    However, I'm unsure how to launch Playwright's Chromium with this option

  10. sneko commented on May 18, 2025

    @sneko

    I was also surprised there is no way to prevent bringing it to front and loosing focus.

    My workaround is to have "reusable pages" (1 if your program is simple, or X if you have a X as concurrency). You wait for a created page instance to be marked as available (with a mutex or so) to reuse it.

    Make sure to clear all listeners if you added some, or the storage if you don't want it to be shared. It's a bit more at risk... but for now that's the only viable solution when headless: true does not fit the use case.

  11. Andrew-Chen-Wang commented on Jun 12, 2026

    @Andrew-Chen-Wang

    This apparently really only affects chromium and only when launching a new page or launchPersistentContext. Added a PR: #41282

  12. johnlshankman-privy commented on Sep 14, 2026

    @johnlshankman-privy

    bringing this back from the dead because this still affects me daily. is there no workaround anyone has found?

    Andrew Chen Wang (@Andrew-Chen-Wang) still has an open PR #41306

  13. Andrew-Chen-Wang commented on Sep 14, 2026

    @Andrew-Chen-Wang

    Johnny Shankman (@johnlshankman-privy) Create a patches folder, and make a file. I use both playwright and patchright so:

    patches/patchright-core-no-focus.patch:

    diff --git a/lib/server/chromium/crBrowser.js b/lib/server/chromium/crBrowser.js
    --- a/lib/server/chromium/crBrowser.js
    +++ b/lib/server/chromium/crBrowser.js
    @@ -315,7 +315,7 @@ class CRBrowserContext extends import_browserContext.BrowserContext {
         return this._crPages().map((crPage) => crPage._page);
       }
       async doCreateNewPage() {
    -    const { targetId } = await this._browser._session.send("Target.createTarget", { url: "about:blank", browserContextId: this._browserContextId });
    +    const { targetId } = await this._browser._session.send("Target.createTarget", { url: "about:blank", browserContextId: this._browserContextId, background: true, focus: false });
         return this._browser._crPages.get(targetId)._page;
       }
       async doGetCookies(urls) {
    

    one more: patches/playwright-core-no-focus.patch

    diff --git a/lib/server/chromium/crBrowser.js b/lib/server/chromium/crBrowser.js
    --- a/lib/server/chromium/crBrowser.js
    +++ b/lib/server/chromium/crBrowser.js
    @@ -315,7 +315,7 @@ class CRBrowserContext extends import_browserContext.BrowserContext {
         return this._crPages().map((crPage) => crPage._page);
       }
       async doCreateNewPage() {
    -    const { targetId } = await this._browser._session.send("Target.createTarget", { url: "about:blank", browserContextId: this._browserContextId });
    +    const { targetId } = await this._browser._session.send("Target.createTarget", { url: "about:blank", browserContextId: this._browserContextId, background: true, focus: false });
         return this._browser._crPages.get(targetId)._page;
       }
       async doGetCookies(urls) {
    

    in pnpm-workspace.yaml:

    patchedDependencies:
      "patchright-core@*": patches/patchright-core-no-focus.patch
      "playwright-core@*": patches/playwright-core-no-focus.patch

    The @* entries in pnpm-workspace.yaml reapply the patches across versions,
    including the installed Playwright alpha. Keep patch failures fatal: if an upgrade
    changes the surrounding code, regenerate the affected patch with pnpm patch
    and pnpm patch-commit, then restore the wildcard entry. Check headed page creation,
    navigation, input, and macOS focus behavior after browser upgrades.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions