← Back to Discord Forum

How do I distinguish windows?

Our electron app has (at least) 3 different windows. Sometimes a preliminary modal dialog pops up. Then the main window, along with a 3rd window. I want to write tests that verify whether or not the 1st modal dialog shows up. I'm using electronApplication.on('window', (newWindow: Page) => { ... }) to watch them, but can't find any properties on each newWindow to figure it out.

This thread is trying to answer question "How can I distinguish between different windows in an Electron app for testing purposes?"

0 replies


Working with Multiple Windows in Electron Apps with Playwright

You can easily interact with different windows in an Electron app using Playwright. First, launch your Electron app with electron.launch(), passing the main script name as an argument.

const { _electron: electron } = require('@playwright/test');

(async () => {
  const electronApp = await electron.launch({ args: ['main.js'] });

To get the first window that opens, use electronApp.firstWindow(). This returns a Page object representing the window.

const window = await electronApp.firstWindow();

Now, you can perform various actions on this window, like getting its title, capturing screenshots, or clicking buttons.

console.log(await window.title());
await window.screenshot({ path: 'intro.png' });
await window.click('text=Click me');

If your app has multiple windows, you can use browserWindow(page) to get a BrowserWindow object for a specific page, or windows() to get an array of Page objects for all open windows.

const browserWindow = await electronApplication.browserWindow(page);
const allWindows = await electronApplication.windows();

With these methods, you can easily distinguish between different windows in your Electron app for testing purposes. Remember, this guide assumes you're using Playwright with Electron. If your app has specific requirements, you might need to adjust your approach.

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.