Search code examples
typescriptplaywrightplaywright-typescript

How to make Chrome windows to open up only on my primary screen?


I'm using Playwright with Typescript. My station has 2 screens, the 1st (primary) is the screen of my laptop. The 2nd screen is an external screen that is connected to my laptop.

When I run a set of tests, sometimes the Chrome windows open on my primary screen, and sometimes on my secondary screen.

How to make the tests run only on my primary screen?

I tried this code, but it does not work:

async initPage(): Promise<void> {
        super.initPage()
        try {
            await this.page.waitForLoadState('domcontentloaded')
            await this.page.waitForLoadState('networkidle')
        } catch (error) {}
        await this.page.evaluate(() => {
            window.moveTo(0, 0)
        })
    }
  

Solution

  • This can be achieved by adding --window-position=0,0 flag to launchOptions args property via playwright.config.ts file.

    playwright.config.ts example:

    import { defineConfig } from "@playwright/test";
    
    export default defineConfig({
      testDir: "./tests",
    
      use: {
        headless: false,
    
        // launchOptions allows you to pass additional options when launching the browser.
        launchOptions: {
          // This line sets the position of the browser window to the top-left corner of the screen (coordinates 0,0).
          args: ["--window-position=0,0"],
        },
      },
    
      projects: [{ name: "chromium" }],
    });
    

    Now, since The (0, 0) screen coordinate is always the top left pixel of the primary monitor, the test always will be launched on the primary screen.

    The list of Chromium flags can be found here.