<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Switch between new tabs in Ruby &#8211; TestAutomationLabs</title>
	<atom:link href="https://testautomationlabs.com/tag/switch-between-new-tabs-in-ruby/feed/" rel="self" type="application/rss+xml" />
	<link>https://testautomationlabs.com</link>
	<description>Quality Matters</description>
	<lastBuildDate>Sat, 22 Feb 2025 15:19:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://testautomationlabs.com/wp-content/uploads/2023/08/imageedit_2_5514276710-150x150.png</url>
	<title>Switch between new tabs in Ruby &#8211; TestAutomationLabs</title>
	<link>https://testautomationlabs.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Handling a new browser window with Watir</title>
		<link>https://testautomationlabs.com/handling-a-new-browser-window-with-watir/</link>
					<comments>https://testautomationlabs.com/handling-a-new-browser-window-with-watir/#respond</comments>
		
		<dc:creator><![CDATA[TestAutomationLabs]]></dc:creator>
		<pubDate>Tue, 15 Aug 2023 15:04:03 +0000</pubDate>
				<category><![CDATA[Automation Testing]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Handling a new browser window]]></category>
		<category><![CDATA[Switch between new tabs in Ruby]]></category>
		<guid isPermaLink="false">https://testautomationlabs.com/?p=297</guid>

					<description><![CDATA[Introduction Hello readers, today in this article we will learn about Handling a new browser window or tab in Watir. Watir stands for Web Application Testing in Ruby. Using the window class, you can handle new browser tabs or windows that open during the test. The window class allows us to switch between different browser [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Introduction</h2>



<p>Hello readers, today in this article we will learn about Handling a new browser window or tab in Watir. Watir stands for Web Application Testing in Ruby. Using the  <code>window</code>  class, you can handle new browser tabs or windows that open during the test. The window class allows us to switch between different browser windows or tabs. Let&#8217;s see how you can handle new browser tabs in Watir Ruby.</p>



<h2 class="wp-block-heading">Handling a new browser window in Ruby.</h2>



<p>Handling a new browser window in Ruby is not so hard, During the test when you click a link or perform an action that opens a new browser tab or window, you have to switch to that new tab to interact with its elements or contents. For that first, you need to save the identifiers of new open windows and then switch to the new tab.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
require &#039;watir&#039;

# Initialize the browser
browser = Watir::Browser.new :chrome

# Open a webpage
browser.goto &#039;https://www.example.com&#039;

# Click a link that opens a new tab/window
browser.link(id: &#039;link_element_id&#039;).click

# Store the handles of all open windows/tabs
original_window = browser.window

# Switch to the new tab/window
new_window = browser.window(title: &#039;New Tab web page title&#039;)
new_window.use

# Perform actions in the new tab/window
# ...

# Switch back to the original tab/window
original_window.use

# Close the browser
browser.close
</pre></div>


<h3 class="wp-block-heading">Switching Between Tabs</h3>



<p>To switch to the new browser tab or window you can use <code>use</code> method</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
# Switch to a specific window/tab by its title
browser.window(title: &#039;New Tab web page title&#039;).use

# Switch back to the original window/tab
original_window.use
</pre></div>


<h3 class="wp-block-heading">Handling Multiple Tabs</h3>



<p>If you have multiple new tabs in your web browser and you want to switch between them based on their order, you can simply use <code>windows</code> method.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
# Get an array of all open windows/tabs
all_windows = browser.windows

# Switch to the first window
all_windows&#x5B;0].use

# Switch to the second window
all_windows&#x5B;1].use
</pre></div>


<h3 class="wp-block-heading">Closing a Browser Tab</h3>



<p>To close the browser tab/window you can use <code>close</code> method.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
# Close the current tab/window
browser.window.use.close
</pre></div>


<p>Remember that the above code snippets are only for example and might need to be adapted to your specific use case and website structure. Also, Watir provides different attributes to locate browser windows, for example, by title, URL, and index, so you can choose any of these based on your testing needs. If you are interested in exploring more on Browser Window in Ruby Watir, you can simply read the <a href="http://watir.com/guides/windows/" data-type="link" data-id="http://watir.com/guides/windows/" target="_blank" rel="noopener">documentation</a> provided by Watir.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://testautomationlabs.com/handling-a-new-browser-window-with-watir/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
