Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][Electron] filechooser event not emitted when dialog.show(Open|Save)Dialog is called #5013

Closed
rfricz opened this issue Jan 14, 2021 · 6 comments

Comments

@rfricz
Copy link

rfricz commented Jan 14, 2021

Context:

  • Playwright Version: playwright-electron 0.5.0
  • Operating System: Windows 10 20H2
  • Node.js version: 12.18.1
  • Browser: Electron 11.1.1

Description
The filechooser event is not emitted when dialog.show(Open|Save)Dialog is called in Electron’s main process. It doesn’t matter if the dialog function is called via app.evaluate during a test or triggered from the webapp. Electron also allows selecting directories by these dialogs, FileChooser should support this too.

Code Snippet

The following test suite that times out shows the issue. Adapted from How to demo on the npm package page, only spec.js is different. Put all three files in one dir and run npx mocha spec.js:

main.js:

const { app, BrowserWindow } = require('electron');

function createWindow () {
  let win = new BrowserWindow({
    width: 800,
    height: 600,
  });

  win.loadFile('index.html');
}

app.whenReady().then(createWindow);

index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
    <style>
      html {
        width: 100%;
        height: 100%;
        display: flex;
        background: white;
      }

      body {
        flex: auto;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
      }
    </style>
  </head>
  <body>
    <h1>Hello World!</h1>
    <button onclick="console.log('click')">Click me</button>
  </body>
</html>

spec.js:

const { electron  } = require('playwright-electron');
const assert = require('assert');
const electronPath = require('electron');
const path = require('path');

describe('filechooser bug', function () {
  this.timeout(10000);

  before(async () => {
    this.app = await electron.launch(electronPath, {
      args: [path.join(__dirname, "main.js")]
    });
  });

  after(async () => {
    await this.app.close();
  });

  it('filechooser fires in electron', async () => {
    let resolve;
    const promise = new Promise(r => {
      resolve = r;
    });
    const page = await this.app.firstWindow();
    page.on('filechooser', async (fileChooser) => {
      resolve(true);
      await fileChooser.setFiles('/foo/bar.baz');
    });

    await this.app.evaluate(async ({ BrowserWindow, dialog }) => {
      const win = BrowserWindow.getAllWindows()[0];
      await dialog.showOpenDialog(win, {});
    });
    assert.ok(await promise);
  });
});
@yury-s yury-s changed the title [BUG] filechooser event not emitted when dialog.show(Open|Save)Dialog is called in Electron [BUG][Electron] filechooser event not emitted when dialog.show(Open|Save)Dialog is called Jan 14, 2021
@pavelfeldman
Copy link
Member

I'll close this as a part of the bug triaging process. We have hundreds of bugs and feature requests with dozens and even hundreds of upvotes, while this one only has a few thumbs up. Please feel free to open a new bug and link this one if you'd like to see it addressed.

@Daveiano
Copy link

Daveiano commented Jan 2, 2022

Any progress or update on this? I am struggling with the same issue.

@wsw0108
Copy link

wsw0108 commented Jan 11, 2022

Based on spectron-fake-dialog, I wrote a little library to fake electron dialog for playwright/electron,
https://github.com/wsw0108/playwright-fake-dialog

@eric-burel
Copy link

eric-burel commented Jan 25, 2022

As far as I understand dialogs are not normal browser filechooser, as they are triggered from the backend/main process. So the new dialog is probably more a "window" object, though I could not grab it either this way.

@wsw0108 package works perfect for mocking dialogs. I still need to figure keypress to trigger my dialog though but that's a different issue.

@akosmenyhert
Copy link

@wsw0108 I tried your test repo, but the native menu open file test does not open any file. On a Windows machine, after the mock method, the clickMenuItemById does not work. Without the mock method it opens a dialog. Also, the setTimeut is not fired because the test is finished before the timeout ended.
Do you have any idea how to fix this?

@jessfraz
Copy link

can we pay you all to fix this we need it @pavelfeldman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants