question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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);
  });
});

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:17
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
wsw0108commented, 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

0reactions
akosmenyhertcommented, Feb 18, 2022

@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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[BUG][Electron] filechooser event not emitted when dialog ...
[BUG][Electron] filechooser event not emitted when dialog.show(Open|Save)Dialog is called #8278. Open. OiNutter opened this issue on Aug 18, ...
Read more >
FileChooser (JavaFX 8) - Oracle Help Center
The configuration of the displayed dialog is controlled by the values of the FileChooser properties set before the corresponding show*Dialog method is called....
Read more >
dialog | Electron
Displays a modal dialog that shows an error message. This API can be called safely before the ready event the app module emits,...
Read more >
Gtk.FileChooserDialog - GTK Documentation
GtkFileChooserDialog is a dialog box suitable for use with “File/Open” or “File/Save as” commands. This widget works by putting a GtkFileChooserWidget ...
Read more >
JFileChooser.showSaveDialog() not showing up
The application loses the focus, so the Dialog appears in the background. The code doesn't "hang" by itself, it just waits until the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found