<?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>Test Automation Blog &#187; Java</title>
	<atom:link href="http://testautomationblog.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://testautomationblog.com</link>
	<description>On software test automation and quality assurance</description>
	<lastBuildDate>Sun, 16 May 2010 09:33:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Selecting Unnamed Pop-up Windows in Selenium</title>
		<link>http://testautomationblog.com/2009/10/08/selecting-unnamed-pop-up-windows-in-selenium/</link>
		<comments>http://testautomationblog.com/2009/10/08/selecting-unnamed-pop-up-windows-in-selenium/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 18:11:04 +0000</pubDate>
		<dc:creator>TAB</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[automation problems]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Pop-Up]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://testautomationblog.com/?p=133</guid>
		<description><![CDATA[If the application you are testing tends to open new browser windows using the target="_blank" technique, you might have some problems with Selenium.]]></description>
			<content:encoded><![CDATA[<p><em>If the application you are testing tends to open new browser windows using the target="_blank" technique, you might have some problems with Selenium.</em></p>
<p>One of the reasons I find <a href="http://seleniumhq.org/" target="_blank">Selenium</a> a great test automation tool for the web  is that it lets you easily run your test scripts on different browsers without too much effort. Most scripts will run smoothly on IE, Firefox and even on more exotic browsers like Safari, Opera and Chrome without needing browser-specific code or having to tweak the test for a particular browser.  One of the few areas where you are likely to face problems is the handling of pop-up windows.  If the application you are testing tends to open new browser windows using the <strong>target="_blank" </strong>technique, you might have some problems with Selenium. The standard <strong>.selectWindow()</strong> method provided by Selenium requires either a title of the pop-up window (which is often the same as the title of the main window) or a name or a variable name (which are both missing, when the window is opened using a target="_blank" link).</p>
<p>You will need a workaround to select such windows and unfortunately the workarounds do not always work the same for all browsers. The following code snippets might be useful if you run into the same problem. They are tested on the <strong>*chrome</strong> and <strong>*iehta</strong> Selenium modes (with Internet Explorer 7 and Firefox 3).</p>
<p>Here is how you can select the pop-up window which is opened after a target="_blank" link is clicked. This actually combines several hacks and workarounds, but works at the end (at least for me):</p>
<pre class="java">        <span style="color: #808080; font-style: italic;">//A hack to select unnamed pop-up windows</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>browser.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;*iehta&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">//Find out if Selenium substituted the blank target...</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>selenium.<span style="color: #006600;">isElementPresent</span><span style="color: #66cc66;">&#40;</span>
                       <span style="color: #ff0000;">&quot;//a[starts-with(@target,'selenium_blank')]&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #808080; font-style: italic;">// ...and make it notice the new window on IE</span>
                selenium.<span style="color: #006600;">openWindow</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>, selenium.<span style="color: #006600;">getAttribute</span><span style="color: #66cc66;">&#40;</span>
                      <span style="color: #ff0000;">&quot;//a[starts-with(@target,'selenium_blank')]@target&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
        <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> winNames = selenium.<span style="color: #006600;">getAllWindowNames</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #808080; font-style: italic;">//the last one is the pop-up window we want</span>
        selenium.<span style="color: #006600;">waitForPopUp</span><span style="color: #66cc66;">&#40;</span>winNames<span style="color: #66cc66;">&#91;</span>winNames.<span style="color: #006600;">length</span><span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#93;</span>, timeout<span style="color: #66cc66;">&#41;</span>;
        selenium.<span style="color: #006600;">selectWindow</span><span style="color: #66cc66;">&#40;</span>winNames<span style="color: #66cc66;">&#91;</span>winNames.<span style="color: #006600;">length</span><span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>And here is how you can go back to the main window after you finished working with the pop-up:</p>
<pre class="java">        <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> winTitles = selenium.<span style="color: #006600;">getAllWindowTitles</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        selenium.<span style="color: #006600;">selectWindow</span><span style="color: #66cc66;">&#40;</span>winTitles<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</pre>
]]></content:encoded>
			<wfw:commentRss>http://testautomationblog.com/2009/10/08/selecting-unnamed-pop-up-windows-in-selenium/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
