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

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

target_locator_spec.rb

Source:target_locator_spec.rb Github

copy

Full Screen

...26 compliant_on browser: :firefox do27 sleep 128 end29 end30 let(:new_window) { driver.window_handles.find { |handle| handle != driver.window_handle } }31 # Safari is using GET instead of POST (W3C vs JWP)32 not_compliant_on browser: :safari do33 # Server - https://github.com/SeleniumHQ/selenium/issues/179534 not_compliant_on driver: :remote, browser: :edge do35 it 'should find the active element' do36 driver.navigate.to url_for('xhtmlTest.html')37 expect(driver.switch_to.active_element).to be_an_instance_of(WebDriver::Element)38 end39 end40 end41 # Doesn't switch to frame by id directly42 not_compliant_on browser: :safari do43 it 'should switch to a frame directly' do44 driver.navigate.to url_for('iframes.html')45 driver.switch_to.frame('iframe1')46 expect(driver.find_element(name: 'login')).to be_kind_of(WebDriver::Element)47 end48 end49 it 'should switch to a frame by Element' do50 driver.navigate.to url_for('iframes.html')51 iframe = driver.find_element(tag_name: 'iframe')52 driver.switch_to.frame(iframe)53 expect(driver.find_element(name: 'login')).to be_kind_of(WebDriver::Element)54 end55 not_compliant_on browser: :phantomjs do56 it 'should switch to parent frame' do57 driver.navigate.to url_for('iframes.html')58 iframe = driver.find_element(tag_name: 'iframe')59 driver.switch_to.frame(iframe)60 expect(driver.find_element(name: 'login')).to be_kind_of(WebDriver::Element)61 driver.switch_to.parent_frame62 expect(driver.find_element(id: 'iframe_page_heading')).to be_kind_of(WebDriver::Element)63 end64 end65 # Safari Note - Ensure Popup Blocker turned off to prevent failures66 it 'should switch to a window and back when given a block' do67 driver.navigate.to url_for('xhtmlTest.html')68 driver.find_element(link: 'Open new window').click69 wait.until { driver.window_handles.size == 2 }70 expect(driver.title).to eq('XHTML Test Page')71 driver.switch_to.window(new_window) do72 wait.until { driver.title == 'We Arrive Here' }73 end74 wait.until { driver.title == 'XHTML Test Page' }75 end76 not_compliant_on browser: :safari do77 it 'should handle exceptions inside the block' do78 driver.navigate.to url_for('xhtmlTest.html')79 driver.find_element(link: 'Open new window').click80 wait.until { driver.window_handles.size == 2 }81 expect(driver.title).to eq('XHTML Test Page')82 expect do83 driver.switch_to.window(new_window) { raise 'foo' }84 end.to raise_error(RuntimeError, 'foo')85 expect(driver.title).to eq('XHTML Test Page')86 end87 end88 # Safari does not want to click that link89 not_compliant_on browser: :safari do90 it 'should switch to a window without a block' do91 driver.navigate.to url_for('xhtmlTest.html')92 driver.find_element(link: 'Open new window').click93 wait.until { driver.window_handles.size == 2 }94 expect(driver.title).to eq('XHTML Test Page')95 driver.switch_to.window(new_window)96 expect(driver.title).to eq('We Arrive Here')97 end98 end99 # Safari does not want to click that link100 not_compliant_on browser: :safari do101 it 'should use the original window if the block closes the popup' do102 driver.navigate.to url_for('xhtmlTest.html')103 driver.find_element(link: 'Open new window').click104 wait.until { driver.window_handles.size == 2 }105 expect(driver.title).to eq('XHTML Test Page')106 driver.switch_to.window(new_window) do107 wait.until { driver.title == 'We Arrive Here' }108 driver.close109 end110 expect(driver.current_url).to include('xhtmlTest.html')111 expect(driver.title).to eq('XHTML Test Page')112 end113 end114 not_compliant_on browser: [:ie, :safari] do115 context 'with more than two windows' do116 it 'should close current window when more than two windows exist' do117 driver.navigate.to url_for('xhtmlTest.html')118 wait_for_element(link: 'Create a new anonymous window')119 driver.find_element(link: 'Create a new anonymous window').click120 wait.until { driver.window_handles.size == 2 }121 driver.find_element(link: 'Open new window').click122 wait.until { driver.window_handles.size == 3 }123 driver.switch_to.window(driver.window_handle) { driver.close }124 expect(driver.window_handles.size).to eq 2125 end126 it 'should close another window when more than two windows exist' do127 driver.navigate.to url_for('xhtmlTest.html')128 wait_for_element(link: 'Create a new anonymous window')129 driver.find_element(link: 'Create a new anonymous window').click130 wait.until { driver.window_handles.size == 2 }131 driver.find_element(link: 'Open new window').click132 wait.until { driver.window_handles.size == 3 }133 window_to_close = driver.window_handles.last134 driver.switch_to.window(window_to_close) { driver.close }135 expect(driver.window_handles.size).to eq 2136 end137 it 'should iterate over open windows when current window is not closed' do138 driver.navigate.to url_for('xhtmlTest.html')139 wait_for_element(link: 'Create a new anonymous window')140 driver.find_element(link: 'Create a new anonymous window').click141 wait.until { driver.window_handles.size == 2 }142 driver.find_element(link: 'Open new window').click143 wait.until { driver.window_handles.size == 3 }144 titles = {}145 driver.window_handles.each do |wh|146 driver.switch_to.window(wh) { titles[driver.title] = wh }147 end148 handle = titles['We Arrive Here']149 driver.switch_to.window(handle)150 expect(driver.title).to eq('We Arrive Here')151 end152 # https://github.com/SeleniumHQ/selenium/issues/3339153 not_compliant_on driver: :remote do154 it 'should iterate over open windows when current window is closed' do155 driver.navigate.to url_for('xhtmlTest.html')156 wait_for_element(link: 'Create a new anonymous window')157 driver.find_element(link: 'Create a new anonymous window').click158 wait.until { driver.window_handles.size == 2 }159 driver.find_element(link: 'Open new window').click160 wait.until { driver.window_handles.size == 3 }161 driver.close162 titles = {}163 driver.window_handles.each do |wh|164 driver.switch_to.window(wh) { titles[driver.title] = wh }165 end166 handle = titles['We Arrive Here']167 driver.switch_to.window(handle)168 expect(driver.title).to eq('We Arrive Here')169 end170 end171 end172 end173 not_compliant_on browser: :safari do174 # https://github.com/SeleniumHQ/selenium/issues/3339175 not_compliant_on driver: :remote do176 it 'should switch to a window and execute a block when current window is closed' do177 driver.navigate.to url_for('xhtmlTest.html')178 driver.find_element(link: 'Open new window').click179 wait.until { driver.window_handles.size == 2 }180 driver.switch_to.window(new_window)181 wait.until { driver.title == 'We Arrive Here' }182 driver.close183 driver.switch_to.window(driver.window_handles.first) do184 wait.until { driver.title == 'XHTML Test Page' }185 end186 expect(driver.title).to eq('XHTML Test Page')187 end188 end189 end190 it 'should switch to default content' do191 driver.navigate.to url_for('iframes.html')192 driver.switch_to.frame 0193 driver.switch_to.default_content194 driver.find_element(id: 'iframe_page_heading')195 end196 # Edge BUG - https://connect.microsoft.com/IE/feedback/details/1850030197 not_compliant_on browser: :phantomjs do...

Full Screen

Full Screen

driver.rb

Source:driver.rb Github

copy

Full Screen

...98 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 message...

Full Screen

Full Screen

base_object.rb

Source:base_object.rb Github

copy

Full Screen

...211 # iframe = find(locator)212 @@driver.switch_to.frame(locator)213 end214 def get_tab_handles215 return @@driver.window_handles216 end217 def switch_to_tab(handle)218 puts "in switch to tab"219 @@driver.switch_to.window(handle)220 end221 def switch_to_first_tab()222 puts "in switch to first tab"223 @@driver.switch_to.window(@@driver.window_handles.first)224 window_handle = @@driver.window_handles.first225 return window_handle226 end227 def switch_to_last_tab()228 puts "in switch to last tab"229 # @@driver.action.send_keys(:control + :tab).perform230 @@driver.switch_to.window(@@driver.window_handles.last)231 window_handle = @@driver.window_handles.last232 return window_handle233 end234 def close_tab(handle)235 puts "in close tab"236 @@driver.switch_to.window(handle)237 @@driver.close238 end239 def switch_to_last_window()240 @@driver.switch_to.window(@@driver.window_handles.last)241 end242 def select_menu_item(menu_item_text)243 puts 'in select_menu_item'244 menu_item = {xpath: "//div/a[contains(@class,'menu-item')]/span[contains(text(),'#{menu_item_text}')]"}245 wait_for {displayed? (menu_item)}246 click_on(menu_item)247 end248 def select_from_dropdown(dropdown, dropdown_locator, item)249 puts "In select from dropdown"250 dropdown_loc = find(dropdown)251 click_on(dropdown)252 sleep(3)253 list_item = {xpath: "#{dropdown_locator}ul/li/a[contains(text(),'#{item}')]"}#templatePulldown>span>ul>li254 click_on(list_item)...

Full Screen

Full Screen

window_handles

Using AI Code Generation

copy

Full Screen

1driver.switch_to.window(all_windows[0])2driver.switch_to.window(all_windows[1])3driver.switch_to.window(all_windows[2])4driver.switch_to.window(current_window)

Full Screen

Full Screen

window_handles

Using AI Code Generation

copy

Full Screen

1 @driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'2 @driver.find_element(:name, 'btnG').click3 @driver.find_element(:link_text, 'Selenium WebDriver').click4 @driver.find_element(:link_text, 'Download').click5 assert_equal(2, @driver.window_handles.size)6 @driver.switch_to.window(@driver.window_handles.last)7 assert(@driver.title.include?('Selenium'))8 @driver.switch_to.window(@driver.window_handles.first)9 assert(@driver.title.include?('Google'))10 @driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'11 @driver.find_element(:name, 'btnG').click12 @driver.find_element(:link_text, 'Selenium WebDriver').click13 @driver.find_element(:link_text, 'Download').click14 assert_equal(2, @driver.window_handles.size)15 @driver.switch_to.window(@driver.window_handles.last)16 assert(@driver.title.include?('Selenium'))17 @driver.switch_to.window(@driver.window_handles.first)18 assert(@driver.title.include?('Google'))

Full Screen

Full Screen

window_handles

Using AI Code Generation

copy

Full Screen

1 @driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'2 @driver.find_element(:name, 'btnG').click3 @driver.find_element(:link_text, 'Selenium WebDriver').click4 @driver.find_element(:link_text, 'Download').click5 assert_equal(2, @driver.window_handles.size)6 @driver.switch_to.window(@driver.window_handles.last)7 assert(@driver.title.include?('Selenium'))8 @driver.switch_to.window(@driver.window_handles.first)9 assert(@driver.title.include?('Google'))10 @driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'11 @driver.find_element(:name, 'btnG').click12 @driver.find_element(:link_text, 'Selenium WebDriver').click13 @driver.find_element(:link_text, 'Download').click14 assert_equal(2, @driver.window_handles.size)15 @driver.switch_to.window(@driver.window_handles.last)16 assert(@driver.title.include?('Selenium'))17 @driver.switch_to.window(@driver.window_handles.first)18 assert(@driver.title.include?('Google'))

Full Screen

Full Screen

window_handles

Using AI Code Generation

copy

Full Screen

1driver.find_element(:link_text, "Gmail").click2driver.find_element(:link_text, "Images").click3driver.switch_to.window(window_handles.last)4driver.switch_to.window(window_handles.first)5driver.find_element(:link, "Gmail").click6driver.switch_to.window(windows[1])7driver.switch_to.window(windows[0])

Full Screen

Full Screen

window_handles

Using AI Code Generation

copy

Full Screen

1driver.find_element(:link, "Gmail").click2driver.switch_to.window(windows[1])3driver.switch_to.window(windows[0])

Full Screen

Full Screen

window_handles

Using AI Code Generation

copy

Full Screen

1driver.find_element(:link_text, "Gmail").click2driver.find_element(:link_text, "Sign in").click3driver.find_element(:id, "Email").send_keys "

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