3

So I'm currently trying to automate uploading a profile photo on an Electron App using Playwright and I'm running into issues with 'filechooser' event.

 await windowA.click('data-testid');

  const [fileChooser] = await Promise.all([
    windowA.waitForEvent('filechooser'),
    // windowA.locator('text=Edit').click(),
    windowA.waitForTimeout(3000),

    windowA.locator(selector).click(),
  ]);

The element used to upload a photo isn't an input type so I'm using

   await fileChooser.setFiles(
    [filepath],
    { timeout: 1000 }
   );

The issue is trying to get playwright to select an image from the input dialog box that pops up and it just won't select any files. I've also been trying to get playwright to select an image in my fixtures folder, which is in a relative path to the test, but haven't had success in either case.

The error that Playwright is displaying is

page.waitForEvent: Timeout while waiting for event "filechooser"

waiting for event "filechooser"

Any know what the issue is?

burtonjane
  • 33
  • 4
  • Is `windowA` a `contentFrame`? If so, the `filechooser` event might be popping op on the original page instead of the `contentFrame`. So you might want to change your code into something like ```const [fileChooser] = await Promise.all([ page.waitForEvent('filechooser'), windowA.waitForTimeout(3000), windowA.locator(selector).click(), ]);``` And then, ```await fileChooser.setFiles( [filepath], { timeout: 1000 } );``` – archon Mar 02 '22 at 16:46
  • windowA is the page (just different naming convention for Electron), not a contentFrame. – burtonjane Mar 03 '22 at 00:48
  • Does the file manager pop up? – archon Mar 03 '22 at 00:52
  • Yes it does but I can't seem to get it to select any files. – burtonjane Mar 03 '22 at 03:08

1 Answers1

2

My slippers told me that if you are using the window.showOpenFilePicker() to get a file from the user, you won't get the filechooser event at all. This is because internally, the showOpenFilePicker is not triggering an event as it is still a WIP. More infos can be found there but I don't think there is a workaround for now https://githubmemory.com/repo/microsoft/playwright/issues/8850

Pupetter has actually the same issue: https://github.com/puppeteer/puppeteer/issues/5210`

One fix would be to not use the showOpenFilePicker() at all, but instead rely on the <input> element to gather the file. This is a bit more cumbersome for the dev but is more supported and should trigger the 'filechooser' event.

Another fix could be to add a function you can override when running in test move for it to not even need to open the file chooser. Something like

getFileFromPicker() => { 
    if(!isRunningInTest) { 
        // do the showOpenFilePicker logic as usual in the app and the user will need to choose a file from it
    } else {
        // provide synchronously a buffer to use as file content, and so do not even show the picker to the testing user.
    }

sexyslippers69
  • 334
  • 2
  • 9