Class: Capybara::Playwright::Node::SendKeys

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/playwright/node.rb

Defined Under Namespace

Classes: PressKey, TypeText

Constant Summary collapse

MODIFIERS =
{
  alt: 'Alt',
  ctrl: 'Control',
  control: 'Control',
  meta: 'Meta',
  command: 'Meta',
  cmd: 'Meta',
  shift: 'Shift',
}.freeze
KEYS =
{
  cancel: 'Cancel',
  help: 'Help',
  backspace: 'Backspace',
  tab: 'Tab',
  clear: 'Clear',
  return: 'Enter',
  enter: 'Enter',
  shift: 'Shift',
  control: 'Control',
  alt: 'Alt',
  pause: 'Pause',
  escape: 'Escape',
  space: 'Space',
  page_up: 'PageUp',
  page_down: 'PageDown',
  end: 'End',
  home: 'Home',
  left: 'ArrowLeft',
  up: 'ArrowUp',
  right: 'ArrowRight',
  down: 'ArrowDown',
  insert: 'Insert',
  delete: 'Delete',
  semicolon: 'Semicolon',
  equals: 'Equal',
  numpad0: 'Numpad0',
  numpad1: 'Numpad1',
  numpad2: 'Numpad2',
  numpad3: 'Numpad3',
  numpad4: 'Numpad4',
  numpad5: 'Numpad5',
  numpad6: 'Numpad6',
  numpad7: 'Numpad7',
  numpad8: 'Numpad8',
  numpad9: 'Numpad9',
  multiply: 'NumpadMultiply',
  add: 'NumpadAdd',
  separator: 'NumpadDecimal',
  subtract: 'NumpadSubtract',
  decimal: 'NumpadDecimal',
  divide: 'NumpadDivide',
  f1: 'F1',
  f2: 'F2',
  f3: 'F3',
  f4: 'F4',
  f5: 'F5',
  f6: 'F6',
  f7: 'F7',
  f8: 'F8',
  f9: 'F9',
  f10: 'F10',
  f11: 'F11',
  f12: 'F12',
  meta: 'Meta',
  command: 'Meta',
}

Instance Method Summary collapse

Constructor Details

#initialize(element_or_keyboard, keys) ⇒ SendKeys

Returns a new instance of SendKeys.



741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
# File 'lib/capybara/playwright/node.rb', line 741

def initialize(element_or_keyboard, keys)
  @element_or_keyboard = element_or_keyboard

  holding_keys = []
  @executables = keys.each_with_object([]) do |key, executables|
    if MODIFIERS[key]
      holding_keys << key
    else
      if holding_keys.empty?
        case key
        when String
          executables << TypeText.new(key)
        when Symbol
          executables << PressKey.new(
            key: key_for(key),
            modifiers: [],
          )
        when Array
          _key = key.last
          code =
            if _key.is_a?(String) && _key.length == 1
              _key
            elsif _key.is_a?(Symbol)
              key_for(_key)
            else
              raise ArgumentError.new("invalid key: #{_key}. Symbol of 1-length String is expected.")
            end
          modifiers = key.first(key.size - 1).map { |k| modifier_for(k) }
          executables << PressKey.new(
            key: code,
            modifiers: modifiers,
          )
        end
      else
        modifiers = holding_keys.map { |k| modifier_for(k) }

        case key
        when String
          key.each_char do |char|
            executables << PressKey.new(
              key: char,
              modifiers: modifiers,
            )
          end
        when Symbol
          executables << PressKey.new(
            key: key_for(key),
            modifiers: modifiers
          )
        else
          raise ArgumentError.new("#{key} cannot be handled with holding key #{holding_keys}")
        end
      end
    end
  end
end

Instance Method Details

#executeObject



806
807
808
809
810
# File 'lib/capybara/playwright/node.rb', line 806

def execute
  @executables.each do |executable|
    executable.execute_for(@element_or_keyboard)
  end
end