Best Selenium code snippet using Selenium.WebDriver.warn
driver.rb
Source:driver.rb
...10 SPECIAL_OPTIONS = %i[browser clear_local_storage clear_session_storage].freeze11 attr_reader :app, :options12 def self.load_selenium13 require 'selenium-webdriver'14 warn "Warning: You're using an unsupported version of selenium-webdriver, please upgrade." if Gem.loaded_specs['selenium-webdriver'].version < Gem::Version.new('3.5.0')15 rescue LoadError => e16 raise e if e.message !~ /selenium-webdriver/17 raise LoadError, "Capybara's selenium driver is unable to load `selenium-webdriver`, please install the gem and add `gem 'selenium-webdriver'` to your Gemfile if you are using bundler."18 end19 def browser20 unless @browser21 # if firefox?22 # options[:desired_capabilities] ||= {}23 # options[:desired_capabilities][:unexpectedAlertBehaviour] = "ignore"24 # end25 @processed_options = options.reject { |key, _val| SPECIAL_OPTIONS.include?(key) }26 @browser = Selenium::WebDriver.for(options[:browser], @processed_options)27 extend ChromeDriver if chrome?28 extend MarionetteDriver if marionette?29 main = Process.pid30 at_exit do31 # Store the exit status of the test run since it goes away after calling the at_exit proc...32 @exit_status = $ERROR_INFO.status if $ERROR_INFO.is_a?(SystemExit)33 quit if Process.pid == main34 exit @exit_status if @exit_status # Force exit with stored status35 end36 end37 @browser38 end39 def initialize(app, **options)40 self.class.load_selenium41 @session = nil42 @app = app43 @browser = nil44 @exit_status = nil45 @frame_handles = {}46 @options = DEFAULT_OPTIONS.merge(options)47 @node_class = ::Capybara::Selenium::Node48 end49 def visit(path)50 browser.navigate.to(path)51 end52 def refresh53 browser.navigate.refresh54 end55 def go_back56 browser.navigate.back57 end58 def go_forward59 browser.navigate.forward60 end61 def html62 browser.page_source63 end64 def title65 browser.title66 end67 def current_url68 browser.current_url69 end70 def find_xpath(selector)71 browser.find_elements(:xpath, selector).map(&method(:build_node))72 end73 def find_css(selector)74 browser.find_elements(:css, selector).map(&method(:build_node))75 end76 def wait?; true; end77 def needs_server?; true; end78 def execute_script(script, *args)79 browser.execute_script(script, *native_args(args))80 end81 def evaluate_script(script, *args)82 result = execute_script("return #{script}", *args)83 unwrap_script_result(result)84 end85 def evaluate_async_script(script, *args)86 browser.manage.timeouts.script_timeout = Capybara.default_max_wait_time87 result = browser.execute_async_script(script, *native_args(args))88 unwrap_script_result(result)89 end90 def save_screenshot(path, **_options)91 browser.save_screenshot(path)92 end93 def reset!94 # Use instance variable directly so we avoid starting the browser just to reset the session95 return unless @browser96 navigated = false97 timer = Capybara::Helpers.timer(expire_in: 10)98 begin99 unless navigated100 # Only trigger a navigation if we haven't done it already, otherwise it101 # can trigger an endless series of unload modals102 begin103 @browser.manage.delete_all_cookies104 clear_storage105 # rescue Selenium::WebDriver::Error::NoSuchAlertError106 # # Handle a bug in Firefox/Geckodriver where it thinks it needs an alert modal to exist107 # # for no good reason108 # retry109 rescue Selenium::WebDriver::Error::UnhandledError # rubocop:disable Lint/HandleExceptions110 # delete_all_cookies fails when we've previously gone111 # to about:blank, so we rescue this error and do nothing112 # instead.113 end114 @browser.navigate.to('about:blank')115 end116 navigated = true117 # Ensure the page is empty and trigger an UnhandledAlertError for any modals that appear during unload118 until find_xpath('/html/body/*').empty?119 raise Capybara::ExpectationNotMet, 'Timed out waiting for Selenium session reset' if timer.expired?120 sleep 0.05121 end122 rescue Selenium::WebDriver::Error::UnhandledAlertError, Selenium::WebDriver::Error::UnexpectedAlertOpenError123 # This error is thrown if an unhandled alert is on the page124 # Firefox appears to automatically dismiss this alert, chrome does not125 # We'll try to accept it126 begin127 @browser.switch_to.alert.accept128 sleep 0.25 # allow time for the modal to be handled129 rescue modal_error130 # The alert is now gone131 if current_url != 'about:blank'132 begin133 # If navigation has not occurred attempt again and accept alert134 # since FF may have dismissed the alert at first attempt135 @browser.navigate.to('about:blank')136 sleep 0.1 # slight wait for alert137 @browser.switch_to.alert.accept138 rescue modal_error # rubocop:disable Metrics/BlockNesting, Lint/HandleExceptions139 # alert now gone, should mean navigation happened140 end141 end142 end143 # try cleaning up the browser again144 retry145 end146 end147 def switch_to_frame(frame)148 case frame149 when :top150 @frame_handles[browser.window_handle] = []151 browser.switch_to.default_content152 when :parent153 # would love to use browser.switch_to.parent_frame here154 # but it has an issue if the current frame is removed from within it155 @frame_handles[browser.window_handle].pop156 browser.switch_to.default_content157 @frame_handles[browser.window_handle].each { |fh| browser.switch_to.frame(fh) }158 else159 @frame_handles[browser.window_handle] ||= []160 @frame_handles[browser.window_handle] << frame.native161 browser.switch_to.frame(frame.native)162 end163 end164 def current_window_handle165 browser.window_handle166 end167 def window_size(handle)168 within_given_window(handle) do169 size = browser.manage.window.size170 [size.width, size.height]171 end172 end173 def resize_window_to(handle, width, height)174 within_given_window(handle) do175 browser.manage.window.resize_to(width, height)176 end177 end178 def maximize_window(handle)179 within_given_window(handle) do180 browser.manage.window.maximize181 end182 sleep 0.1 # work around for https://code.google.com/p/selenium/issues/detail?id=7405183 end184 def fullscreen_window(handle)185 within_given_window(handle) do186 browser.manage.window.full_screen187 end188 end189 def close_window(handle)190 raise ArgumentError, 'Not allowed to close the primary window' if handle == window_handles.first191 within_given_window(handle) do192 browser.close193 end194 end195 def window_handles196 browser.window_handles197 end198 def open_new_window199 browser.execute_script('window.open();')200 end201 def switch_to_window(handle)202 browser.switch_to.window handle203 end204 def accept_modal(_type, **options)205 yield if block_given?206 modal = find_modal(options)207 modal.send_keys options[:with] if options[:with]208 message = modal.text209 modal.accept210 message211 end212 def dismiss_modal(_type, **options)213 yield if block_given?214 modal = find_modal(options)215 message = modal.text216 modal.dismiss217 message218 end219 def quit220 @browser&.quit221 rescue Selenium::WebDriver::Error::SessionNotCreatedError, Errno::ECONNREFUSED # rubocop:disable Lint/HandleExceptions222 # Browser must have already gone223 rescue Selenium::WebDriver::Error::UnknownError => e224 unless silenced_unknown_error_message?(e.message) # Most likely already gone225 # probably already gone but not sure - so warn226 warn "Ignoring Selenium UnknownError during driver quit: #{e.message}"227 end228 ensure229 @browser = nil230 end231 def invalid_element_errors232 [233 ::Selenium::WebDriver::Error::StaleElementReferenceError,234 ::Selenium::WebDriver::Error::UnhandledError,235 ::Selenium::WebDriver::Error::ElementNotVisibleError,236 ::Selenium::WebDriver::Error::InvalidSelectorError, # Work around a race condition that can occur with chromedriver and #go_back/#go_forward237 ::Selenium::WebDriver::Error::ElementNotInteractableError,238 ::Selenium::WebDriver::Error::ElementClickInterceptedError,239 ::Selenium::WebDriver::Error::InvalidElementStateError,240 ::Selenium::WebDriver::Error::ElementNotSelectableError,241 ::Selenium::WebDriver::Error::ElementNotSelectableError,242 ::Selenium::WebDriver::Error::NoSuchElementError, # IE243 ::Selenium::WebDriver::Error::InvalidArgumentError # IE244 ]245 end246 def no_such_window_error247 Selenium::WebDriver::Error::NoSuchWindowError248 end249private250 def w3c?251 browser && browser.capabilities.is_a?(Selenium::WebDriver::Remote::W3C::Capabilities)252 end253 def marionette?254 firefox? && w3c?255 end256 def firefox?257 browser_name == :firefox258 end259 def chrome?260 browser_name == :chrome261 end262 def edge?263 browser_name == :edge264 end265 def ie?266 %i[internet_explorer ie].include?(browser_name)267 end268 def browser_name269 browser.browser270 end271 def native_args(args)272 args.map { |arg| arg.is_a?(Capybara::Selenium::Node) ? arg.native : arg }273 end274 def clear_storage275 if options[:clear_session_storage]276 if @browser.respond_to? :session_storage277 @browser.session_storage.clear278 else279 warn 'sessionStorage clear requested but is not available for this driver'280 end281 end282 if options[:clear_local_storage] # rubocop:disable Style/GuardClause283 if @browser.respond_to? :local_storage284 @browser.local_storage.clear285 else286 warn 'localStorage clear requested but is not available for this driver'287 end288 end289 end290 def modal_error291 Selenium::WebDriver::Error::NoSuchAlertError292 end293 def within_given_window(handle)294 original_handle = current_window_handle295 if handle == original_handle296 yield297 else298 switch_to_window(handle)299 result = yield300 switch_to_window(original_handle)...
selenium_spec_marionette.rb
Source:selenium_spec_marionette.rb
...84 # Ensure browser is actually quit so we don't leave hanging processe85 RSpec::Mocks.space.proxy_for(@original_browser).reset86 @original_browser.quit87 end88 it 'warns UnknownError returned during quit because the browser is probably already gone' do89 allow(@driver).to receive(:warn)90 allow(@driver.browser).to(91 receive(:quit)92 .and_raise(Selenium::WebDriver::Error::UnknownError, 'random message')93 )94 expect { @driver.quit }.not_to raise_error95 expect(@driver.instance_variable_get(:@browser)).to be_nil96 expect(@driver).to have_received(:warn).with(/random message/)97 end98 it 'ignores silenced UnknownError returned during quit because the browser is almost definitely already gone' do99 allow(@driver).to receive(:warn)100 allow(@driver.browser).to(101 receive(:quit)102 .and_raise(Selenium::WebDriver::Error::UnknownError, 'Error communicating with the remote browser')103 )104 expect { @driver.quit }.not_to raise_error105 expect(@driver.instance_variable_get(:@browser)).to be_nil106 expect(@driver).not_to have_received(:warn)107 end108 end109 end110 context 'storage' do111 describe '#reset!' do112 it 'does not clear either storage by default' do113 @session = TestSessions::SeleniumMarionette114 @session.visit('/with_js')115 @session.find(:css, '#set-storage').click116 @session.reset!117 @session.visit('/with_js')118 expect(@session.driver.browser.local_storage.keys).not_to be_empty119 expect(@session.driver.browser.session_storage.keys).not_to be_empty120 end121 it 'clears storage when set' do122 @session = Capybara::Session.new(:selenium_marionette_clear_storage, TestApp)123 @session.visit('/with_js')124 @session.find(:css, '#set-storage').click125 @session.reset!126 @session.visit('/with_js')127 expect(@session.driver.browser.local_storage.keys).to be_empty128 expect(@session.driver.browser.session_storage.keys).to be_empty129 end130 end131 end132 context '#refresh' do133 def extract_results(session)134 expect(session).to have_xpath("//pre[@id='results']")135 YAML.load Nokogiri::HTML(session.body).xpath("//pre[@id='results']").first.inner_html.lstrip136 end137 it 'can repost by accepting confirm' do138 @session = TestSessions::SeleniumMarionette139 @session.visit('/form')140 @session.select('Sweden', from: 'form_region')141 @session.click_button('awesome')142 sleep 1143 expect do144 @session.accept_confirm(wait: 0.1) do145 @session.refresh146 sleep 2147 end148 sleep 1149 end.to change { extract_results(@session)['post_count'] }.by(1)150 end151 end152end153RSpec.describe Capybara::Selenium::Node do154 context '#click' do155 it 'warns when attempting on a table row' do156 session = TestSessions::SeleniumMarionette157 session.visit('/tables')158 tr = session.find(:css, '#agent_table tr:first-child')159 allow(tr.base).to receive(:warn)160 tr.click161 expect(tr.base).to have_received(:warn).with(/Clicking the first cell in the row instead/)162 end163 it 'should allow multiple modifiers', requires: [:js] do164 session = TestSessions::SeleniumMarionette165 session.visit('with_js')166 # Firefox v62+ doesn't generate an event for control+shift+click167 session.find(:css, '#click-test').click(:alt, :ctrl, :meta)168 # it also triggers a contextmenu event when control is held so don't check click type169 expect(session).to have_link('Has been alt control meta')170 end171 end172end...
logger.rb
Source:logger.rb
...28 # Selenium::WebDriver.logger.output = 'selenium.log'29 #30 # @example Use logger manually31 # Selenium::WebDriver.logger.info('This is info message')32 # Selenium::WebDriver.logger.warn('This is warning message')33 #34 class Logger35 extend Forwardable36 include ::Logger::Severity37 def_delegators :@logger, :debug, :debug?,38 :info, :info?,39 :warn, :warn?,40 :error, :error?,41 :fatal, :fatal?,42 :level43 def initialize44 @logger = create_logger($stdout)45 end46 def output=(io)47 # `Logger#reopen` was added in Ruby 2.348 if @logger.respond_to?(:reopen)49 @logger.reopen(io)50 else51 @logger = create_logger(io)52 end53 end54 #55 # For Ruby < 2.3 compatibility56 # Based on https://github.com/ruby/ruby/blob/ruby_2_3/lib/logger.rb#L25057 #58 def level=(severity)59 if severity.is_a?(Integer)60 @logger.level = severity61 else62 case severity.to_s.downcase63 when 'debug'.freeze64 @logger.level = DEBUG65 when 'info'.freeze66 @logger.level = INFO67 when 'warn'.freeze68 @logger.level = WARN69 when 'error'.freeze70 @logger.level = ERROR71 when 'fatal'.freeze72 @logger.level = FATAL73 when 'unknown'.freeze74 @logger.level = UNKNOWN75 else76 raise ArgumentError, "invalid log level: #{severity}"77 end78 end79 end80 #81 # Returns IO object used by logger internally.82 #83 # Normally, we would have never needed it, but we want to84 # use it as IO object for all child processes to ensure their85 # output is redirected there.86 #87 # It is only used in debug level, in other cases output is suppressed.88 #89 # @api private90 #91 def io92 if debug?93 @logger.instance_variable_get(:@logdev).instance_variable_get(:@dev)94 else95 File.new(Platform.null_device, 'w')96 end97 end98 #99 # Marks code as deprecated with replacement.100 #101 # @param [String] old102 # @param [String] new103 #104 def deprecate(old, new)105 warn "[DEPRECATION] #{old} is deprecated. Use #{new} instead."106 end107 private108 def create_logger(output)109 logger = ::Logger.new(output)110 logger.progname = 'Selenium'111 logger.level = ($DEBUG ? DEBUG : WARN)112 logger.formatter = proc do |severity, time, progname, msg|113 "#{time.strftime('%F %T')} #{severity} #{progname} #{msg}\n"114 end115 logger116 end117 end # Logger118 end # WebDriver119end # Selenium...
warn
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Selenium WebDriver"2driver.find_element(:name, 'btnG').click3 driver.find_element(:name, 'q').send_keys "Selenium WebDriver"4 driver.find_element(:name, 'btnG').click5 driver.find_element(:name, 'q').send_keys "Selenium WebDriver"6 driver.find_element(:name, 'btnG').click7 driver.find_element(:name, 'q').send_keys "Selenium WebDriver"8 driver.find_element(:name, 'btnG').click9 driver.find_element(:name, 'q').send_keys "Selenium WebDriver"10 driver.find_element(:name, 'btnG').click
warn
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').click
warn
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').click7driver.find_element(:name, 'q').send_keys "Selenium WebDriver"8driver.find_element(:name, 'btnG').click9driver.find_element(:name, 'q').send_keys "Selenium WebDriver"10driver.find_element(:name, 'btnG').click11driver.find_element(:name, 'q').send_keys "Selenium WebDriver"12driver.find_element(:name, 'btnG').click
warn
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Selenium"2driver.find_element(:name, 'btnG').click3driver.find_element(:name, 'q').send_keys "Selenium"4driver.find_element(:name, 'btnG').click5driver.find_element(:name, 'q').send_keys "Selenium"6driver.find_element(:name, 'btnG').click7driver.find_element(:name, 'q').send_keys "Selenium"8driver.find_element(:name, 'btnG').click9driver.find_element(:name, 'q').send_keys "Selenium"10driver.find_element(:name, 'btnG').click11driver.find_element(:name, 'q').send_keys "Selenium"12driver.find_element(:name, 'btnG').click
warn
Using AI Code Generation
1driver.find_element(:id, "gbqfq").send_keys "Selenium WebDriver"2driver.find_element(:id, "gbqfb").click3element = driver.find_element(:id, "gbqfq")4options.add_cookie(:name => 'foo', :value => 'bar')5driver.find_element(:id, "gbqfq").send_keys "Selenium WebDriver"6driver.find_element(:id, "gbqfb").click7element = driver.find_element(:id, "gbqfq")8wait = Selenium::WebDriver::Wait.new(:timeout => 10)9wait.until { driver.title.downcase.start_with? "selenium" }10driver.action.tap(driver.find_element(:id
warn
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').click7driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'8driver.find_element(:name, 'btnG').click9driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'10driver.find_element(:name, 'btnG').click11driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'12driver.find_element(:name, 'btnG').click
warn
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'2driver.find_element(:name, 'btnK').click3driver.find_element(:link_text, 'Selenium WebDriver').click4driver.find_element(:link_text, 'Downloads').click5driver.find_element(:link_text, 'Ruby').click6driver.find_element(:link_text, 'Downloads').click7driver.find_element(:link_text, '3.141.0').click
warn
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').click7driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'8driver.find_element(:name, 'btnG').click9driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'10driver.find_element(:name, 'btnG').click11driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'12driver.find_element(:name, 'btnG').click
warn
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'2driver.find_element(:name, 'btnK').click3driver.find_element(:link_text, 'Selenium WebDriver').click4driver.find_element(:link_text, 'Downloads').click5driver.find_element(:link_text, 'Ruby').click6driver.find_element(:link_text, 'Downloads').click7driver.find_element(:link_text, '3.141.0').click
warn
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Selenium WebDriver"2driver.find_element(:name, 'btnG').click3 driver.find_element(:name, 'q').send_keys "Selenium WebDriver"4 driver.find_element(:name, 'btnG').click5 driver.find_element(:name, 'q').send_keys "Selenium WebDriver"6 driver.find_element(:name, 'btnG').click7 driver.find_element(:name, 'q').send_keys "Selenium WebDriver"8 driver.find_element(:name, 'btnG').click9 driver.find_element(:name, 'q').send_keys "Selenium WebDriver"10 driver.find_element(:name, 'btnG').click
warn
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').click7driver.find_element(:name, 'q').send_keys "Selenium WebDriver"8driver.find_element(:name, 'btnG').click9driver.find_element(:name, 'q').send_keys "Selenium WebDriver"10driver.find_element(:name, 'btnG').click11driver.find_element(:name, 'q').send_keys "Selenium WebDriver"12driver.find_element(:name, 'btnG').click
warn
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Selenium"2driver.find_element(:name, 'btnG').click3driver.find_element(:name, 'q').send_keys "Selenium"4driver.find_element(:name, 'btnG').click5driver.find_element(:name, 'q').send_keys "Selenium"6driver.find_element(:name, 'btnG').click7driver.find_element(:name, 'q').send_keys "Selenium"8driver.find_element(:name, 'btnG').click9driver.find_element(:name, 'q').send_keys "Selenium"10driver.find_element(:name, 'btnG').click11driver.find_element(:name, 'q').send_keys "Selenium"12driver.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!!