microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.5k stars 3.64k forks source link

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

Open mastrzyz opened 1 year ago

mastrzyz commented 1 year ago

Context:

System:

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/132423423c@thread.v2?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.

Linkgoron commented 1 year ago

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

mastrzyz commented 1 year ago

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.

dgozman commented 1 year ago

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 commented 1 year ago

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

dgozman commented 1 year ago

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.

rafael-anachoreta commented 1 year ago

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?

duereg commented 1 year ago

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

rafael-anachoreta commented 1 year ago

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