剧作家在非输入元素上上传文件

发布于 2025-01-10 21:24:45 字数 747 浏览 0 评论 0原文

因此,我目前正在尝试使用 Playwright 在 Electron 应用程序上自动上传个人资料照片,但遇到了“filechooser”事件的问题。

 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(),
  ]);

用于上传照片的元素不是输入类型,所以我正在使用

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

问题是试图让剧作家从弹出的输入对话框中选择图像,但它不会选择任何文件。我还试图让剧作家在我的装置文件夹中选择一个图像,该文件夹位于测试的相对路径中,但在这两种情况下都没有成功。

Playwright 显示的错误是

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

waiting for event "filechooser"

知道问题是什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2025-01-17 21:24:45

如果您使用 window.showOpenFilePicker() 从用户处获取文件,则根本不会获得 filechooser 事件。这是因为在内部,showOpenFilePicker 没有触发事件,因为它仍然是 WIP。

更多信息可以在 playwright issues #8850 中找到,但我认为没有目前的解决方法。 (Pupetter 实际上有同样的问题

一种解决方法是不使用 showOpenFilePicker() 根本没有,而是依赖 元素来收集文件。这对于开发人员来说有点麻烦,但受到更多支持,并且应该触发 filechooser 事件。

另一个修复可能是添加一个在测试模式下运行时可以覆盖的函数,甚至不需要打开文件选择器,例如:

const 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.
  }
}

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 info can be found in playwright issue #8850 but I don't think there is a workaround for now. (Pupetter actually has the same issue)

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 mode for it to not even need to open the file chooser, something like:

const 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.
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文