0

因此,我目前正在尝试使用 Playwright 在 Electron App 上自动上传个人资料照片,但我遇到了“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"

有谁知道是什么问题?

4

1 回答 1

0

我的拖鞋告诉我,如果您使用window.showOpenFilePicker()来从用户那里获取文件,则根本不会收到该filechooser事件。这是因为在内部,showOpenFilePicker它不会触发事件,因为它仍然是 WIP。可以在那里找到更多信息,但我认为现在没有解决方法 https://githubmemory.com/repo/microsoft/playwright/issues/8850

木偶实际上有同样的问题: https://github.com/puppeteer/puppeteer/issues/5210`

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

另一个修复可能是添加一个在测试移动中运行时可以覆盖的功能,甚至不需要打开文件选择器。就像是

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

于 2022-03-03T05:14:52.043 回答