Class: Puppeteer::Bidi::FileChooser

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/bidi/file_chooser.rb,
sig/puppeteer/bidi/file_chooser.rbs

Overview

FileChooser represents a file chooser dialog opened by an input element Based on Puppeteer's FileChooser implementation

Instance Method Summary collapse

Constructor Details

#initialize(element, multiple) ⇒ Object

Parameters:

  • element (ElementHandle)

    The input element that opened the file chooser

  • multiple (Boolean)

    Whether multiple files can be selected



11
12
13
14
15
# File 'lib/puppeteer/bidi/file_chooser.rb', line 11

def initialize(element, multiple)
  @element = element
  @multiple = multiple
  @handled = false
end

Instance Method Details

#accept(paths) ⇒ Object

Accept the file chooser with the given file paths

Parameters:

  • paths (Array<String>)

    File paths to accept

Returns:

  • (Object)

Raises:

  • (RuntimeError)

    If the file chooser has already been handled

  • (RuntimeError)

    If multiple files passed to single-file input



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/puppeteer/bidi/file_chooser.rb', line 27

def accept(paths)
  raise 'Cannot accept FileChooser which is already handled!' if @handled

  # Validate that single-file inputs don't receive multiple files
  if !@multiple && paths.length > 1
    raise 'Multiple file paths passed to a file input that does not accept multiple files'
  end

  @handled = true
  @element.upload_file(*paths)
end

#cancelObject

Cancel the file chooser

Returns:

  • (Object)

Raises:

  • (RuntimeError)

    If the file chooser has already been handled



41
42
43
44
45
46
47
48
49
50
# File 'lib/puppeteer/bidi/file_chooser.rb', line 41

def cancel
  raise 'Cannot cancel FileChooser which is already handled!' if @handled

  @handled = true
  @element.evaluate(<<~JAVASCRIPT)
  element => {
    element.dispatchEvent(new Event('cancel', {bubbles: true}));
  }
  JAVASCRIPT
end

#multiple?Boolean

Check if multiple files can be selected

Returns:

  • (Boolean)

    True if multiple files can be selected



19
20
21
# File 'lib/puppeteer/bidi/file_chooser.rb', line 19

def multiple?
  @multiple
end