Best Selenium code snippet using Selenium.WebDriver.text
driver.rb
Source:driver.rb
...200 def accept_modal(_type, options={})201 yield if block_given?202 modal = find_modal(options)203 modal.send_keys options[:with] if options[:with]204 message = modal.text205 modal.accept206 message207 end208 def dismiss_modal(_type, options={})209 yield if block_given?210 modal = find_modal(options)211 message = modal.text212 modal.dismiss213 message214 end215 def quit216 @browser.quit if @browser217 rescue Selenium::WebDriver::Error::SessionNotCreatedError, Errno::ECONNREFUSED218 # Browser must have already gone219 rescue Selenium::WebDriver::Error::UnknownError => e220 unless silenced_unknown_error_message?(e.message) # Most likely already gone221 # probably already gone but not sure - so warn222 warn "Ignoring Selenium UnknownError during driver quit: #{e.message}"223 end224 ensure225 @browser = nil226 end227 def invalid_element_errors228 [::Selenium::WebDriver::Error::StaleElementReferenceError,229 ::Selenium::WebDriver::Error::UnhandledError,230 ::Selenium::WebDriver::Error::ElementNotVisibleError,231 ::Selenium::WebDriver::Error::InvalidSelectorError, # Work around a race condition that can occur with chromedriver and #go_back/#go_forward232 ::Selenium::WebDriver::Error::ElementNotInteractableError,233 ::Selenium::WebDriver::Error::ElementClickInterceptedError,234 ::Selenium::WebDriver::Error::InvalidElementStateError,235 ::Selenium::WebDriver::Error::ElementNotSelectableError,236 ]237 end238 def no_such_window_error239 Selenium::WebDriver::Error::NoSuchWindowError240 end241 # @api private242 def marionette?243 firefox? && browser && @w3c244 end245 # @api private246 def firefox?247 browser_name == "firefox"248 end249 # @api private250 def chrome?251 browser_name == "chrome"252 end253 # @deprecated This method is being removed254 def browser_initialized?255 super && !@browser.nil?256 end257 private258 # @api private259 def browser_name260 options[:browser].to_s261 end262 def modal_error263 if defined?(Selenium::WebDriver::Error::NoSuchAlertError)264 Selenium::WebDriver::Error::NoSuchAlertError265 else266 Selenium::WebDriver::Error::NoAlertPresentError267 end268 end269 def find_window(locator)270 handles = browser.window_handles271 return locator if handles.include? locator272 original_handle = browser.window_handle273 handles.each do |handle|274 switch_to_window(handle)275 if (locator == browser.execute_script("return window.name") ||276 browser.title.include?(locator) ||277 browser.current_url.include?(locator))278 switch_to_window(original_handle)279 return handle280 end281 end282 raise Capybara::ElementNotFound, "Could not find a window identified by #{locator}"283 end284 def insert_modal_handlers(accept, response_text)285 prompt_response = if accept286 if response_text.nil?287 "default_text"288 else289 "'#{response_text.gsub("\\", "\\\\\\").gsub("'", "\\\\'")}'"290 end291 else292 'null'293 end294 script = <<-JS295 if (typeof window.capybara === 'undefined') {296 window.capybara = {297 modal_handlers: [],298 current_modal_status: function() {299 return [this.modal_handlers[0].called, this.modal_handlers[0].modal_text];300 },301 add_handler: function(handler) {302 this.modal_handlers.unshift(handler);303 },304 remove_handler: function(handler) {305 window.alert = handler.alert;306 window.confirm = handler.confirm;307 window.prompt = handler.prompt;308 },309 handler_called: function(handler, str) {310 handler.called = true;311 handler.modal_text = str;312 this.remove_handler(handler);313 }314 };315 };316 var modal_handler = {317 prompt: window.prompt,318 confirm: window.confirm,319 alert: window.alert,320 called: false321 }322 window.capybara.add_handler(modal_handler);323 window.alert = window.confirm = function(str = "") {324 window.capybara.handler_called(modal_handler, str.toString());325 return #{accept ? 'true' : 'false'};326 }327 window.prompt = function(str = "", default_text = "") {328 window.capybara.handler_called(modal_handler, str.toString());329 return #{prompt_response};330 }331 JS332 execute_script script333 end334 def within_given_window(handle)335 original_handle = self.current_window_handle336 if handle == original_handle337 yield338 else339 switch_to_window(handle)340 result = yield341 switch_to_window(original_handle)342 result343 end344 end345 def find_modal(options={})346 # Selenium has its own built in wait (2 seconds)for a modal to show up, so this wait is really the minimum time347 # Actual wait time may be longer than specified348 wait = Selenium::WebDriver::Wait.new(349 timeout: options.fetch(:wait, session_options.default_max_wait_time) || 0 ,350 ignore: modal_error)351 begin352 wait.until do353 alert = @browser.switch_to.alert354 regexp = options[:text].is_a?(Regexp) ? options[:text] : Regexp.escape(options[:text].to_s)355 alert.text.match(regexp) ? alert : nil356 end357 rescue Selenium::WebDriver::Error::TimeOutError358 raise Capybara::ModalNotFound.new("Unable to find modal dialog#{" with #{options[:text]}" if options[:text]}")359 end360 end361 def find_headless_modal(options={})362 # Selenium has its own built in wait (2 seconds)for a modal to show up, so this wait is really the minimum time363 # Actual wait time may be longer than specified364 wait = Selenium::WebDriver::Wait.new(365 timeout: options.fetch(:wait, session_options.default_max_wait_time) || 0 ,366 ignore: modal_error)367 begin368 wait.until do369 called, alert_text = evaluate_script('window.capybara && window.capybara.current_modal_status()')370 if called371 execute_script('window.capybara && window.capybara.modal_handlers.shift()')372 regexp = options[:text].is_a?(Regexp) ? options[:text] : Regexp.escape(options[:text].to_s)373 if alert_text.match(regexp)374 alert_text375 else376 raise Capybara::ModalNotFound.new("Unable to find modal dialog#{" with #{options[:text]}" if options[:text]}")377 end378 elsif called.nil?379 # page changed so modal_handler data has gone away380 warn "Can't verify modal text when page change occurs - ignoring" if options[:text]381 ""382 else383 nil384 end385 end386 rescue Selenium::WebDriver::Error::TimeOutError387 raise Capybara::ModalNotFound.new("Unable to find modal dialog#{" with #{options[:text]}" if options[:text]}")388 end389 end390 def silenced_unknown_error_message?(msg)391 silenced_unknown_error_messages.any? { |r| msg =~ r }392 end393 def silenced_unknown_error_messages394 [ /Error communicating with the remote browser/ ]395 end396 def unwrap_script_result(arg)397 case arg398 when Array399 arg.map { |e| unwrap_script_result(e) }400 when Hash401 arg.each { |k, v| arg[k] = unwrap_script_result(v) }...
Class - Settings Page.rb
Source:Class - Settings Page.rb
...86 wait.until {@driver.find_element(CONTACTS_OPTN).displayed?}87 contacts_selection = @driver.find_element(CONTACTS_OPTN)88 contacts_selection.click89 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)90 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}91 end92 def open_crews()93 wait = Selenium::WebDriver::Wait.new(:timeout => 5)94 wait.until {@driver.find_element(CREWS_OPTN).displayed?}95 crews_selection = @driver.find_element(CREWS_OPTN)96 crews_selection.click97 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)98 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}99 end100 def open_customerassets()101 wait = Selenium::WebDriver::Wait.new(:timeout => 5)102 wait.until {@driver.find_element(CUSTOMERASSETS_OPTN).displayed?}103 customerassets_selection = @driver.find_element(CUSTOMERASSETS_OPTN)104 customerassets_selection.click105 end106 def open_employees()107 wait = Selenium::WebDriver::Wait.new(:timeout => 5)108 wait.until {@driver.find_element(EMPLOYEES_OPTN).displayed?}109 employees_selection = @driver.find_element(EMPLOYEES_OPTN)110 employees_selection.click111 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)112 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}113 end114 def open_documents()115 wait = Selenium::WebDriver::Wait.new(:timeout => 5)116 wait.until {@driver.find_element(DOCUMENTS_OPTN).displayed?}117 documents_selection = @driver.find_element(DOCUMENTS_OPTN)118 documents_selection.click119 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)120 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}121 end122 def open_timeentries()123 wait = Selenium::WebDriver::Wait.new(:timeout => 5)124 wait.until {@driver.find_element(TIMEENTRIES_OPTN).displayed?}125 timeentries_selection = @driver.find_element(TIMEENTRIES_OPTN)126 timeentries_selection.click127 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)128 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}129 end130 def open_timesheets()131 wait = Selenium::WebDriver::Wait.new(:timeout => 5)132 wait.until {@driver.find_element(TIMESHEETS_OPTN).displayed?}133 timesheets_selection = @driver.find_element(TIMESHEETS_OPTN)134 timesheets_selection.click135 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)136 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}137 end138 def open_invoicelines()139 wait = Selenium::WebDriver::Wait.new(:timeout => 5)140 wait.until {@driver.find_element(INVOICELINES_OPTN).displayed?}141 invoicelines_selection = @driver.find_element(INVOICELINES_OPTN)142 invoicelines_selection.click143 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)144 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}145 end146 def open_joblines()147 wait = Selenium::WebDriver::Wait.new(:timeout => 5)148 wait.until {@driver.find_element(JOBLINES_OPTN).displayed?}149 joblines_selection = @driver.find_element(JOBLINES_OPTN)150 joblines_selection.click151 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)152 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}153 wait3 = Selenium::WebDriver::Wait.new(:timeout => 5)154 wait3.until {@driver.find_element(JOBLINES_TOP_REFNUMBER).displayed?}155 end156 def open_productsandservices()157 wait = Selenium::WebDriver::Wait.new(:timeout => 5)158 wait.until {@driver.find_element(PRODUCTSANDSERVICES_OPTN).displayed?}159 productsandservices_selection = @driver.find_element(PRODUCTSANDSERVICES_OPTN)160 productsandservices_selection.click161 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)162 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}163 end164 def open_productcategories()165 wait = Selenium::WebDriver::Wait.new(:timeout => 5)166 wait.until {@driver.find_element(PRODUCTCATEGORIES_OPTN).displayed?}167 productcategories_selection = @driver.find_element(PRODUCTCATEGORIES_OPTN)168 productcategories_selection.click169 end170 def open_announcements()171 wait = Selenium::WebDriver::Wait.new(:timeout => 5)172 wait.until {@driver.find_element(ANNOUNCEMENTS_OPTN).displayed?}173 announcements_selection = @driver.find_element(ANNOUNCEMENTS_OPTN)174 announcements_selection.click175 end176 def open_releasenotes()177 wait = Selenium::WebDriver::Wait.new(:timeout => 5)178 wait.until {@driver.find_element(RELEASENOTES_OPTN).displayed?}179 releasenotes_selection = @driver.find_element(RELEASENOTES_OPTN)180 releasenotes_selection.click181 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)182 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}183 end184 def open_royalties()185 wait = Selenium::WebDriver::Wait.new(:timeout => 5)186 wait.until {@driver.find_element(ROYALTIES_OPTN).displayed?}187 royalties_selection = @driver.find_element(ROYALTIES_OPTN)188 royalties_selection.click189 end190 #CSS Methods: Lists191 def open_assetcategories()192 wait = Selenium::WebDriver::Wait.new(:timeout => 5)193 wait.until {@driver.find_element(ASSETCATEGORIES_OPTN).displayed?}194 assetcategories_selection = @driver.find_element(ASSETCATEGORIES_OPTN)195 assetcategories_selection.click196 end197 def open_classes()198 wait = Selenium::WebDriver::Wait.new(:timeout => 5)199 wait.until {@driver.find_element(CLASSES_OPTN).displayed?}200 classes_selection = @driver.find_element(CLASSES_OPTN)201 classes_selection.click202 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)203 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}204 end205 def open_paymentmethods()206 wait = Selenium::WebDriver::Wait.new(:timeout => 5)207 wait.until {@driver.find_element(PAYMENTMETHODS_OPTN).displayed?}208 paymentmethods_selection = @driver.find_element(PAYMENTMETHODS_OPTN)209 paymentmethods_selection.click210 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)211 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}212 end213 def open_priceagreements()214 wait = Selenium::WebDriver::Wait.new(:timeout => 5)215 wait.until {@driver.find_element(PRICEAGREEMENTS_OPTN).displayed?}216 priceagreements_selection = @driver.find_element(PRICEAGREEMENTS_OPTN)217 priceagreements_selection.click218 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)219 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}220 end221 def open_terms()222 wait = Selenium::WebDriver::Wait.new(:timeout => 5)223 wait.until {@driver.find_element(TERMS_OPTN).displayed?}224 terms_selection = @driver.find_element(TERMS_OPTN)225 terms_selection.click226 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)227 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}228 end229 def open_trucks()230 wait = Selenium::WebDriver::Wait.new(:timeout => 5)231 wait.until {@driver.find_element(TRUCKS_OPTN).displayed?}232 trucks_selection = @driver.find_element(TRUCKS_OPTN)233 trucks_selection.click234 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)235 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}236 end237 def open_rmas()238 wait = Selenium::WebDriver::Wait.new(:timeout => 5)239 wait.until {@driver.find_element(RMAS_OPTN).displayed?}240 rmas_selection = @driver.find_element(RMAS_OPTN)241 rmas_selection.click242 end243 #CSS Methods: Setup244 def open_companysettings()245 wait = Selenium::WebDriver::Wait.new(:timeout => 5)246 wait.until {@driver.find_element(COMPANYSETTINGS_OPTN).displayed?}247 companysettings_selection = @driver.find_element(COMPANYSETTINGS_OPTN)248 companysettings_selection.click249 end250 def open_chartofaccounts()251 wait = Selenium::WebDriver::Wait.new(:timeout => 5)252 wait.until {@driver.find_element(CHARTOFACCOUNTS_OPTN).displayed?}253 chartofaccounts_selection = @driver.find_element(CHARTOFACCOUNTS_OPTN)254 chartofaccounts_selection.click255 end256 def open_departments()257 wait = Selenium::WebDriver::Wait.new(:timeout => 5)258 wait.until {@driver.find_element(DEPARTMENTS_OPTN).displayed?}259 departments_selection = @driver.find_element(DEPARTMENTS_OPTN)260 departments_selection.click261 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)262 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}263 end264 def open_locations()265 wait = Selenium::WebDriver::Wait.new(:timeout => 5)266 wait.until {@driver.find_element(LOCATIONS_OPTN).displayed?}267 locations_selection = @driver.find_element(LOCATIONS_OPTN)268 locations_selection.click269 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)270 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}271 end272 def open_paymentprocessing()273 wait = Selenium::WebDriver::Wait.new(:timeout => 5)274 wait.until {@driver.find_element(PAYMENTPROCESSING_OPTN).displayed?}275 paymentprocessing_selection = @driver.find_element(PAYMENTPROCESSING_OPTN)276 paymentprocessing_selection.click277 end278 def open_roles()279 wait = Selenium::WebDriver::Wait.new(:timeout => 5)280 wait.until {@driver.find_element(ROLES_OPTN).displayed?}281 roles_selection = @driver.find_element(ROLES_OPTN)282 roles_selection.click283 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)284 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}285 end286 def open_taxratesandagencies()287 wait = Selenium::WebDriver::Wait.new(:timeout => 5)288 wait.until {@driver.find_element(TAXRATESANDAGENCIES_OPTN).displayed?}289 taxrates_selection = @driver.find_element(TAXRATESANDAGENCIES_OPTN)290 taxrates_selection.click291 wait2 = Selenium::WebDriver::Wait.new(:timeout => 5)292 wait2.until {@driver.find_element(class: "Counter_Message").text != "0 records"}293 end294end...
text
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Hello World"2driver.find_element(:name, 'btnK').click3driver.find_element(:name, 'q').send_keys "Hello World"4driver.find_element(:name, 'btnK').click5driver.find_element(:name, 'q').send_keys "Hello World"6driver.find_element(:name, 'btnK').click7driver.find_element(:name, 'q').send_keys "Hello World"8driver.find_element(:name, 'btnK').click9driver.find_element(:name, 'q').send_keys "Hello World"10driver.find_element(:name, 'btnK').click11driver.find_element(:name, 'q').send_keys "Hello World"12driver.find_element(:name, 'btnK').click
text
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys 'Selenium'2driver.find_element(:name, 'btnK').click3driver.find_element(:name, 'q').send_keys 'Selenium'4driver.find_element(:name, 'btnK').click5driver.find_element(:name, 'q').send_keys 'Selenium'6driver.find_element(:name, 'btnK').click7driver.find_element(:name, 'q').send_keys 'Selenium'8driver.find_element(:name, 'btnK').click9driver.find_element(:name, 'q').send_keys 'Selenium'10driver.find_element(:name, 'btnK').click11driver.find_element(:name, 'q').send_keys 'Selenium'12driver.find_element(:name, 'btnK').click
text
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Selenium WebDriver"2driver.find_element(:name, 'btnG').click3wait = Selenium::WebDriver::Wait.new(:timeout => 10)4wait.until { driver.title.downcase.start_with? "selenium webdriver" }
text
Using AI Code Generation
1driver.find_element(:name, "").send_keys "Selenium WebDriver"2driver.find_element(:name, "btnG").click3driver.find_element(:name, "q").send_keys "Selenium WebDriver"4driver.find_element(:name, "btnG".click5driver.find_element(:name, "g").send_keys "Selenium WebDriver"6driver.find_element(:name, "btnG").click7driver.find_element(:name, 'q').send_keys "Selenium WebDriver"8driver.find_element(:name, 'btnG').click9wait = Selenium::WebDriver::Wait.new(:timeout => 10)10wait.until { driver.title.downcase.start_with? "selenium webdriver" }
text
Using AI Code Generation
1driver.find_element(:name, "q").send_keys "Selenium"2driver.find_element(:name, "btnG").click3driver.find_element(:link, "Gmail").click4driver.find_element(:link, "Images").click5driver.find_element(:link, "Maps").click6driver.find_element(:link, "Play").click
text
Using AI Code Generation
1driver.find_element(:name, "q").send_keys "Selenium WebDriver"2driver.find_element(:name, "btnG").click3driver.find_element(:name, "q").send_keys "Selenium WebDriver"4driver.find_element(:name, "btnG").click5driver.find_element(:name, "q").send_keys "Selenium WebDriver"6driver.find_element(:name, "btnG").click
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!!