waitForFileChooser method

Future<FileChooser> waitForFileChooser({
  1. Duration? timeout,
})

NOTE In non-headless Chromium, this method results in the native file picker dialog not showing up for the user.

This method is typically coupled with an action that triggers file choosing. The following example clicks a button that issues a file chooser, and then responds with /tmp/myfile.pdf as if a user has selected this file.

var futureFileChooser = page.waitForFileChooser();
// some button that triggers file selection
await page.click('#upload-file-button');
var fileChooser = await futureFileChooser;

await fileChooser.accept([File('myfile.pdf')]);

NOTE This must be called before the file chooser is launched. It will not return a currently active file chooser.

Parameters:

  • timeout Maximum wait time in milliseconds, defaults to 30 seconds, pass 0 to disable the timeout. The default value can be changed by using the page.defaultTimeout property.
  • returns: Future<FileChooser> A promise that resolves after a page requests a file picker.

Implementation

Future<FileChooser> waitForFileChooser({Duration? timeout}) async {
  if (_fileChooserInterceptors.isEmpty) {
    await devTools.page.setInterceptFileChooserDialog(true);
  }

  timeout ??= defaultTimeout ?? globalDefaultTimeout;
  var callback = Completer<FileChooser>();
  _fileChooserInterceptors.add(callback);

  return callback.future.timeout(timeout).whenComplete(() {
    _fileChooserInterceptors.remove(callback);
  });
}