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)
})
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With