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.



606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/capybara/playwright/node.rb', line 606

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



671
672
673
674
675
# File 'lib/capybara/playwright/node.rb', line 671

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