Sha256: 6e4091f11875a64d675a2c59309902d71cd03effd5c33ad3c09ed9d1768a8795

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

class Capybara::Selenium::ChromeNode < Capybara::Selenium::Node
  def set_file(value) # rubocop:disable Naming/AccessorMethodName
    super(value)
  rescue ::Selenium::WebDriver::Error::ExpectedError => e
    if e.message =~ /File not found : .+\n.+/m
      raise ArgumentError, "Selenium < 3.14 with remote Chrome doesn't support multiple file upload"
    end
    raise
  end

  def drag_to(element)
    return super unless self[:draggable] == 'true'

    scroll_if_needed { driver.browser.action.click_and_hold(native).perform }
    driver.execute_script HTML5_DRAG_DROP_SCRIPT, self, element
  end

private

  def bridge
    driver.browser.send(:bridge)
  end

  HTML5_DRAG_DROP_SCRIPT = <<~JS
    var source = arguments[0];
    var target = arguments[1];

    var dt = new DataTransfer();
    var opts = { cancelable: true, bubbles: true, dataTransfer: dt };

    var dragEvent = new DragEvent('dragstart', opts);
    source.dispatchEvent(dragEvent);
    target.scrollIntoView({behavior: 'instant', block: 'center', inline: 'center'});
    var dragOverEvent = new DragEvent('dragover', opts);
    target.dispatchEvent(dragOverEvent);
    var dragLeaveEvent = new DragEvent('dragleave', opts);
    target.dispatchEvent(dragLeaveEvent);
    if (dragOverEvent.defaultPrevented) {
      var dropEvent = new DragEvent('drop', opts);
      target.dispatchEvent(dropEvent);
    }
    var dragEndEvent = new DragEvent('dragend', opts);
    source.dispatchEvent(dragEndEvent);
  JS
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
tdiary-5.0.11 vendor/bundle/gems/capybara-3.6.0/lib/capybara/selenium/nodes/chrome_node.rb
capybara-3.6.0 lib/capybara/selenium/nodes/chrome_node.rb