PlayWright Electron: Is it possible to get BrowserWindow from Page?

1.3k Views Asked by At

I'm trying to find the electron window object that contains the loaded page. I want to take the following action:

let window1 : BrowserWindow | null = null
let window2 : BrowserWindow | null = null

  electronApp.on("window", async (page) => {
    //evaluate page so that title is loaded
    await page.evaluate(() => { });
    if(await page.title() === 'page1') {
      window1 = getElectronBrowserWindow(page); // Example
    }
    else if(await page.title() === 'page2') {
      window2 = getElectronBrowserWindow(page);
  }
  });

In my test I would like to do something like:

 test("check if window is not visible", async () => {
   let visibility = window1.isVisible();
   expect(visibility).toBeFalsy
  });
 });

I've come across electronApplication.browserWindow(page). However, it does not provide the page's BrowserWindow object.

1

There are 1 best solutions below

0
On BEST ANSWER

Yes, it can be done.

What electronApplication.browserWindow(page) provides is an object of type JSHandle<Electron.BrowserWindow>. You have to use itsevaluate() method.

I'm not sure of all the details of your test and the differences b/w window1 and window2, but here's an example of testing that a browser window is visible. Hopefully this clarifies

import { expect, test } from "@playwright/test";
import { ElectronApplication, Page, JSHandle, _electron } from "playwright";

let electronApp: ElectronApplication;
let page: Page;
let bwHandle: JSHandle<Electron.BrowserWindow>;

test.beforeAll(async () => {
  electronApp = await _electron.launch({
    // your launch options
  });
  page = await electronApp.firstWindow();
});

test("check if window is visible", async () => {
  bwHandle = await electronApp.browserWindow(page);
  const visible = await bwHandle.evaluate((win) => win.isVisible());
  expect(visible).toBeTruthy();
});

test.afterAll(async () => {
  await electronApp.close();
});