Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
        })
    }
  
like image 548
Tal Angel Avatar asked Oct 24 '25 02:10

Tal Angel


2 Answers

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.

like image 82
I.sh. Avatar answered Oct 27 '25 02:10

I.sh.


Here is a possible solution, but without greater insight into your code you will have to figure out how to integrate it. You can leverage the launch options from chromium to achieve what you want when launching the browser.

const { chromium } = require('playwright');  // Or 'chromium' or 'webkit'.

const openBrowser = async () => {
  const browser = await chromium.launch({
    args: [
        '--window-position=0,0'
    ],
    headless: false,
  });
  const page = await browser.newPage();
  await page.goto('https://example.com');
};

Playwright does not recommend using the args parameter when launching a browser (I personally doubt the arg I use here is risky), but using the --window-position option and setting the value to 0,0 will guarantee the window always opens in the top left corner of the main display.

like image 36
Jesus Diaz Rivero Avatar answered Oct 27 '25 01:10

Jesus Diaz Rivero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!