How to use window_handle method of Selenium.WebDriver.Remote Package

Best Selenium code snippet using Selenium.WebDriver.Remote.window_handle

driver.rb

Source:driver.rb Github

copy

Full Screen

...139 end140 def switch_to_frame(frame)141 case frame142 when :top143 @frame_handles[browser.window_handle] = []144 browser.switch_to.default_content145 when :parent146 # would love to use browser.switch_to.parent_frame here147 # but it has an issue if the current frame is removed from within it148 @frame_handles[browser.window_handle].pop149 browser.switch_to.default_content150 @frame_handles[browser.window_handle].each { |fh| browser.switch_to.frame(fh) }151 else152 @frame_handles[browser.window_handle] ||= []153 @frame_handles[browser.window_handle] << frame.native154 browser.switch_to.frame(frame.native)155 end156 end157 def current_window_handle158 browser.window_handle159 end160 def window_size(handle)161 within_given_window(handle) do162 size = browser.manage.window.size163 [size.width, size.height]164 end165 end166 def resize_window_to(handle, width, height)167 within_given_window(handle) do168 # Don't set the size if already set - See https://github.com/mozilla/geckodriver/issues/643169 if marionette? && (window_size(handle) == [width, height])170 {}171 else172 browser.manage.window.resize_to(width, height)173 end174 end175 end176 def maximize_window(handle)177 within_given_window(handle) do178 browser.manage.window.maximize179 end180 sleep 0.1 # work around for https://code.google.com/p/selenium/issues/detail?id=7405181 end182 def close_window(handle)183 within_given_window(handle) do184 browser.close185 end186 end187 def window_handles188 browser.window_handles189 end190 def open_new_window191 browser.execute_script('window.open();')192 end193 def switch_to_window(handle)194 browser.switch_to.window handle195 end196 def within_window(locator)197 handle = find_window(locator)198 browser.switch_to.window(handle) { yield }199 end200 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 ,...

Full Screen

Full Screen

window_handle

Using AI Code Generation

copy

Full Screen

1driver.execute_script("window.open()")2driver.switch_to.window(driver.window_handles.last)3driver.execute_script("window.open()")4driver.switch_to.window(driver.window_handles.last)5driver.switch_to.window(driver.window_handles.first)

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