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.



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/capybara/playwright/node.rb', line 512

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.upcase
            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.upcase,
              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



577
578
579
580
581
# File 'lib/capybara/playwright/node.rb', line 577

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