Path: blob/trunk/rb/lib/selenium/webdriver/common/interactions/key_actions.rb
1990 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21module KeyActions22#23# Performs a key press. Does not release the key - subsequent interactions may assume it's kept pressed.24# Note that the key is never released implicitly - either ActionBuilder#key_up(key) or ActionBuilder#release_actions25# must be called to release the key.26#27# @example Press a key28#29# driver.action.key_down(:control).perform30#31# @example Press a key on an element32#33# el = driver.find_element(id: "some_id")34# driver.action.key_down(el, :shift).perform35#36# @overload key_down(key, device: nil)37# @param [Symbol, String] key The key to press38# @param [Symbol, String] device Optional name of the KeyInput device to press the key on39# @overload key_down(element, key, device: nil)40# @param [Element] element An optional element to move to first41# @param [Symbol, String] key The key to press42# @param [Symbol, String] device Optional name of the KeyInput device to press the key on43# @return [ActionBuilder] A self reference44#4546def key_down(*, device: nil)47key_action(*, action: :create_key_down, device: device)48end4950#51# Performs a key release.52# Releasing a non-depressed key will yield undefined behaviour.53#54# @example Release a key55#56# driver.action.key_up(:shift).perform57#58# @example Release a key from an element59#60# el = driver.find_element(id: "some_id")61# driver.action.key_up(el, :alt).perform62#63# @overload key_up(key, device: nil)64# @param [Symbol, String] key The key to press65# @param [Symbol, String] device Optional name of the KeyInput device to press the key on66# @overload key_up(element, key, device: nil)67# @param [Element] element An optional element to move to first68# @param [Symbol, String] key The key to release69# @param [Symbol, String] device Optional name of the KeyInput device to release the key on70# @return [ActionBuilder] A self reference71#7273def key_up(*, device: nil)74key_action(*, action: :create_key_up, device: device)75end7677#78# Sends keys to the active element. This differs from calling79# Element#send_keys(keys) on the active element in two ways:80#81# * The modifier keys included in this call are not released.82# * There is no attempt to re-focus the element - so send_keys(:tab) for switching elements should work.83#84# @example Send the text "help" to an element85#86# el = driver.find_element(id: "some_id")87# driver.action.send_keys(el, "help").perform88#89# @example Send the text "help" to the currently focused element90#91# driver.action.send_keys("help").perform92#93# @overload send_keys(keys, device: nil)94# @param [Array, Symbol, String] keys The key(s) to press and release95# @param [Symbol, String] device Optional name of the KeyInput device to press and release the keys on96# @overload send_keys(element, keys, device: nil)97# @param [Element] element An optional element to move to first98# @param [Array, Symbol, String] keys The key(s) to press and release99# @param [Symbol, String] device Optional name of the KeyInput device to press and release the keys on100# @return [ActionBuilder] A self reference101#102103def send_keys(*args, device: nil)104click(args.shift) if args.first.is_a? Element105args.map { |x| x.is_a?(String) ? x.chars : x }.flatten.each do |arg|106key_down(arg, device: device)107key_up(arg, device: device)108end109self110end111112private113114#115# @api private116#117# @overload key_down(key, action: nil, device: nil)118# @param [Symbol, String] key The key to press119# @param [Symbol] action The name of the key action to perform120# @param [Symbol, String] device Optional name of the KeyInput device to press the key on121# @overload key_down(element, key, action: nil, device: nil)122# @param [Element] element An optional element to move to first123# @param [Symbol, String] key The key to press124# @param [Symbol] action The name of the key action to perform125# @param [Symbol, String] device Optional name of the KeyInput device to press the key on126#127# @param [Array] args128# @option args [Element] element An optional element to move to first129# @option args [Symbol, String] key The key to perform the action with130# @param [Symbol] action The name of the key action to perform131# @param [Symbol, String] device optional name of the KeyInput device to press the key on132# @return [ActionBuilder] A self reference133#134135def key_action(*args, action: nil, device: nil)136key_input = key_input(device)137click(args.shift) if args.first.is_a? Element138key_input.send(action, args.last)139tick(key_input)140self141end142143def key_input(name = nil)144device(name: name, type: Interactions::KEY) || add_key_input('keyboard')145end146end # KeyActions147end # WebDriver148end # Selenium149150151