Best Selenium code snippet using Selenium.WebDriver.KeyActions.key_input
w3c_action_builder.rb
Source:w3c_action_builder.rb
...63 #64 # @example Add a key input device65 #66 # builder = device.action67 # builder.add_key_input('keyboard2')68 #69 # @param [String] name name for the device70 # @return [Interactions::KeyInput] The key input added71 #72 def add_key_input(name)73 new_input = Interactions.key(name)74 add_input(new_input)75 new_input76 end77 #78 # Retrieves the input device for the given name79 #80 # @param [String] name name of the input device81 # @return [Selenium::WebDriver::Interactions::InputDevice] input device with given name82 #83 def get_device(name)84 @devices.find { |device| device.name == name.to_s }85 end86 #87 # Retrieves the current PointerInput devices88 #89 # @return [Array] array of current PointerInput devices90 #91 def pointer_inputs92 @devices.select { |device| device.type == Interactions::POINTER }93 end94 #95 # Retrieves the current KeyInput device96 #97 # @return [Selenium::WebDriver::Interactions::InputDevice] current KeyInput device98 #99 def key_inputs100 @devices.select { |device| device.type == Interactions::KEY }101 end102 #103 # Creates a pause for the given device of the given duration. If no duration is given, the pause will only wait104 # for all actions to complete in that tick.105 #106 # @example Send keys to an element107 #108 # action_builder = driver.action109 # keyboard = action_builder.key_input110 # el = driver.find_element(id: "some_id")111 # driver.action.click(el).pause(keyboard).pause(keyboard).pause(keyboard).send_keys('keys').perform112 #113 # @param [InputDevice] device Input device to pause114 # @param [Float] duration Duration to pause115 # @return [W3CActionBuilder] A self reference.116 #117 def pause(device, duration = nil)118 device.create_pause(duration)119 self120 end121 #122 # Creates multiple pauses for the given device of the given duration.123 #124 # @example Send keys to an element125 #126 # action_builder = driver.action127 # keyboard = action_builder.key_input128 # el = driver.find_element(id: "some_id")129 # driver.action.click(el).pauses(keyboard, 3).send_keys('keys').perform130 #131 # @param [InputDevice] device Input device to pause132 # @param [Integer] number of pauses to add for the device133 # @param [Float] duration Duration to pause134 # @return [W3CActionBuilder] A self reference.135 #136 def pauses(device, number, duration = nil)137 number.times { device.create_pause(duration) }138 self139 end140 #141 # Executes the actions added to the builder....
action_builder.rb
Source:action_builder.rb
...63 #64 # @example Add a key input device65 #66 # builder = device.action67 # builder.add_key_input('keyboard2')68 #69 # @param [String] name name for the device70 # @return [Interactions::KeyInput] The key input added71 #72 def add_key_input(name)73 new_input = Interactions.key(name)74 add_input(new_input)75 new_input76 end77 #78 # Retrieves the input device for the given name79 #80 # @param [String] name name of the input device81 # @return [Selenium::WebDriver::Interactions::InputDevice] input device with given name82 #83 def get_device(name)84 @devices.find { |device| device.name == name.to_s }85 end86 #87 # Retrieves the current PointerInput devices88 #89 # @return [Array] array of current PointerInput devices90 #91 def pointer_inputs92 @devices.select { |device| device.type == Interactions::POINTER }93 end94 #95 # Retrieves the current KeyInput device96 #97 # @return [Selenium::WebDriver::Interactions::InputDevice] current KeyInput device98 #99 def key_inputs100 @devices.select { |device| device.type == Interactions::KEY }101 end102 #103 # Creates a pause for the given device of the given duration. If no duration is given, the pause will only wait104 # for all actions to complete in that tick.105 #106 # @example Send keys to an element107 #108 # action_builder = driver.action109 # keyboard = action_builder.key_input110 # el = driver.find_element(id: "some_id")111 # driver.action.click(el).pause(keyboard).pause(keyboard).pause(keyboard).send_keys('keys').perform112 #113 # @param [InputDevice] device Input device to pause114 # @param [Float] duration Duration to pause115 # @return [ActionBuilder] A self reference.116 #117 def pause(device, duration = nil)118 device.create_pause(duration)119 self120 end121 #122 # Creates multiple pauses for the given device of the given duration.123 #124 # @example Send keys to an element125 #126 # action_builder = driver.action127 # keyboard = action_builder.key_input128 # el = driver.find_element(id: "some_id")129 # driver.action.click(el).pauses(keyboard, 3).send_keys('keys').perform130 #131 # @param [InputDevice] device Input device to pause132 # @param [Integer] number of pauses to add for the device133 # @param [Float] duration Duration to pause134 # @return [ActionBuilder] A self reference.135 #136 def pauses(device, number, duration = nil)137 number.times { device.create_pause(duration) }138 self139 end140 #141 # Executes the actions added to the builder....
key_actions_spec.rb
Source:key_actions_spec.rb
...32 allow(keyboard).to receive(:create_key_down)33 allow(builder).to receive(:tick)34 builder.send('key_action', key, action: :create_key_down, device: 'name')35 end36 it 'should get the first key_input when no device name is supplied' do37 expect(builder).to receive(:get_device).with(nil)38 expect(builder).to receive(:key_inputs).and_return([keyboard])39 allow(keyboard).to receive(:create_key_down)40 allow(builder).to receive(:tick)41 builder.send('key_action', key, action: :create_key_down)42 end43 it 'should click the element if the first argument is a WebElement' do44 allow(builder).to receive(:get_device).and_return(keyboard)45 expect(builder).to receive(:click).with(element)46 allow(keyboard).to receive(:create_key_down)47 allow(builder).to receive(:tick)48 builder.send('key_action', element, key, action: :create_key_down)49 end50 it 'should create a key_down action for the key_input' do51 allow(builder).to receive(:get_device).and_return(keyboard)52 expect(keyboard).to receive(:create_key_down).with(key)53 allow(builder).to receive(:tick)54 builder.send('key_action', key, action: :create_key_down)55 end56 it 'should create a key_up action for the key_input' do57 allow(builder).to receive(:get_device).and_return(keyboard)58 expect(keyboard).to receive(:create_key_up).with(key)59 allow(builder).to receive(:tick)60 builder.send('key_action', key, action: :create_key_up)61 end62 it 'should pass the key_input to the #tick method' do63 allow(builder).to receive(:get_device).and_return(keyboard)64 allow(keyboard).to receive(:create_key_down)65 expect(builder).to receive(:tick).with(keyboard)66 builder.send('key_action', key, action: :create_key_down)67 end68 it 'should return itself' do69 allow(builder).to receive(:get_device).and_return(keyboard)70 allow(keyboard).to receive(:create_key_down)71 allow(builder).to receive(:tick).with(keyboard)72 expect(builder.send('key_action', key, action: :create_key_down)).to eq(builder)73 end74 end # when performing a key action75 it 'should create a key_down action' do76 expect(builder).to receive(:key_action).with(element, key, action: :create_key_down, device: 'name')...
key_actions.rb
Source:key_actions.rb
...92 self93 end94 private95 def key_action(*args, action: nil, device: nil)96 key_input = get_device(device) || key_inputs.first97 click(args.shift) if args.first.is_a? Element98 key_input.send(action, args.last)99 tick(key_input)100 self101 end102 end # KeyActions103 end # WebDriver104end # Selenium
key_input
Using AI Code Generation
1search_box = driver.find_element(:name, 'q')2search_box.send_keys("Selenium WebDriver")3search_box.send_keys(:enter)4wait = Selenium::WebDriver::Wait.new(:timeout => 10)5wait.until { driver.title.downcase.start_with? "selenium webdriver" }
key_input
Using AI Code Generation
1search_box = driver.find_element(:name, 'q')2search_button = driver.find_element(:name, 'btnG')3search_result_stats = driver.find_element(:id, 'resultStats')4search_results = driver.find_elements(:class, 'r')5search_result_links = driver.find_elements(:class, 'r')6 puts result.find_element(:tag_name, 'a').attribute('href')7Method Description find_element(:how, what) Find the first element matching the given criteria find_elements(:how, what) Find all elements matching the given criteria find_element(:id, 'id') Find the first element matching the given id find_elements(:id, 'id') Find all elements matching the given id find_element(:link_text, 'text') Find the first element matching the given link text find_elements(:link_text, 'text') Find all elements matching the given link text find_element(:partial_link_text, 'text') Find the first element matching the given partial link text find_elements(:partial_link_text, 'text') Find all elements matching the given partial link text find_element(:
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!