剧作家在非输入元素上上传文件
因此,我目前正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用
window.showOpenFilePicker()
从用户处获取文件,则根本不会获得filechooser
事件。这是因为在内部,showOpenFilePicker
没有触发事件,因为它仍然是 WIP。更多信息可以在 playwright issues #8850 中找到,但我认为没有目前的解决方法。 (Pupetter 实际上有同样的问题)
一种解决方法是不使用
showOpenFilePicker()
根本没有,而是依赖元素来收集文件。这对于开发人员来说有点麻烦,但受到更多支持,并且应该触发
filechooser
事件。另一个修复可能是添加一个在测试模式下运行时可以覆盖的函数,甚至不需要打开文件选择器,例如:
If you are using the
window.showOpenFilePicker()
to get a file from the user, you won't get thefilechooser
event at all. This is because internally, theshowOpenFilePicker
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 thefilechooser
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: