How to use time method of Selenium.WebDriver Package

Best Selenium code snippet using Selenium.WebDriver.time

driver.rb

Source:driver.rb Github

copy

Full Screen

...87 result = execute_script("return #{script}", *args)88 unwrap_script_result(result)89 end90 def evaluate_async_script(script, *args)91 browser.manage.timeouts.script_timeout = Capybara.default_max_wait_time92 result = browser.execute_async_script(script, *native_args(args))93 unwrap_script_result(result)94 end95 def save_screenshot(path, **_options)96 browser.save_screenshot(path)97 end98 def reset!99 # Use instance variable directly so we avoid starting the browser just to reset the session100 return unless @browser101 if firefox? || chrome?102 switch_to_window(window_handles.first)103 window_handles.slice(1..-1).each { |win| close_window(win) }104 end105 navigated = false106 start_time = Capybara::Helpers.monotonic_time107 begin108 unless navigated109 # Only trigger a navigation if we haven't done it already, otherwise it110 # can trigger an endless series of unload modals111 begin112 @browser.manage.delete_all_cookies113 clear_storage114 # rescue Selenium::WebDriver::Error::NoSuchAlertError115 # # Handle a bug in Firefox/Geckodriver where it thinks it needs an alert modal to exist116 # # for no good reason117 # retry118 rescue Selenium::WebDriver::Error::UnhandledError # rubocop:disable Lint/HandleExceptions119 # delete_all_cookies fails when we've previously gone120 # to about:blank, so we rescue this error and do nothing121 # instead.122 end123 @browser.navigate.to("about:blank")124 end125 navigated = true126 # Ensure the page is empty and trigger an UnhandledAlertError for any modals that appear during unload127 until find_xpath("/html/body/*").empty?128 raise Capybara::ExpectationNotMet, 'Timed out waiting for Selenium session reset' if (Capybara::Helpers.monotonic_time - start_time) >= 10129 sleep 0.05130 end131 rescue Selenium::WebDriver::Error::UnhandledAlertError, Selenium::WebDriver::Error::UnexpectedAlertOpenError132 # This error is thrown if an unhandled alert is on the page133 # Firefox appears to automatically dismiss this alert, chrome does not134 # We'll try to accept it135 begin136 @browser.switch_to.alert.accept137 sleep 0.25 # allow time for the modal to be handled138 rescue modal_error139 # The alert is now gone140 if current_url != "about:blank"141 begin142 # If navigation has not occurred attempt again and accept alert143 # since FF may have dismissed the alert at first attempt144 @browser.navigate.to("about:blank")145 sleep 0.1 # slight wait for alert146 @browser.switch_to.alert.accept147 rescue modal_error # rubocop:disable Metrics/BlockNesting, Lint/HandleExceptions148 # alert now gone, should mean navigation happened149 end150 end151 end152 # try cleaning up the browser again153 retry154 end155 end156 def switch_to_frame(frame)157 case frame158 when :top159 @frame_handles[browser.window_handle] = []160 browser.switch_to.default_content161 when :parent162 # would love to use browser.switch_to.parent_frame here163 # but it has an issue if the current frame is removed from within it164 @frame_handles[browser.window_handle].pop165 browser.switch_to.default_content166 @frame_handles[browser.window_handle].each { |fh| browser.switch_to.frame(fh) }167 else168 @frame_handles[browser.window_handle] ||= []169 @frame_handles[browser.window_handle] << frame.native170 browser.switch_to.frame(frame.native)171 end172 end173 def current_window_handle174 browser.window_handle175 end176 def window_size(handle)177 within_given_window(handle) do178 size = browser.manage.window.size179 [size.width, size.height]180 end181 end182 def resize_window_to(handle, width, height)183 within_given_window(handle) do184 # Don't set the size if already set - See https://github.com/mozilla/geckodriver/issues/643185 if marionette? && (window_size(handle) == [width, height])186 {}187 else188 browser.manage.window.resize_to(width, height)189 end190 end191 end192 def maximize_window(handle)193 within_given_window(handle) do194 browser.manage.window.maximize195 end196 sleep 0.1 # work around for https://code.google.com/p/selenium/issues/detail?id=7405197 end198 def close_window(handle)199 raise ArgumentError, "Not allowed to close the primary window" if handle == window_handles.first200 within_given_window(handle) do201 browser.close202 end203 end204 def window_handles205 browser.window_handles206 end207 def open_new_window208 browser.execute_script('window.open();')209 end210 def switch_to_window(handle)211 browser.switch_to.window handle212 end213 def accept_modal(_type, **options)214 yield if block_given?215 modal = find_modal(options)216 modal.send_keys options[:with] if options[:with]217 message = modal.text218 modal.accept219 message220 end221 def dismiss_modal(_type, **options)222 yield if block_given?223 modal = find_modal(options)224 message = modal.text225 modal.dismiss226 message227 end228 def quit229 @browser&.quit230 rescue Selenium::WebDriver::Error::SessionNotCreatedError, Errno::ECONNREFUSED # rubocop:disable Lint/HandleExceptions231 # Browser must have already gone232 rescue Selenium::WebDriver::Error::UnknownError => e233 unless silenced_unknown_error_message?(e.message) # Most likely already gone234 # probably already gone but not sure - so warn235 warn "Ignoring Selenium UnknownError during driver quit: #{e.message}"236 end237 ensure238 @browser = nil239 end240 def invalid_element_errors241 [242 ::Selenium::WebDriver::Error::StaleElementReferenceError,243 ::Selenium::WebDriver::Error::UnhandledError,244 ::Selenium::WebDriver::Error::ElementNotVisibleError,245 ::Selenium::WebDriver::Error::InvalidSelectorError, # Work around a race condition that can occur with chromedriver and #go_back/#go_forward246 ::Selenium::WebDriver::Error::ElementNotInteractableError,247 ::Selenium::WebDriver::Error::ElementClickInterceptedError,248 ::Selenium::WebDriver::Error::InvalidElementStateError,249 ::Selenium::WebDriver::Error::ElementNotSelectableError,250 ::Selenium::WebDriver::Error::ElementNotSelectableError,251 ::Selenium::WebDriver::Error::NoSuchElementError, # IE252 ::Selenium::WebDriver::Error::InvalidArgumentError # IE253 ]254 end255 def no_such_window_error256 Selenium::WebDriver::Error::NoSuchWindowError257 end258 # @api private259 def marionette?260 firefox? && browser && @w3c261 end262 # @api private263 def firefox?264 browser_name == :firefox265 end266 # @api private267 def chrome?268 browser_name == :chrome269 end270 # @api private271 def edge?272 browser_name == :edge273 end274 # @api private275 def ie?276 browser_name == :ie277 end278 # @api private279 def browser_name280 browser.browser281 end282private283 def native_args(args)284 args.map { |arg| arg.is_a?(Capybara::Selenium::Node) ? arg.native : arg }285 end286 def clear_storage287 if options[:clear_session_storage]288 if @browser.respond_to? :session_storage289 @browser.session_storage.clear290 else291 warn "sessionStorage clear requested but is not available for this driver"292 end293 end294 if options[:clear_local_storage]295 if @browser.respond_to? :local_storage296 @browser.local_storage.clear297 else298 warn "localStorage clear requested but is not available for this driver"299 end300 end301 end302 def modal_error303 if defined?(Selenium::WebDriver::Error::NoSuchAlertError)304 Selenium::WebDriver::Error::NoSuchAlertError305 else306 Selenium::WebDriver::Error::NoAlertPresentError307 end308 end309 def within_given_window(handle)310 original_handle = current_window_handle311 if handle == original_handle312 yield313 else314 switch_to_window(handle)315 result = yield316 switch_to_window(original_handle)317 result318 end319 end320 def find_modal(text: nil, **options)321 # Selenium has its own built in wait (2 seconds)for a modal to show up, so this wait is really the minimum time322 # Actual wait time may be longer than specified323 wait = Selenium::WebDriver::Wait.new(324 timeout: options.fetch(:wait, session_options.default_max_wait_time) || 0,325 ignore: modal_error326 )327 begin328 wait.until do329 alert = @browser.switch_to.alert330 regexp = text.is_a?(Regexp) ? text : Regexp.escape(text.to_s)331 alert.text.match(regexp) ? alert : nil332 end333 rescue Selenium::WebDriver::Error::TimeOutError334 raise Capybara::ModalNotFound, "Unable to find modal dialog#{" with #{text}" if text}"335 end336 end337 def silenced_unknown_error_message?(msg)338 silenced_unknown_error_messages.any? { |r| msg =~ r }...

Full Screen

Full Screen

time

Using AI Code Generation

copy

Full Screen

1element = driver.find_element(:name, 'q')2element = driver.find_element(:name, 'q')3element = driver.find_element(:name, 'q')4element = driver.find_element(:name, 'q')5element = driver.find_element(:name, 'q')6element = driver.find_element(:name, 'q')

Full Screen

Full Screen

time

Using AI Code Generation

copy

Full Screen

1wait = Selenium::WebDriver::Wait.new(:timeout => 10)2wait.until { driver.title.downcase.start_with? "google" }3wait.until { driver.title.downcase.start_with? "yahoo" }4wait.until { driver.title.downcase.start_with? "google" }5wait.until { driver.title.downcase.start_with? "google" }

Full Screen

Full Screen

time

Using AI Code Generation

copy

Full Screen

1driver.find_element(:name, 'q').send_keys('selenium webdriver')2driver.find_element(:name, 'btnK').click3driver.find_element(:name, 'q').send_keys('selenium webdriver')4driver.find_element(:name, 'btnK').click

Full Screen

Full Screen

time

Using AI Code Generation

copy

Full Screen

1element = driver.find_element(:name, 'q')2wait.until { driver.find_element(:class => "gNO89b").displayed? }3driver.find_element(:class => "gNO89b").click4wait.until { driver.find_element(:class => "LC20lb").displayed? }5driver.find_element(:class => "LC20lb").click6wait.until { driver.find_element(:class => "gLFyf").displayed? }7driver.find_element(:class => "gLFyf").click8wait.until { driver.find_element(:class => "gLFyf").displayed? }9driver.find_element(:class => "gLFyf").click10wait.until { driver.find_element(:class =>

Full Screen

Full Screen

time

Using AI Code Generation

copy

Full Screen

1search_box = driver.find_element(:name, 'q')2search_button = driver.find_element(:name, 'btnK')3wait = Selenium::WebDriver::Wait.new(:timeout => 10)4wait.until { driver.find_element(:id, 'resultStats') }5result = driver.find_element(:id, 'resultStats')6search_box = driver.find_element(:name, 'q')7search_button = driver.find_element(:name, 'btnK')8result = driver.find_element(:id, 'resultStats')9element = driver.find_element(:name, 'q')

Full Screen

Full Screen

time

Using AI Code Generation

copy

Full Screen

1driver.find_element(:name, 'q').send_keys('selenium webdriver')2driver.find_element(:name, 'btnK').click3driver.find_element(:name, 'q').send_keys('selenium webdriver')4driver.find_element(:name, 'btnK').click

Full Screen

Full Screen

time

Using AI Code Generation

copy

Full Screen

1element = driver.find_element(:name, 'q')2wait.until { driver.find_element(:class => "gNO89b").displayed? }3driver.find_element(:class => "gNO89b").click4wait.until { driver.find_element(:class => "LC20lb").displayed? }5driver.find_element(:class => "LC20lb").click6wait.until { driver.find_element(:class => "gLFyf").displayed? }7driver.find_element(:class => "gLFyf").click8wait.until { driver.find_element(:class => "gLFyf").displayed? }9driver.find_element(:class => "gLFyf").click10wait.until { driver.find_element(:class =>

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 Selenium 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