How to use send_keys method of Capybara Package

Best Capybara code snippet using Capybara.send_keys

app_spec.rb

Source:app_spec.rb Github

copy

Full Screen

...19 # currently, these are just enough to make them unique, so they will be accepted20 types.each do |type|21 case type22 when :method23 native.send_keys('o')24 native.send_keys('m')25 when :integer26 native.send_keys('j')27 native.send_keys('i')28 when :string29 native.send_keys('j')30 native.send_keys('s')31 else raise "WHAT TYPE IS THIS: #{type.inspect}"32 end33 end34 end35 def assert_game_over36 expect(capybara).to have_css '.summary'37 end38 def assert_game_in_progress39 expect(capybara).to have_no_css '.summary'40 end41 def native42 capybara.find('html').native43 end44 def init_phantom45 capybara.visit '/' unless capybara.current_url == '/'46 capybara.execute_script("localStorage.clear()")47 end48 def click_game_link(game_name)49 capybara.click_link game_name50 expect(capybara.current_path).to eq "/games/#{game_name.gsub " ", "_"}"51 end52 def return_from_game53 expect(capybara.current_path).to start_with '/games'54 native.send_keys(:Return)55 wait_upto(0.5) { expect(capybara.current_path).to eq '/' }56 end57 def wait_upto(duration)58 start_time = Time.now59 while (Time.now - start_time) < duration60 begin61 yield62 return63 rescue RSpec::Expectations::ExpectationNotMetError64 sleep duration/565 end66 end67 yield68 end69 def code_contains(text)70 line1 = capybara.all('.CodeMirror-line:first-of-type').first71 expect(line1.text).to eq '1 + 2'72 end73 def assert_time_change(before:, sleep_for:, after:)74 expect(capybara.find('.stats .time').text).to eq before75 sleep(sleep_for)76 expect(capybara.find('.stats .time').text).to eq after77 end78 def assert_message(matcher)79 info_box = capybara.find('.infoBox')80 expect(info_box.text).to match matcher81 end82 def assert_completed_games(times: nil, corrects: nil, incorrects: nil)83 expect(84 capybara.all('.available_games .completed.stats .status').map(&:text)85 ).to be_all { |text| text == 'Completed' }86 times and expect(87 capybara.all('.available_games .completed.stats .time').map(&:text)88 ).to eq times89 corrects and expect(90 capybara.all('.available_games .completed.stats .correct').map(&:text)91 ).to eq corrects92 incorrects and expect(93 capybara.all('.available_games .completed.stats .incorrect').map(&:text)94 ).to eq incorrects95 end96 def assert_current_game_task(text)97 current_element = capybara.all('.currentElement').map(&:text).join.delete(" ")98 expect(current_element).to eq text99 end100 def assert_score_change(before_correct: nil,101 before_incorrect: nil,102 before_time: nil,103 guesses: [],104 after_correct: nil,105 after_incorrect: nil,106 after_time: nil,107 summary: nil108 )109 correct = capybara.find '.stats .correct'110 incorrect = capybara.find '.stats .incorrect'111 time = capybara.find '.stats .time'112 before_correct and expect(correct.text ).to eq before_correct.to_s113 before_incorrect and expect(incorrect.text).to eq before_incorrect.to_s114 before_time and expect(time.text ).to eq before_time.to_s115 summary and expect(capybara).to have_no_css '.summary'116 guess *guesses117 after_correct and expect(correct.text ).to eq after_correct.to_s118 after_incorrect and expect(incorrect.text).to eq after_incorrect.to_s119 after_time and expect(time.text ).to eq after_time.to_s120 summary and expect(capybara.find('.summary').text).to match summary.to_s121 end122 def assert_totals(completed: nil, correct: nil, incorrect: nil, time: nil)123 completed and expect(capybara.find('.total_score .games_completed').text).to eq completed.to_s124 correct and expect(capybara.find('.total_score .correct' ).text).to eq correct.to_s125 incorrect and expect(capybara.find('.total_score .incorrect' ).text).to eq incorrect.to_s126 time and expect(capybara.find('.total_score .time' ).text).to eq time.to_s127 end128 end129 def browser130 page.find('html').native131 end132 def capybara133 page134 end135 def ss136 capybara.save_and_open_screenshot137 end138 def lol139 @synseer ||= Browser.new(page)140 end141 before(:each) { lol.init_phantom }142 example 'new user plays their first game' do143 # When I go to the root page, it shows me a listing of syntax games and scores144 capybara.visit '/'145 lol.assert_completed_games times: [], corrects: [], incorrects: []146 # I have completed 0 games, and have a total time of 0 seconds, 0 correct, and 0 incorrect147 lol.assert_totals completed: 0, correct: 0, incorrect: 0, time: '0:00'148 # When I click on the "integer addition" example, it takes me to "/games/integer_addition"149 lol.click_game_link 'integer addition'150 # There is a code display containing the text "1 + 2"151 lol.code_contains '1 + 2'152 # After 1 second, my time has increased from 0 to 1153 lol.assert_time_change before: '0:00', sleep_for: 1, after: '0:01'154 # I see that it is waiting for me to classify the "1 + 2" expression155 lol.assert_current_game_task '1+2'156 # I guess that it is a method, and my "correct" count increases from 0 to 1, it confirms my answer157 lol.assert_score_change before_correct: 0, guesses: [:method], after_correct: 1158 lol.assert_message /\bcorrect.*call/i159 # I see that it is waiting for me to classify the "1" expression160 lol.assert_current_game_task '1'161 # I incorrectl guess that it is a method, my "correct" count stays at 1 while my "incorrect" count increases from 0 to 1162 lol.assert_score_change guesses: [:method],163 before_correct: 1, before_incorrect: 0,164 after_correct: 1, after_incorrect: 1165 # It tells me what the correct answer is,166 lol.assert_message /\bincorrect.*int/i167 # I guess that it is an integer, and my "correct" count increases from 1 to 2168 lol.assert_score_change before_correct: 1, guesses: [:integer], after_correct: 2169 # I see that it is waiting for me to classify the "2" expression170 lol.assert_current_game_task '2'171 # I guess that it is a method, and my "correct" count stays at 2 while my "incorrect" count increases from 1 to 2172 lol.assert_score_change guesses: [:method],173 before_correct: 2, before_incorrect: 1,174 after_correct: 2, after_incorrect: 2175 # I guess that it is an integer, and my "correct" count increases from 2 to 3,176 # and I have completed the challenge177 lol.assert_score_change guesses: [:integer],178 before_correct: 2,179 after_correct: 3,180 summary: /\b2 incorrect\b/i # just some stat that should be in it181 # After 1 second, my time has not increased182 lol.assert_time_change before: '0:01', sleep_for: 1, after: '0:01'183 # I press "return" and it takes me back to the root page184 lol.return_from_game185 # Now I see that all games are scored as unattempted, except "integer addition", which shows my score of 1 second, 3 correct, and 2 incorrect186 lol.assert_completed_games times: ['0:01'], corrects: ['3'], incorrects: ['2']187 # I have completed 1 game, and have a total of 1 seconds, 3 correct, and 2 incorrect188 lol.assert_totals completed: 1, correct: 3, incorrect: 2, time: '0:01'189 end190 it 'allows me to repeat games with "r", accept next games with "enter", and tracks only my best scores' do191 # Since I am new, I cannot play a previous game192 capybara.visit "/"193 lol.assert_totals completed: 0 # sanity check194 browser.send_keys('r')195 sleep 0.1196 expect(capybara.current_path).to eq '/'197 # I play the first game, 'integer addition', with one error198 browser.send_keys :Return199 sleep 0.1200 expect(capybara.current_path).to eq '/games/integer_addition'201 lol.guess :method # <- error202 lol.guess :method, :integer, :integer203 browser.send_keys(:Return)204 sleep 0.1205 expect(capybara.current_path).to eq '/'206 # I have one error207 lol.assert_totals completed: 1, incorrect: 1208 # I press "r" to replay the game209 browser.send_keys("r")210 sleep 0.1211 expect(capybara.current_path).to eq '/games/integer_addition'212 # I have no errors213 lol.guess :method, :integer, :integer214 browser.send_keys(:Return)215 sleep 0.1216 expect(capybara.current_path).to eq '/'217 # better score replaces the worse score218 lol.assert_totals completed: 1, incorrect: 0219 # I press "r" to replay the game220 browser.send_keys("r")221 sleep 0.1222 expect(capybara.current_path).to eq '/games/integer_addition'223 # I play have one error224 lol.guess :integer # <-- error225 lol.guess :method, :integer, :integer226 browser.send_keys(:Return)227 sleep 0.1228 expect(capybara.current_path).to eq '/'229 # worse score does not beat the better score230 lol.assert_totals completed: 1, incorrect: 0231 # I press enter to play the next game232 browser.send_keys(:Return)233 sleep 0.1234 expect(capybara.current_path).to eq '/games/string_literal'235 # I play with no errors236 lol.guess :string237 # I press enter to go to the main page238 browser.send_keys(:Return)239 sleep 0.1240 expect(capybara.current_path).to eq '/'241 # I have two games played with no errors242 lol.assert_totals completed: 2, incorrect: 0243 # Enter wraps back around244 browser.send_keys(:Return)245 sleep 0.1246 expect(capybara.current_path).to eq '/games/integer_addition'247 # I can press "r" to retry, without going back to the main page248 lol.assert_current_game_task '1+2'249 lol.guess :method, :integer250 lol.assert_current_game_task '2'251 lol.guess :integer252 lol.assert_game_over253 browser.send_keys("r")254 sleep 0.1255 lol.assert_game_in_progress256 lol.assert_current_game_task '1+2'257 end258 it 'filters my keys to the available options as I type, accepts my entry once unique, clears when I press esc, deletes when I press backspace' do259 capybara.visit '/'260 capybara.click_link 'integer addition'261 get_potentials = -> {262 capybara.all('.potential_entries .syntax_node').map(&:text) +263 capybara.all('.potential_entries .entry_group').map(&:text)264 }265 # filters266 lol.assert_current_game_task '1+2'267 expect(get_potentials.call).to include "control-flow"268 browser.send_keys("c")269 expect(get_potentials.call).to include "if statement"270 expect(get_potentials.call).to_not include 'until true'271 browser.send_keys("l")272 expect(get_potentials.call).to include 'until true'273 expect(get_potentials.call).to_not include "if statement"274 # clears275 lol.assert_current_game_task '1+2'276 expect(get_potentials.call).to_not include "control-flow"277 browser.send_keys(:Escape)278 expect(get_potentials.call).to include "control-flow"279 # deletes280 lol.assert_current_game_task '1+2'281 expect(get_potentials.call).to include "control-flow"282 browser.send_keys("c")283 expect(get_potentials.call).to include "loop"284 browser.send_keys("l")285 expect(get_potentials.call).to include "while true"286 browser.send_keys(:Backspace)287 browser.send_keys(:Backspace)288 expect(get_potentials.call).to include "control-flow"289 # accepts290 lol.assert_current_game_task '1+2'291 browser.send_keys("o")292 expect(get_potentials.call).to_not include "control-flow"293 browser.send_keys("m")294 expect(get_potentials.call).to include "control-flow"295 end296end...

Full Screen

Full Screen

web_tables_page.rb

Source:web_tables_page.rb Github

copy

Full Screen

...23 end24 def add_user25 user = Users.main_user26 @add_btn.click27 @first_name.send_keys(user.firstname)28 @last_name.send_keys(user.lastname)29 @email.send_keys(user.email)30 @age.send_keys('21')31 @salary.send_keys('3000')32 @department.send_keys('Sports')33 @submit_btn.click34 end35 def edit_user(name, field, value)36 Capybara.within find(@client_rows.path, text: "#{name}") do37 case field38 when 'first name'39 @first_name.send_keys(value)40 when 'last name'41 @last_name.send_keys(value)42 when 'email'43 @email.send_keys(value)44 when 'age'45 @age.send_keys(value)46 when 'salary'47 @salary.send_keys(value)48 when 'department'49 @department.send_keys(value)50 end51 @submit_btn.click52 end53 end54 def delete_user(name)55 Capybara.within find(@client_rows.path, text: "#{name}") do56 @delete_btn.click57 end58 end59 def user_in_table(name, status)60 @search_box.set("")61 @search_box.send_keys(name)62 Capybara.within find(@client_rows.path, match: :first) do63 case status64 when 'visible'65 expect(@client_rows).to have_text name66 when 'not visible'67 expect(@client_rows).not_to have_text name68 end69 end70 end71end...

Full Screen

Full Screen

browser_test.rb

Source:browser_test.rb Github

copy

Full Screen

...11visit '/test.html'12sleep(5)13element = find("div", :id => "myeditablediv1")14if OS.mac?15 element.send_keys("mac OS detected")16 element.send_keys [:meta, :shift, :arrow_left]17else18 element.send_keys("Linux OS detected")19 element.send_keys [:shift, :home]20end21print("Sent keys to editable div")22screenshot_and_save_page23sleep(10)24element = find("input", :id => "mytextfield1")25if OS.mac?26 element.send_keys("mac OS detected")27 element.send_keys [:meta, :shift, :arrow_left]28else29 element.send_keys("Linux OS detected")30 element.send_keys [:shift, :home]31end32print("Sent keys to text field")33screenshot_and_save_page34sleep(10)...

Full Screen

Full Screen

send_keys

Using AI Code Generation

copy

Full Screen

1 visit('/')2 fill_in('q', :with => 'capybara')3 find_button('Google Search').click4element = driver.find_element(:name, 'q')5search_field = browser.text_field(:name => 'q')6search_field.set("capybara")7search_field.send_keys(:enter)8search_field = browser.text_field(:name => 'q')9search_field.set("capybara")10search_field.send_keys(:enter)11search_field = browser.text_field(:name => 'q')12search_field.set("capybara")13search_field.send_keys(:enter)14search_field = browser.text_field(:name => 'q')15search_field.set("capybara")16search_field.send_keys(:enter)

Full Screen

Full Screen

send_keys

Using AI Code Generation

copy

Full Screen

1visit('/')2fill_in('q', :with => 'selenium')3send_keys(:enter)

Full Screen

Full Screen

send_keys

Using AI Code Generation

copy

Full Screen

1find_field('q').send_keys :enter2find_field('q').native.send_keys :enter3find_field('q').native.send_keys :enter

Full Screen

Full Screen

send_keys

Using AI Code Generation

copy

Full Screen

1visit('/')2fill_in('q', :with => 'Hello World!')3click_button('Google Search')4if has_content?('Hello World!')

Full Screen

Full Screen

send_keys

Using AI Code Generation

copy

Full Screen

1session = Capybara::Session.new(:selenium)2session.visit('/')3session.fill_in('q', :with => 'Selenium')4session.find(:xpath, '//input[3]').click5session.send_keys(:enter)

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Capybara automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful