playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[BUG] Electron App not able to find the firstWindow of Teams.

Open mastrzyz opened this issue 2 years ago • 4 comments

Context:

  • Playwright Version: 1.30
  • Operating System: Windows
  • Node.js version: 16.13
  • Browser: electron
  • Extra: https://teams.microsoft.com/

System:

  • OS: Windows 10
  • Memory: 8.51 GB / 31.72 GB

Binaries:

  • Node: 16.13.0 - C:\Program Files\nodejs\node.EXE
  • Yarn: 1.21.1 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
  • npm: 8.1.0 - C:\Program Files\nodejs\npm.CMD

Languages:

  • Bash: 5.0.16 - C:\WINDOWS\system32\bash.EXE

Code Snippet Help us help you! Put down a short code snippet that illustrates your bug and that we can run and debug locally. For example:

1. npm init
2. npm install playwright
const { _electron: electron } = require("playwright");

(async () => {
  const teamsAppLocation =
    "C:\\Users\\marcin\\AppData\\Local\\Microsoft\\Teams\\current\\Teams.exe";

  // Launch Electron app.
  const electronApp = await electron.launch({
    executablePath: teamsAppLocation,
  });

  setInterval(() => {
    const result = electronApp.windows();
    const urls = result.map((r) => r.url());
    console.log(urls);
  }, 400);
})();

Describe the bug

We let the script start the script and iterate through each window.

The App loads and we only see 3 windows and never the main window.

[
  'file:///C:/Users/marcin/AppData/Local/Microsoft/Teams/current/resources/app.asar/lib/renderer/notifications/notifications.html',
  'file:///C:/Users/marcin/AppData/Local/Microsoft/Teams/current/resources/app.asar/lib/pluginhost/csp.html',
  'https://teams.microsoft.com/multi-window/?agent=electron&version='
]

this goes on for a long time and will not change.

If we "Kick start" the process by opening dev tools, we see a fourth window show up. Ctrl+Shift+I

[
  'file:///C:/Users/marcin/AppData/Local/Microsoft/Teams/current/resources/app.asar/lib/renderer/notifications/notifications.html',
  'file:///C:/Users/marcin/AppData/Local/Microsoft/Teams/current/resources/app.asar/lib/pluginhost/csp.html',
  'https://teams.microsoft.com/multi-window/?agent=electron&version=',
  'https://teams.microsoft.com/_#/conversations/[email protected]?ctx=chat'
]

Could this be a missing .onPage event in Playwright not capturing the main window? Is there anyway for us to "force refresh" the current open pages in Playwright?

Thanks! this wasn't reproing in Selenium for more context.

mastrzyz avatar Feb 02 '23 01:02 mastrzyz

I actually faced the same issue with Teams a few weeks ago as well, and what solved the issue for me was calling Target.getTargets with the CDP session

Linkgoron avatar Feb 20 '23 14:02 Linkgoron

I actually faced the same issue with Teams a few weeks ago as well, and what solved the issue for me was calling Target.getTargets with the CDP session

Oddly this solves my issue.

mastrzyz avatar Feb 28 '23 00:02 mastrzyz

Upstream investigation notes: RenderFrameDevToolsAgentHost::AddAllAgentHosts skips WebContents without a live render frame, which is the case for Electron's BrowserWindow that was created, but never loaded the url yet. This in turn makes Target.setAutoAttach not report a target, and Playwright never finds out about it.

dgozman avatar May 22 '23 22:05 dgozman

Upstream Chromium fix https://chromium-review.googlesource.com/c/chromium/src/+/4546274, needs a roll to Electron.

dgozman avatar May 23 '23 03:05 dgozman

The fix is available in Chromium 116. I've manually verified with Electron v26.0.0-alpha.3, and the issue is fixed there. We can cleanup some parts of loader.ts once Electron 26+ is common.

dgozman avatar Jun 05 '23 22:06 dgozman

I'm facing similar issues with my Electron app when bumping Playwright past v1.34.

@dgozman , do I understand correctly that the problem is on Playwright's Electron version and not on the application's Electron version?

rafael-anachoreta avatar Aug 22 '23 09:08 rafael-anachoreta

Can Confirm: Had this exact issue, upgraded to Electron@26 resolved the problem for me.

duereg avatar Aug 23 '23 19:08 duereg

Okay, so I have finally figured out what was making my application misbehave.

Originally, I was running my tests with "@playwright/test": "^1.29.2" and I was importing electron from playwright.

import { test, expect } from '@playwright/test'
import { _electron as electron } from 'playwright'
// ... 
// App correctly starts and I'm able to interact with the first window
    const electronApp = await electron.launch({
      executablePath: findExecutablePath(),
      recordVideo: {
        dir: 'test-results/videos',
      },
    })

After bumping to version 1.39.0, I had to update the imports and my tests stopped working

import { test, expect, _electron as electron } from '@playwright/test'
// ...
// App correctly starts but I'm unable to interact with the first window
    const electronApp = await electron.launch({
      executablePath: findExecutablePath(),
      recordVideo: {
        dir: 'test-results/videos',
      },
    })

It turns out that if I remove the recordVideo option, the tests work again and I can correctly select my first window!

import { test, expect, _electron as electron } from '@playwright/test'
// ...
// App correctly starts and I'm able to interact with the first window
    const electronApp = await electron.launch({
      executablePath: findExecutablePath(),
    })

@dgozman, @mastrzyz could you kindly confirm if this is also the case for you? Please let me know if you'd like me to report this as a separate issue.

Edit: I've reported the issue here

rafael-anachoreta avatar Oct 13 '23 17:10 rafael-anachoreta