<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Relevant Codes (by Anshoo Arora)</title>
	
	<link>http://relevantcodes.com</link>
	<description>A Resource for Test Automation Development</description>
	<lastBuildDate>Thu, 10 May 2012 10:32:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/RelevantCodes" /><feedburner:info uri="relevantcodes" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license><image><link>http://creativecommons.org/licenses/by/3.0/</link><url>http://creativecommons.org/images/public/somerights20.gif</url><title>Some Rights Reserved</title></image><feedburner:emailServiceId>RelevantCodes</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><feedburner:feedFlare href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare href="http://www.bloglines.com/sub/http://feeds.feedburner.com/RelevantCodes" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:feedFlare href="http://www.plusmo.com/add?url=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://plusmo.com/res/graphics/fbplusmo.gif">Subscribe with Plusmo</feedburner:feedFlare><feedburner:feedFlare href="http://www.thefreedictionary.com/_/hp/AddRSS.aspx?http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://img.tfd.com/hp/addToTheFreeDictionary.gif">Subscribe with The Free Dictionary</feedburner:feedFlare><feedburner:feedFlare href="http://www.bitty.com/manual/?contenttype=rssfeed&amp;contentvalue=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://www.bitty.com/img/bittychicklet_91x17.gif">Subscribe with Bitty Browser</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsalloy.com/?rss=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://www.newsalloy.com/subrss3.gif">Subscribe with NewsAlloy</feedburner:feedFlare><feedburner:feedFlare href="http://www.live.com/?add=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://tkfiles.storage.msn.com/x1piYkpqHC_35nIp1gLE68-wvzLZO8iXl_JMledmJQXP-XTBOLfmQv4zhj4MhcWEJh_GtoBIiAl1Mjh-ndp9k47If7hTaFno0mxW9_i3p_5qQw">Subscribe with Live.com</feedburner:feedFlare><feedburner:feedFlare href="http://mix.excite.eu/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://image.excite.co.uk/mix/addtomix.gif">Subscribe with Excite MIX</feedburner:feedFlare><feedburner:feedFlare href="http://download.attensa.com/app/get_attensa.html?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://www.attensa.com/blogs/attensa/WindowsLiveWriter/BadgeredintoBadges_10C02/attensa_feed_button5.gif">Subscribe with Attensa for Outlook</feedburner:feedFlare><feedburner:feedFlare href="http://www.webwag.com/wwgthis.php?url=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://www.webwag.com/images/wwgthis.gif">Subscribe with Webwag</feedburner:feedFlare><feedburner:feedFlare href="http://www.podcastready.com/oneclick_bookmark.php?url=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://www.podcastready.com/images/podcastready_button.gif">Subscribe with Podcast Ready</feedburner:feedFlare><feedburner:feedFlare href="http://www.flurry.com/pushRssFeed.do?r=fb&amp;url=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://www.flurry.com/images/flurry_rss_logo2.gif">Subscribe with Flurry</feedburner:feedFlare><feedburner:feedFlare href="http://www.wikio.com/subscribe?url=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://www.wikio.com/shared/img/add2wikio.gif">Subscribe with Wikio</feedburner:feedFlare><feedburner:feedFlare href="http://www.dailyrotation.com/index.php?feed=http%3A%2F%2Ffeeds.feedburner.com%2FRelevantCodes" src="http://www.dailyrotation.com/rss-dr2.gif">Subscribe with Daily Rotation</feedburner:feedFlare><item>
		<title>Selenium PageObjects and PageFactory</title>
		<link>http://feedproxy.google.com/~r/RelevantCodes/~3/Yh3hDRE33_c/</link>
		<comments>http://relevantcodes.com/pageobjects-and-pagefactory-design-patterns-in-selenium/#comments</comments>
		<pubDate>Tue, 01 May 2012 05:54:52 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[InitElements]]></category>
		<category><![CDATA[OpenQA]]></category>
		<category><![CDATA[PageFactory]]></category>
		<category><![CDATA[PageObject]]></category>
		<category><![CDATA[WebDriver]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=5229</guid>
		<description><![CDATA[This article discusses the PageObject design pattern and the PageFactory class from WebDriver support libraries. I believe this is a very robust way to model your tests. The way this design pattern is formulated, it becomes easier making changes to the flow, logic or the UI. A few discrepancies between the C# and Java implementation of the PageFactory have been included as well.]]></description>
			<content:encoded><![CDATA[<p></p><p>This article discusses the PageObject design pattern and a factory class from WebDriver support libraries. I had a lot of fun writing this article and I believe this is a very robust way to model your tests. The way this design pattern is formulated, it becomes easier making changes to the flow, logic or the UI test code. Let&#8217;s now cover the concepts behind the design pattern and its factory class.</p>
<p class="info">I have used the <a href="http://newtours.demoaut.com">NewTours Flight Application</a>, .NET 4.0 with <a href="http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-csharp-express">Visual Express 2010</a> and <a href="http://seleniumhq.org/download/">WebDriver 2.20</a> API for writing examples in this article.</p>
<h2>PageObject Design Pattern</h2>
<p>The PageObject design pattern models areas of a UI as objects within test code. The functionality classes (PageObjects) in this design represent a logical relationship between the pages of the application. Each class is referred to as a PageObject and returns other PageObjects to facilitate the flow between pages. Because PageObjects are returned, it becomes necessary to model both successful and unsuccessful events that can occur when interacted with a page. For example, consider logging into Gmail. After entering the user details, the step either passes and navigates to the Inbox page or stays on the Login page possibly due to invalid input parameters. A pass would then return the Inbox PageObject whereas a fail would return the Login PageObject.</p>
<p>This means better tests, exception handling and reporting. It may sound a little confusing, but its quite a simple yet an elegant approach to write your tests. Let&#8217;s break down the above explanation into actual PageObjects.</p>
<p>In the code below, the following events occur:</p>
<ol>
<li>Constructor verifies if page is valid</li>
<li>Attempts login to the Flights application</li>
<li>(If successful) Returns the FindFlights PageObject</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #6666cc; font-weight: bold;">class</span> LoginPage
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> LoginPage<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #000000;">.</span><span style="color: #0000FF;">driver</span> <span style="color: #000000;">=</span> driver<span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #008000; font-style: italic;">// 1. verify if page is valid</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Title</span> <span style="color: #000000;">!=</span> <span style="color: #666666;">&quot;Welcome: Mercury Tours&quot;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #0600FF; font-weight: bold;">new</span> NoSuchWindowException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;This is not the Login page&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// return FindFlightsPage PageObject</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> FindFlightsPage <span style="color: #0600FF; font-weight: bold;">Do</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> UserName, <span style="color: #6666cc; font-weight: bold;">string</span> Password<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008000; font-style: italic;">// 2. steps to login to the Flights application</span>
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;userName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span>UserName<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;password&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span>Password<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;login&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">Click</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #008000; font-style: italic;">// 3. return FindFlights PageObject</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">new</span> FindFlightsPage<span style="color: #008000;">&#40;</span>driver<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>For the above to work, the <code>FindFlightsPage</code> PageObject must be created. In the code below, the FindFlightsPage PageObject is modeled to perform the following actions:</p>
<ol>
<li>Constructor verifies if page is valid</li>
<li>A method is called to find a flight</li>
<li>The Logout method is called and actions performed</li>
<li>Return the LoginPage PageObject</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #6666cc; font-weight: bold;">class</span> FindFlightsPage
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> FindFlightsPage<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span> 
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #000000;">.</span><span style="color: #0000FF;">driver</span> <span style="color: #000000;">=</span> driver<span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #008000; font-style: italic;">// 1. verify if page is valid</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Title</span> <span style="color: #000000;">!=</span> <span style="color: #666666;">&quot;Find a Flight: Mercury Tours:&quot;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #0600FF; font-weight: bold;">new</span> NoSuchWindowException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;This is not the FindFlights page&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// 2. method/code-block to find a flight</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> <span style="color: #0600FF; font-weight: bold;">Do</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Console<span style="color: #000000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;In FindFlightsPage.Do [Checking for Flights]&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// returns LoginPage PageObject</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> LoginPage Logout<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008000; font-style: italic;">// 3. log-off and return to LoginPage</span>
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">LinkText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;SIGN-OFF&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">Click</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">LinkText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Home&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">Click</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #008000; font-style: italic;">// 4. return the LoginPage object</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">new</span> LoginPage<span style="color: #008000;">&#40;</span>driver<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Main execution entry point (the test code) below.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="">OpenQA.Selenium</span><span style="color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="">OpenQA.Selenium.Firefox</span><span style="color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="">OpenQA.Selenium.Support.UI</span><span style="color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="">System</span><span style="color: #000000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> Program
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008000; font-style: italic;">// instantiate FirefoxDriver and navigate to NewTours flight app</span>
        <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">new</span> <span style="color: #6666cc; font-weight: bold;">FirefoxDriver</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #008000; font-style: italic;">// navigate to NewTours app</span>
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Navigate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">GoToUrl</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://newtours.demoaut.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #008000; font-style: italic;">// instantiate LoginPage</span>
        LoginPage Login <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">new</span> LoginPage<span style="color: #008000;">&#40;</span>driver<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>        
&nbsp;
        <span style="color: #008000; font-style: italic;">// Login.Do returns the FindFlightsPage PageObject</span>
        FindFlightsPage FindFlights <span style="color: #000000;">=</span> Login<span style="color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;">Do</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;test&quot;</span>, <span style="color: #666666;">&quot;test&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>FindFlights <span style="color: #000000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> 
        <span style="color: #008000;">&#123;</span> 
            <span style="color: #008000; font-style: italic;">// perform steps to find a flight</span>
            FindFlights<span style="color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;">Do</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span> 
&nbsp;
            <span style="color: #008000; font-style: italic;">// FindFlights.Logout returns LoginPage</span>
            Login <span style="color: #000000;">=</span> FindFlights<span style="color: #000000;">.</span><span style="color: #0000FF;">Logout</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span> 
        <span style="color: #008000;">&#125;</span>
&nbsp;
        Console<span style="color: #000000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Quit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As we saw above, the <code>Login.Do(args)</code> method returns the FindFlights PageObject whereas the <code>FindFlights.Logout()</code> method returns the LoginPage PageObject. We saw that the public methods of each class represent the functionality offered by the page. The real-world application of this concept will certainly contain more actions against the UI and may return a large number of PageObjects.</p>
<h2>PageFactory Class</h2>
<p>The PageFactory Class is an extension to the PageObject design pattern. It is used to initialize the elements of the PageObject or instantiate the PageObject itself (not in C# though &#8211; see the Notes section below). Annotations for elements can also be created (and recommended) as the describing properties may not always be descriptive enough to tell one object from the other.</p>
<p>The <code>InitElements</code> method of PageFactory initializes the elements of the PageObject. The code below shows PageFactory usage in detail.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="">OpenQA.Selenium</span><span style="color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="">OpenQA.Selenium.Firefox</span><span style="color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="">OpenQA.Selenium.Support.PageObjects</span><span style="color: #000000;">;</span> <span style="color: #008000; font-style: italic;">// *</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="">System</span><span style="color: #000000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> LoginPage
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>FindsBy<span style="color: #008000;">&#40;</span>How <span style="color: #000000;">=</span> How<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IWebElement userName<span style="color: #000000;">;</span> <span style="color: #008000; font-style: italic;">// How.NAME = userName</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>FindsBy<span style="color: #008000;">&#40;</span>How <span style="color: #000000;">=</span> How<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IWebElement password<span style="color: #000000;">;</span> <span style="color: #008000; font-style: italic;">// How.NAME = password</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>FindsBy<span style="color: #008000;">&#40;</span>How <span style="color: #000000;">=</span> How<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IWebElement login<span style="color: #000000;">;</span> <span style="color: #008000; font-style: italic;">// How.NAME = login</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> LoginPage<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #000000;">.</span><span style="color: #0000FF;">driver</span> <span style="color: #000000;">=</span> driver<span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> FindFlightsPage <span style="color: #0600FF; font-weight: bold;">Do</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> UserName, <span style="color: #6666cc; font-weight: bold;">string</span> Password<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>    
        userName<span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span>UserName<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        password<span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span>Password<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        login<span style="color: #000000;">.</span><span style="color: #0000FF;">Click</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">new</span> FindFlightsPage<span style="color: #008000;">&#40;</span>driver<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> Program
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">new</span> <span style="color: #6666cc; font-weight: bold;">FirefoxDriver</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Navigate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">GoToUrl</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://newtours.demoaut.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        LoginPage Login <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">new</span> LoginPage<span style="color: #008000;">&#40;</span>driver<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #008000; font-style: italic;">// initialize elements of the LoginPage class</span>
        PageFactory<span style="color: #000000;">.</span><span style="color: #0000FF;">InitElements</span><span style="color: #008000;">&#40;</span>driver, Login<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #008000; font-style: italic;">// all elements in the 'WebElements' region are now alive!</span>
        <span style="color: #008000; font-style: italic;">// FindElement or FindElements no longer required to locate elements</span>
&nbsp;
        FindFlightsPage FindFlights <span style="color: #000000;">=</span> Login<span style="color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;">Do</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;User&quot;</span>, <span style="color: #666666;">&quot;Pass&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Quit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The WebElements <code>userName</code>, <code>password</code> and <code>login</code> are not explicitly defined using property-value pairs. However, if you execute the code above, &#8216;UserName&#8217; and &#8216;Password&#8217; strings will be supplied to the relevant text fields. The <u>WebElement variable names were enough to identify the controls</u>.</p>
<p>In the above example, PageFactory.InitElements facilitates searching for elements marked with the <code>FindsBy</code> attribute by using the <code>NAME</code> property (notice: How = How.Name) to find the target element. There are other ways of object identification though and <u>it is not required to use the object property as the variable name</u> to identify it (as shown next).</p>
<p>The <code>How</code> parameter of <code>FindsBy</code> attribute is used for the object property (html tag). <code>Using=</code> then defines the corresponding value of the <code>How=</code> parameter.</p>
<p><img alt="" class="imgborder" src="/Articles/SeleniumPageObject/factory.intellisense.png" title="PageFactory IntelliSense" class="aligncenter" width="530" height="190" /></p>
<p>Until now, <code>NAME</code> property has been directly used as the variable name. This is not very flexible approach, and I was only using it to provide a quick overview. Annotations are possible, too. The WebElements can be defined by any descriptive name. In the code below, userName, password and login have been modified to txtUserName, txtPassword and txtLogin respectively.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #6666cc; font-weight: bold;">class</span> LoginPage
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>FindsBy<span style="color: #008000;">&#40;</span>How <span style="color: #000000;">=</span> How<span style="color: #000000;">.</span><span style="color: #0000FF;">XPath</span>, <span style="color: #0600FF; font-weight: bold;">Using</span> <span style="color: #000000;">=</span> <span style="color: #666666;">&quot;//input[@type='text' and @name='userName']&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IWebElement txtUserName<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>FindsBy<span style="color: #008000;">&#40;</span>How <span style="color: #000000;">=</span> How<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span>, <span style="color: #0600FF; font-weight: bold;">Using</span> <span style="color: #000000;">=</span> <span style="color: #666666;">&quot;userName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IWebElement txtPassword<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>FindsBy<span style="color: #008000;">&#40;</span>How <span style="color: #000000;">=</span> How<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span>, <span style="color: #0600FF; font-weight: bold;">Using</span> <span style="color: #000000;">=</span> <span style="color: #666666;">&quot;login&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IWebElement btnLogin<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> LoginPage<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #000000;">.</span><span style="color: #0000FF;">driver</span> <span style="color: #000000;">=</span> driver<span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> FindFlightsPage <span style="color: #0600FF; font-weight: bold;">Do</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> UserName, <span style="color: #6666cc; font-weight: bold;">string</span> Password<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        txtUserName<span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span>UserName<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        txtPasswowrd<span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span>Password<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        btnLogin<span style="color: #000000;">.</span><span style="color: #0000FF;">Click</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">new</span> FindFlightsPage<span style="color: #008000;">&#40;</span>driver<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In summary, PageFactory class can be used to initialize elements of a Page class without having to use <code>FindElement</code> or <code>FindElements</code>. Annotations can be used to supply descriptive names of target objects in the AUT to improve code readability. There are however a few differences between C# and Java implementation &#8211; Java provides greater flexibility with PageFactory (see Notes).</p>
<h3>CacheLookup</h3>
<p>One last thing that remains with PageFactory is the <code>CacheLookupAttribute</code>. This is important because it can be used to instruct the <code>InitElements</code> method to <u>cache</u> the element once its located. In other words, any attribute marked <code>[CacheLookup]</code> will not be searched over and over again &#8211; this is especially useful for elements that are always going to be there (not always true for AJAX apps). So, we can search once and cache. All elements used in this article can be defined by this declarative tag as they are static and are always present. Our LoginPage class then becomes:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #6666cc; font-weight: bold;">class</span> LoginPage
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>FindsBy<span style="color: #008000;">&#40;</span>How <span style="color: #000000;">=</span> How<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>CacheLookup<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IWebElement userName<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>FindsBy<span style="color: #008000;">&#40;</span>How <span style="color: #000000;">=</span> How<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>CacheLookup<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IWebElement password<span style="color: #000000;">;</span> 
&nbsp;
    <span style="color: #008000;">&#91;</span>FindsBy<span style="color: #008000;">&#40;</span>How <span style="color: #000000;">=</span> How<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>CacheLookup<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IWebElement login<span style="color: #000000;">;</span> 
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> LoginPage<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #000000;">.</span><span style="color: #0000FF;">driver</span> <span style="color: #000000;">=</span> driver<span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> FindFlightsPage <span style="color: #0600FF; font-weight: bold;">Do</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> UserName, <span style="color: #6666cc; font-weight: bold;">string</span> Password<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        userName<span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span>UserName<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        password<span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span>Password<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        login<span style="color: #000000;">.</span><span style="color: #0000FF;">Click</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">new</span> FindFlightsPage<span style="color: #008000;">&#40;</span>driver<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h2>Notes &#8211; Differences between C# and Java Implementation</h2>
<p>There are 3 discrepencies I found in the <a href="http://code.google.com/p/selenium/wiki/PageFactory">PageFactory documentation</a> at Google Code between the Java and C# implementation.</p>
<p>The first discrepancy is that in Java, the PageFactory.InitElements can return the PageObject. In C#, this is not the case as InitElements returns <code>void</code>. View <a href="/Articles/SeleniumPageObject/Java-InitElementsReturningPageObject.png">this image</a> for a snapshot from Google Code documentation showing Java returning the PageObject.</p>
<p>For the 2nd discrepancy, let&#8217;s refer to the documentation:</p>
<blockquote><p>&#8230; It [PageFactory] does this by first looking for an element with a matching ID attribute. If this fails, the PageFactory falls back to searching for an element by the value of its &#8220;name&#8221; attribute.</p></blockquote>
<p>The above is not the case for C# &#8211; a NoSuchElementException is thrown. The PageFactory implentation for C# only searches for elements using the ID and does not locate the elements using the NAME property, unless <code>How = How.Name</code> is explicitly specified.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #6666cc; font-weight: bold;">class</span> LoginPage
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>FindsBy<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IWebElement userName<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> LoginPage<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #000000;">.</span><span style="color: #0000FF;">driver</span> <span style="color: #000000;">=</span> driver<span style="color: #000000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> <span style="color: #0600FF; font-weight: bold;">Do</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> UserName, <span style="color: #6666cc; font-weight: bold;">string</span> Password<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008000; font-style: italic;">// userName is the NAME property, not ID</span>
        <span style="color: #008000; font-style: italic;">// element will not be located</span>
        <span style="color: #008000; font-style: italic;">// will throw a NoSuchElementException</span>
        userName<span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span>UserName<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span> 
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> Program
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">new</span> <span style="color: #6666cc; font-weight: bold;">FirefoxDriver</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Navigate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">GoToUrl</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://newtours.demoaut.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        LoginPage Login <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">new</span> LoginPage<span style="color: #008000;">&#40;</span>driver<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        PageFactory<span style="color: #000000;">.</span><span style="color: #0000FF;">InitElements</span><span style="color: #008000;">&#40;</span>driver, Login<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        Login<span style="color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;">Do</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;theUserName&quot;</span>, <span style="color: #666666;">&quot;thePassword&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Quit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><img class="imgborder" alt="" src="/Articles/SeleniumPageObject/nosuchelementexception.png" title="NoSuchElementException" class="aligncenter" width="452" height="237" /></p>
<p>The 3rd discrepancy I found was in the initial part of the same <a href="http://code.google.com/p/selenium/wiki/PageFactory">document</a> and noticed the same behavior when testing with Eclipse. The Java implementation can locate the element even without the FindsBy attribute &#8211; this isn&#8217;t the case for C#. View <a href="/Articles/SeleniumPageObject/Java-NoFindsByRequiredForID.png">this image</a> that shows this feature with Java. The below code fails to work for Gmail page for <code>Passwd</code> textBox since the [FindsBy] attribute is not specified.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #6666cc; font-weight: bold;">class</span> GmailLoginPage
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>FindsBy<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IWebElement Email<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// element will not initialize because [FindsBy] attribute is missing</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IWebElement Passwd<span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> GmailLoginPage<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #000000;">.</span><span style="color: #0000FF;">driver</span> <span style="color: #000000;">=</span> driver<span style="color: #000000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> <span style="color: #0600FF; font-weight: bold;">Do</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> UserName, <span style="color: #6666cc; font-weight: bold;">string</span> Password<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Email<span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span>UserName<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #008000; font-style: italic;">// fail here - NullReferenceException</span>
        Passwd<span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span>Password<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> Program
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">new</span> <span style="color: #6666cc; font-weight: bold;">FirefoxDriver</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Navigate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">GoToUrl</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://gmail.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        GmailLoginPage GmailLogin <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">new</span> GmailLoginPage<span style="color: #008000;">&#40;</span>driver<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        PageFactory<span style="color: #000000;">.</span><span style="color: #0000FF;">InitElements</span><span style="color: #008000;">&#40;</span>driver, GmailLogin<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        GmailLogin<span style="color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;">Do</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;theUserName&quot;</span>, <span style="color: #666666;">&quot;thePassword&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
        driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Quit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><img class="imgborder" alt="NullReferenceException" src="/Articles/SeleniumPageObject/nullreferenceexception.png" class="aligncenter" width="452" height="237" /></p>
<p>I hope you enjoyed reading this article. Thanks for visiting Relevant Codes!</p>
<img src="http://feeds.feedburner.com/~r/RelevantCodes/~4/Yh3hDRE33_c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/pageobjects-and-pagefactory-design-patterns-in-selenium/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://relevantcodes.com/pageobjects-and-pagefactory-design-patterns-in-selenium/</feedburner:origLink></item>
		<item>
		<title>Using NUnit to Execute Selenium WebDriver Tests</title>
		<link>http://feedproxy.google.com/~r/RelevantCodes/~3/q0jVRaNMcfA/</link>
		<comments>http://relevantcodes.com/using-nunit-to-execute-selenium-webdriver-tests/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 10:31:59 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[InternetExplorerDriver]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[WebDriver]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=5483</guid>
		<description><![CDATA[In this article, I will explain the basics of using NUnit as the unit/regression testing framework along with the WebDriver API to automate Web browsers. NUnit is developed using C# and uses an attribute based programming model. A great thing about NUnit is that its very easy to learn and allows us to abstract away our tests and enable us to focus on the test code.]]></description>
			<content:encoded><![CDATA[<p></p><p>In this article, I will explain the basics of using NUnit as the unit/regression testing tool for Web browsers. NUnit is developed using C#. In the example, you will see methods using certain attributes from NUnit Framework, which uses an attribute based programming model. A great thing about NUnit is that its very easy to learn and allows us to abstract away our tests and enable us to focus on the test code.</p>
<h3>Prerequisites</h3>
<ul>
<li>I am assuming you have Visual Studio (atleast Visual Express C#) installed and basic understanding of C#.</li>
<li>NUnit is a unit-testing framework that supports .NET languages. For further reading, refer to its <a href="http://nunit.com/index.php?p=docHome&#038;r=2.6">documentation</a>. If you do not have NUnit installed, navigate to the <a href="http://www.nunit.com/index.php?p=download">download section</a>, download and install the most current .msi file.</li>
</ul>
<h2>Creating a Test in Visual Studio</h2>
<p>This section has been described in detail in the previous article: <a href="http://relevantcodes.com/selenium-an-introduction/">Introduction to Selenium.</a> To create the test using the WebDriver API and NUnit attributes, following the steps below.</p>
<ol>
<li>Start Visual Studio</li>
<li>File > New Project > Class Library > Name: SeleniumNUnitTest</li>
<li>Add the following references
<ul>
<li>NUnit.Framework (NUnit.Framework.Dll)</li>
<li>WebDriver (WebDriver.Dll)</li>
<li>WebDriver.Support (WebDriver.Support.Dll)</li>
</ul>
</li>
<li>Finally, paste the below code to the newly created project</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #0600FF; font-weight: bold;">namespace</span> SeleniumNUnitTest
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="">NUnit.Framework</span><span style="color: #000000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="">OpenQA.Selenium</span><span style="color: #000000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="">OpenQA.Selenium.IE</span><span style="color: #000000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="">System</span><span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestFixture<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> GmailTests
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> GmailTests<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>SetUp<span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> LoadDriver<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> 
        <span style="color: #008000;">&#123;</span> 
            Console<span style="color: #000000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;SetUp&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span> 
            driver <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">new</span> <span style="color: #6666cc; font-weight: bold;">InternetExplorerDriver</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span> 
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Login<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Console<span style="color: #000000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Test&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
            driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Navigate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">GoToUrl</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://gmail.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
            driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">Id</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Email&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;test&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
            driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">Id</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Passwd&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;test&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
            driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">Id</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Passwd&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">Submit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
            Assert<span style="color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;">True</span><span style="color: #008000;">&#40;</span>driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Title</span><span style="color: #000000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Inbox&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>TearDown<span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> UnloadDriver<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> 
        <span style="color: #008000;">&#123;</span> 
            Console<span style="color: #000000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;TearDown&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span> 
            driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Quit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span> 
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestFixture, Description<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Tests Google Search with String data&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> GoogleTests
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> GoogleTests<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>SetUp<span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> LoadDriver<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> driver <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">new</span> <span style="color: #6666cc; font-weight: bold;">InternetExplorerDriver</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>TestCase<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Google&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>   <span style="color: #008000; font-style: italic;">// searchString = Google</span>
        <span style="color: #008000;">&#91;</span>TestCase<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Bing&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>     <span style="color: #008000; font-style: italic;">// searchString = Bing</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Search<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> searchString<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008000; font-style: italic;">// execute Search twice with testdata: Google, Bing</span>
&nbsp;
            driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Navigate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">GoToUrl</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://google.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
            driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;q&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span>searchString<span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
            driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;q&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">Submit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
            Assert<span style="color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;">True</span><span style="color: #008000;">&#40;</span>driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Title</span><span style="color: #000000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Google&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>TearDown<span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> UnloadDriver<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Quit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I have used the below attributes to create my test code.</p>
<ul>
<li>[TextFixture] &#8211; marks a class that contains tests and, optionally, setup or teardown methods</li>
<li>[SetUp] &#8211; used to provide a common set of functions that are performed just before each test method is called</li>
<li>[Text] &#8211; marks a method inside a TestFixture class as a test</li>
<li>[TextCase] &#8211; serves the dual purpose of marking a method with parameters as a test method and providing inline data to be used when invoking that method</li>
<li>[TearDown] &#8211; used to provide a common set of functions that are performed after each test method is run</li>
</ul>
<p class="info">In order to verify if the navigation is successful for both TextFixtures (test classes), an assertion <code>Assert.True</code> is used.</p>
<p>Save and compile (build) the solution. If you chose Class Library as the project type, a DLL would be created in the bin/Release or bin/Debug folder of the project. If the project type is other than a Class Library, an .EXE file would be created. Load either the DLL or the EXE into NUnit by selecting File > Open Project.</p>
<h2>Executing Tests from NUnit</h2>
<p>Once the project is been loaded to NUnit from File > Open Project, you will see 3 tests in the tests TreeView: Login, Search(&#8220;Google&#8221;) and Search(&#8220;Bing&#8221;).</p>
<p><img class="imgborder" alt="" src="/Articles/SeleniumNUnit/nunit_tests.gif" title="Tests List" class="alignnone" width="475" height="299" /></p>
<p class="info">You see 3 tests is because GmailTests.Login will run once whereas GoogleTests.Search will run twice with Google and Bing as the input parameters (notice the TestCase attribute). NUnit will execute methods corresponding to the number of <a href="http://nunit.com/index.php?p=testCase&#038;r=2.6">TestCase</a> attributes defined. Also see the <a href="http://nunit.com/index.php?p=values&#038;r=2.6">Values</a> and <a href="http://nunit.com/index.php?p=repeat&#038;r=2.6">Repeat</a> attributes.</p>
<p>All settings are complete. Click <u>R</u>un to execute all tests. Once the run session is complete, the following output log should be displayed.</p>
<p><img class="imgborder" alt="" src="/Articles/SeleniumNUnit/run_result.gif" title="Run Result" class="alignnone" width="720" height="292" /></p>
<p>In the example, the <code>GmailTests</code> class uses Console.WriteLine in LoadDriver, Login and UnloadDriver methods. This will be printed to the Text Output tab of NUnit as shown below:</p>
<p><img class="imgborder" alt="" src="/Articles/SeleniumNUnit/output_log.gif" title="Log" class="alignnone" width="720" height="292" /></p>
<p>By reading the output from the print log, it is also verified that NUnit will run the <code>SetUp</code> method first, followed by <code>Test</code> and <code>TearDown</code> methods.</p>
<img src="http://feeds.feedburner.com/~r/RelevantCodes/~4/q0jVRaNMcfA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/using-nunit-to-execute-selenium-webdriver-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://relevantcodes.com/using-nunit-to-execute-selenium-webdriver-tests/</feedburner:origLink></item>
		<item>
		<title>Selenium – An Introduction</title>
		<link>http://feedproxy.google.com/~r/RelevantCodes/~3/xL42iTyEVao/</link>
		<comments>http://relevantcodes.com/selenium-an-introduction/#comments</comments>
		<pubDate>Tue, 27 Mar 2012 16:47:08 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[C# Selenium]]></category>
		<category><![CDATA[Explicit Wait]]></category>
		<category><![CDATA[FindElement]]></category>
		<category><![CDATA[Implicit Wait]]></category>
		<category><![CDATA[Selenium 1.0]]></category>
		<category><![CDATA[Selenium 2.0]]></category>
		<category><![CDATA[Selenium Grid]]></category>
		<category><![CDATA[Selenium RC]]></category>
		<category><![CDATA[Selenium Remote Control]]></category>
		<category><![CDATA[WebDriver]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=5069</guid>
		<description><![CDATA[This article provides an introduction to Selenium and the Selenium Suite of tools: Selenium IDE, Selenium Grid, Selenium RC and Selenium WebDriver. It also answers some common questions revolving Selenium. I have created a simple example with the well-known Mercury Tours website to understand how to write tests using the WebDriver API.]]></description>
			<content:encoded><![CDATA[<p></p><p>I am writing this article to introduce Selenium => how to use it, its components and also show a simple test using WebDriver 2.0. So far, I&#8217;ve published articles on QTP, VBS, .Net etc. and wanted to expand the normal scope to different tools and always wanted to learn enough Selenium to write about it. I am not writing this article to compare the 2 tools; my aim is only to share knowledge of Selenium with our community.</p>
<h2>Introduction</h2>
<p>Selenium is a tool or a set of libraries that automate and test browsers. One big advantage of Selenium is the array of programming and scripting languages it supports, namely Java, CSharp, Python, Ruby, PHP and Perl. Also, tests written for one browser can be executed on all support browsers generally with a little bit of configuration. This article shows how to write a simple test on Firefox, but in the future, I will be writing articles to work with IE and Chrome as well.</p>
<p>My focus has been around WebDriver, but there are other tools as well: Selenium RC, Selenium IDE and Selenium Grid. Since each tool is different, the guys over at Selenium recommend having working knowledge of all tools. However, all my future articles will be on Selenium 2.0+.</p>
<h2>Selenium Suite</h2>
<p>This section will provide a brief overview of each of the tools that create the Selenium Suite. For more information, please read the <a href="http://seleniumhq.org/docs/index.html">documentation</a> and refer to the <a href="http://seleniumhq.org/download/">downloads</a> section for installation. Individual Drivers are available for download <a href="https://code.google.com/p/selenium/downloads/list?can=2&#038;q=&#038;colspec=Filename+Summary+Uploaded+ReleaseDate+Size+DownloadCount">here</a>.</p>
<h3>Selenium IDE</h3>
<p>Selenium IDE is a Firefox plug-in that allows users to record interactions with the UI, run them and also save the interactions as a Test Case. It is also used to export the recorded events to a language of your choice. Out of box, it does not support flow control and loops as its meant for rapid prototyping. To add iterations or conditionals, a <a href="http://seleniumhq.org/docs/02_selenium_ide.html#user-extensions">user extension</a> called <a href="http://wiki.openqa.org/download/attachments/379/goto_sel_ide.js">goto_sel_ide.js</a> must be installed. An obvious limitation of this tool is that it will only record tests against Firefox, but they can be <a href="http://seleniumhq.org/docs/05_selenium_rc.html#html-suite">run against other browsers</a>. More on Selenium IDE <a href="http://seleniumhq.org/docs/02_selenium_ide.html">here</a>.</p>
<h3>Selenium Grid</h3>
<p>Selenium Grid allows to run multiple tests in parallel on multiple machines at the same time. More on Selenium Grid <a href="http://selenium-grid.seleniumhq.org/index.html">here</a>.</p>
<h3>Selenium 1.0 (RC &#8211; Remote Control)</h3>
<p>At the time of this writing, Selenium RC is still supported in maintenance mode. It consists of Selenium Server and Client libraries (just like with WebDriver). The Server controls browsers and acts as an HTTP proxy. The Client libraries provide for an interface between the programming language of your choice and the Selenium RC Server. More on Selenium RC <a href="http://seleniumhq.org/docs/05_selenium_rc.html">here</a>.</p>
<h3>Selenium 2.0 (WebDriver)</h3>
<p>The team at Selenium describe WebDriver as &#8220;the future direction of the project and the newest addition to the Selenium toolkit&#8221;. WebDriver automates browsers by sending direct calls using each browser&#8217;s native support for automation. An important aspect of WebDriver is that, its not tied to a particular test framework. In other words, you can use it simply from the &#8220;main&#8221; method. To provide support for different browsers, Selenium providers &#8220;Drivers&#8221; that have their unique automation implementations. These drivers include FirefoxDriver, InternetExplorerDriver, ChromeDriver etc.</p>
<h2>WebDriver Installation</h2>
<p>To install WebDriver, download the relevant release of a language of your choice from <a href="http://seleniumhq.org/download/">this page</a> (look under &#8220;Selenium Client Drivers&#8221;). I am using C#, so I have downloaded <code>selenium-dotnet-2.20.0.zip</code>. Also, different drivers may have their own configuration. To learn more, visit the <a href="https://code.google.com/p/selenium/w/list">Selenium Wiki</a>. To start using WebDriver with C#, follow these steps:</p>
<ol>
<li>Unzip the relevant release files (selenium-dotnet-2.20.0.zip)</li>
<li>Select the folder with the target framework version (see below) (I am using .NET 3.5) <br /><img style="margin-top: 20px;margin-bottom:20px;" class="imgborder" alt="" src="/Articles/SeleniumIntroduction/dotnetframeworkversion.png" title=".NET Framework Version" class="aligncenter" width="604" height="113" /></li>
<li>Start Visual Studio and in a new project add reference to all the DLLs (Project -> Add Reference -> Browse -> /path_to_client_lib/) (see below) <br /><img style="margin-top: 20px;margin-bottom:20px;" class="imgborder" alt="" src="/Articles/SeleniumIntroduction/references.png" title="References" class="aligncenter" width="505" height="402" /></li>
<li>Finally, add the relevant references to your library 
<pre style="margin-top: 20px;">
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

// if testing Firefox
using OpenQA.Selenium.Firefox;
// if testing IE
using OpenQA.Selenium.IE;
// if testing Chrome
using OpenQA.Selenium.Chrome;
</pre>
</li>
</ol>
<p>That&#8217;s it. You&#8217;re ready to start using WebDriver! But, remember that, depending upon the Driver you are using you may have to perform additional configuration settings.</p>
<h2>A Simple UI Test</h2>
<p>The code below performs the following actions:</p>
<ol>
<li>Instantiate the FirefoxDriver</li>
<li>Execute the method LoginTest</li>
<li>Navigate to the target URL</li>
<li>Enter login details</li>
<li>Verify if the correct page appears</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="">System</span><span style="color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="">OpenQA.Selenium</span><span style="color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="">OpenQA.Selenium.Firefox</span><span style="color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="">OpenQA.Selenium.Support.UI</span><span style="color: #000000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> RelevantCodes<span style="color: #000000;">.</span><span style="color: #0000FF;">Selenium</span><span style="color: #000000;">.</span><span style="color: #0000FF;">Examples</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">class</span> Execution
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008000; font-style: italic;">// 1. Instantiate the FirefoxDriver object</span>
            <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">new</span> <span style="color: #6666cc; font-weight: bold;">FirefoxDriver</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
            <span style="color: #008000; font-style: italic;">// 2. Execute NewToursLogin.LoginTest</span>
            <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">new</span> NewToursLogin<span style="color: #008000;">&#40;</span>driver<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">LoginTest</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">class</span> NewToursLogin
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> _driver<span style="color: #000000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> NewToursLogin<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #000000;">.</span>_driver <span style="color: #000000;">=</span> driver<span style="color: #000000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> LoginTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #000000;">.</span>_driver<span style="color: #000000;">;</span>
&nbsp;
            <span style="color: #008000; font-style: italic;">// 3. Navigate to the test site</span>
            driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Navigate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">GoToUrl</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://newtours.demoaut.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
            <span style="color: #008000; font-style: italic;">// see SetImplicitTimeout</span>
            driver<span style="color: #000000;">.</span><span style="color: #0000FF;">SetImplicitTimeout</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">15</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">try</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008000; font-style: italic;">// 4. Enter values and click Login</span>
                driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;userName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;test&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
                driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;password&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">SendKeys</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;test&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
                driver<span style="color: #000000;">.</span><span style="color: #0000FF;">FindElement</span><span style="color: #008000;">&#40;</span>By<span style="color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;login&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">Click</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
                <span style="color: #008000; font-style: italic;">// 5. Wait until browser title equals &quot;Find a Flight: Mercury Tours:&quot;</span>
                <span style="color: #008000; font-style: italic;">// QTP users: consider the below statement as .WaitProperty(title, value, 10)</span>
                WebDriverWait wait <span style="color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;">new</span> WebDriverWait<span style="color: #008000;">&#40;</span>driver, TimeSpan<span style="color: #000000;">.</span><span style="color: #0000FF;">FromSeconds</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>wait<span style="color: #000000;">.</span><span style="color: #0000FF;">Until</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>app<span style="color: #008000;">&#41;</span> <span style="color: #000000;">=&gt;</span> app<span style="color: #000000;">.</span><span style="color: #0000FF;">Title</span><span style="color: #000000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Find a Flight: Mercury Tours:&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    Console<span style="color: #000000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Login complete!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span> Exception ex <span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008000; font-style: italic;">// If an exception related to WebDriver occurs..</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ex <span style="color: #008000;">is</span> NoSuchElementException <span style="color: #000000;">||</span> ex <span style="color: #008000;">is</span> WebDriverException<span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    Console<span style="color: #000000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WebDriver: &quot;</span> <span style="color: #000000;">+</span> ex<span style="color: #000000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
                <span style="color: #008000;">&#125;</span>
                <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">throw</span><span style="color: #000000;">;</span> <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> Extensions
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008000; font-style: italic;">// Sets implicit timeout for WebDriver</span>
        <span style="color: #008000; font-style: italic;">// Instructs the WebDriver to poll DOM until an element (or elements) is found</span>
        <span style="color: #008000; font-style: italic;">// QTP users: this is similar to the Global Timeout available in Settings</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetImplicitTimeout<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #6666cc; font-weight: bold;">IWebDriver</span> driver, <span style="color: #6666cc; font-weight: bold;">int</span> seconds<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            driver<span style="color: #000000;">.</span><span style="color: #0000FF;">Manage</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">Timeouts</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #0000FF;">ImplicitlyWait</span><span style="color: #008000;">&#40;</span>TimeSpan<span style="color: #000000;">.</span><span style="color: #0000FF;">FromSeconds</span><span style="color: #008000;">&#40;</span>seconds<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h2>Common Questions</h2>
<h3>I am a beginner. How can I learn Selenium?</h3>
<p>Start by learning one of the supported languages (Java, C#, Python, Perl, PHP, Ruby). WebDriver is the latest version and *may* be the focus moving forward &#8211; use and familiarize yourself with the API. If you&#8217;re tasked to work with Selenium IDE or Selenium RC, read the <a href="http://seleniumhq.org/docs/index.html">documentation</a> to clear any doubts. If you&#8217;re still confused, there are plenty of forums/user-groups available to answer most questions.</p>
<h3>How can I install Selenium? What is the installation process?</h3>
<p>It depends. What type of tool are you working with? The Selenium Suite has several tools/APIs under its belt and each one is different. Read the section Selenium Suite in this article for more information.</p>
<h3>Which language should I use with Selenium?</h3>
<p>Simple. Use any language that you&#8217;re most familiar with. Not everyone may agree with me, but I find it more important to work with a language that I am more comfortable with than learning something new just because it has more code examples and documentation available. If you are new to all support languages, I would recommend either Java, C# or Python. I use C#.</p>
<h3>My background is in QTP. Will I be able to leverage that with Selenium?</h3>
<p>Yes. My experience in QTP has helped me. A. LOT. Besides, even though you&#8217;re learning a different tool, the principals behind Test Automation remain the same. If you’re planning to use WebDriver, I do not think the learning curve will be very steep if you’re familiar with one of the supporting languages.</p>
<img src="http://feeds.feedburner.com/~r/RelevantCodes/~4/xL42iTyEVao" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/selenium-an-introduction/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		<feedburner:origLink>http://relevantcodes.com/selenium-an-introduction/</feedburner:origLink></item>
		<item>
		<title>Testing Web Services using QTP, VBScript</title>
		<link>http://feedproxy.google.com/~r/RelevantCodes/~3/SI-NSLZLAKc/</link>
		<comments>http://relevantcodes.com/working-with-webservices/#comments</comments>
		<pubDate>Fri, 16 Mar 2012 20:55:09 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[CreateXML]]></category>
		<category><![CDATA[CreateXMLFromFile]]></category>
		<category><![CDATA[Distance Matrix]]></category>
		<category><![CDATA[Get]]></category>
		<category><![CDATA[Google API]]></category>
		<category><![CDATA[LoadXML]]></category>
		<category><![CDATA[Post]]></category>
		<category><![CDATA[ResponseText]]></category>
		<category><![CDATA[WebService]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XMLDOM]]></category>
		<category><![CDATA[XMLHTTP]]></category>
		<category><![CDATA[XMLHTTPRequest]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=4994</guid>
		<description><![CDATA[This article was originally requested by one of our visitors - Dhup Chaya. It shows a way to work with Web Services (Google's Distance Matrix API) using XMLHTTPRequest (sending request &#038; receiving response) and parsing the response with both XMLUtil and XMLDOM.]]></description>
			<content:encoded><![CDATA[<p></p><p>W3C defines a Web service as &#8220;a standard means of interoperating between different software applications, running on a variety of platforms and/or frameworks&#8221;. In simple terms, a Web service can be used to integrate web applications. In this exchange of data, XML is used to tag data, SOAP is used to transfer data, WSDL is used to describe how data is exchanged and finally, UDDI lists the services available. For further reading, you can visit <a href="http://msdn.microsoft.com/en-us/library/ms996507.aspx">XML Web Services Basics</a>.</p>
<p>In this topic, we will be using 3 important objects and provide their overview before diving deep into this topic. They are:</p>
<ol>
<li>XMLUtil</li>
<li>XMLDOM</li>
<li>XMLHTTPRequest</li>
</ol>
<h3>XMLUtil</h3>
<p>XMLUtil object provided with QTP is used to access and return XML objects. In other words, XMLUtil can be used to access or expose the contents of an XML file. XMLUtil supports the following 2 methods to create and return an object of type XMLData:</p>
<ol>
<li>CreateXML Method</li>
<li>CreateXMLFromFile</li>
</ol>
<p>More information regarding XMLUtil can be located at QuickTest Professional Object Model Reference.</p>
<h3>XMLDOM</h3>
<p>Similar to XMLUtil object used in QTP, XMLDOM also provides methods and properties to access and return XML objects. I am more accustomed to using this object as opposed to XMLUtil, but they can be both used interchangeably. For further reading, you can visit <a href="http://msdn.microsoft.com/en-us/library/aa468547.aspx">A Beginner&#8217;s Guide to the XML DOM</a>. To create an instance of XMLDOM, CreateObject must be used.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #0600FF; font-weight: bold;">Set</span> xmlParser = <span style="color: #0600FF; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;Microsoft.XMLDOM&quot;</span>)</pre></div></div>

<h3>XMLHTTPRequest</h3>
<p>Unlike XMLUtil and XMLDOM, which are both XML parsers, the XMLHTTPRequest object is an API used to send HTTP or HTTPS requests directly to a Web Server and receiving the response using its Post, Send and Get methods. For further reading, you can visit <a href="http://msdn.microsoft.com/en-us/library/ie/ms535874%28v=vs.85%29.aspx">XMLHttpRequest object</a>. To create an instance of XMLHTTPRequest, CreateObject must be used.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #0600FF; font-weight: bold;">Set</span> XMLHTTPRequest = <span style="color: #0600FF; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;Microsoft.XMLHTTP&quot;</span>)</pre></div></div>

<h2>Google Maps &#8211; The Distance Matrix API</h2>
<p>The Google <a href="https://developers.google.com/maps/documentation/distancematrix/">Distance Matrix API</a> is a Web service used to provide travel distance and time for travel start (origins) and end points (destinations). The response from the API consists of rows containing data for each origin and destination pair.</p>
<p>To request information from the Distance Matrix API, the following URL with <code>output</code> type and input <code>parameters</code> must be used:</p>
<pre>http://maps.googleapis.com/maps/api/distancematrix/output?parameters</pre>
<h3>Output</h3>
<p>Output may be in either <code>JSON</code> or <code>XML</code>. In this topic, <code>XML</code> will be used.</p>
<h3>Parameters</h3>
<p>There is a total of 7 parameters that can be used to request information back from the API. 4 of these parameters are optional.</p>
<ol>
<li>Origins (required)</li>
<li>Destinations (required)</li>
<li>Mode (optional)</li>
<li>Language (optional)</li>
<li>Avoid (optional)</li>
<li>Units (optional)</li>
<li>Sensor (required)</li>
</ol>
<p>For more information, you can refer to the Distance Matrix API <a href="https://developers.google.com/maps/documentation/distancematrix/">documentation</a>.</p>
<p>I feel we&#8217;ve covered enough basics and its time to dig into exchanging data with the Distance Matrix API and then, parse it to get values we&#8217;re concerned with. Below are the values we will be using to request data:</p>
<ul>
<li>output = xml</li>
<li>origins = Atlanta, GA, USA</li>
<li>destinations = Dallas, TX, USA</li>
<li>units = imperial (in miles)</li>
<li>sensor = False</li>
</ul>
<p>The final URL string:</p>
<pre>http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Atlanta+GA+USA&#038;destinations=Dallas+TX+USA&#038;units=imperial&#038;sensor=false</pre>
<p>I also computed the distance using the Google Maps UI (maps.google.com) and got the following result. The result below should match the result obtained from XMLHTTPRequest.</p>
<p><img alt="" class="imgborder" src="/Articles/WorkingWithWebServices/by_car.jpg" title="Google Maps output" class="aligncenter" width="319" height="335" /></p>
<h2>Requesting Information from Distance Matrix API</h2>
<p>In the following code, XMLHTTPRequest object is used to send the string above and receive the corresponding response from the Distance Matrix API.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;">url = <span style="color: #800000;">&quot;http://maps.googleapis.com/maps/api/distancematrix/xml?&quot;</span> &amp; _
		<span style="color: #800000;">&quot;origins=Atlanta+GA+USA&amp;&quot;</span> &amp; _
		<span style="color: #800000;">&quot;destinations=Dallas+TX+USA&amp;&quot;</span> &amp; _
		<span style="color: #800000;">&quot;units=imperial&amp;&quot;</span> &amp; _
		<span style="color: #800000;">&quot;sensor=false&quot;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">Set</span> oReq = <span style="color: #0600FF; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;Microsoft.XMLHTTP&quot;</span>)
&nbsp;
oReq.<span style="color: #0600FF; font-weight: bold;">open</span> <span style="color: #800000;">&quot;POST&quot;</span>, url, <span style="color: #0600FF; font-weight: bold;">False</span>
oReq.send
&nbsp;
<span style="color: #008000;">'Response
</span>responseText = oReq.responseText
&nbsp;
<span style="color: #0600FF; font-weight: bold;">Print</span> responseText</pre></div></div>

<h5>Output</h5>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DistanceMatrixResponse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;status<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>OK<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/status<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;origin_address<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Atlanta, GA, USA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/origin_address<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;destination_address<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Dallas, TX, USA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/destination_address<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;row<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;status<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>OK<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/status<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;duration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>47410<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>13 hours 10 mins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/duration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;distance<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1254681<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>780 mi<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/distance<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/row<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DistanceMatrixResponse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Simple, isn&#8217;t it? Receiving the response is one thing, parsing it for data important to us is another. In the next part, parsing the above XML will be shown using both XMLUtil and XMLDOM objects.</p>
<h2>Parsing Information using XMLUtil, XMLDOM</h2>
<p>In the last section, we received some data from Google&#8217;s Distance Matrix API by using the <code>.responseText</code> method of XMLHTTPRequest. In this section, we will parse the ResponseText to retrieve the time taken and distance of travel. Below are the 2 nodes from the output above we&#8217;re concerned with:</p>
<pre>
DistanceMatrixResponse\row\element\duration\text
DistanceMatrixResponse\row\element\distance\text
</pre>
<p>I have used the method <code>GetValueByXPath</code> but you can also use <code>ChildElements</code> and <code>ChildElementsByPath</code> methods.</p>
<h5>XMLUtil</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;">sNodeDistance = <span style="color: #800000;">&quot;row/element/distance/text&quot;</span>
sNodeDuration = <span style="color: #800000;">&quot;row/element/duration/text&quot;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">Set</span> oParser = XMLUtil.CreateXML()
&nbsp;
<span style="color: #008000;">'Note: responseText comes from XMLHTTPRequest in the previous section
</span>oParser.Load responseText
&nbsp;
<span style="color: #008000;">'Using the XPath above to retrieve distance
</span>nodeDistance = oParser.GetRootElement.GetValueByXPath(sNodeDistance)
&nbsp;
<span style="color: #008000;">'Using the XPath above to retrieve duration
</span>nodeDuration = oParser.GetRootElement.GetValueByXPath(sNodeDuration)
&nbsp;
<span style="color: #0600FF; font-weight: bold;">Print</span> nodeDistance
<span style="color: #0600FF; font-weight: bold;">Print</span> nodeDuration</pre></div></div>

<h5>Output</h5>
<p><img alt="" class="imgborder" src="/Articles/WorkingWithWebServices/output.jpg" title="Output" class="aligncenter" width="312" height="153" /></p>
<p>With XMLDOM, I have used the method <code>selectSingleNode</code> but you could also use <code>selectNodes</code>, <code>getElementsByTagName</code> etc. to drill down to the target node.</p>
<h5>XMLDOM</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;">sNodeDistance = <span style="color: #800000;">&quot;row/element/distance/text&quot;</span>
sNodeDuration = <span style="color: #800000;">&quot;row/element/duration/text&quot;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">Set</span> oParser = <span style="color: #0600FF; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;Microsoft.XMLDOM&quot;</span>)
&nbsp;
<span style="color: #008000;">'Note: responseText comes from XMLHTTPRequest in the previous section
</span>oParser.loadXML(responseText)
&nbsp;
<span style="color: #008000;">'Distance node: comes from XPath above
</span><span style="color: #0600FF; font-weight: bold;">Set</span> nodeDistance = oParser.documentElement.selectSingleNode(sNodeDistance)
&nbsp;
<span style="color: #008000;">'Duration node: comes from XPath above
</span><span style="color: #0600FF; font-weight: bold;">Set</span> nodeDuration = oParser.documentElement.selectSingleNode(sNodeDuration)
&nbsp;
<span style="color: #0600FF; font-weight: bold;">Print</span> nodeDistance.text
<span style="color: #0600FF; font-weight: bold;">Print</span> nodeDuration.text</pre></div></div>

<h5>Output</h5>
<p><img alt="" class="imgborder" src="/Articles/WorkingWithWebServices/output.png" title="Output" class="aligncenter" width="312" height="153" /></p>
<h2>Summary</h2>
<p>In this topic, we saw how to send a request and receive response using XMLHTTPRequest. Later, to parse the response, we used XMLDOM and XMLUtil to retrieve the results.</p>
<img src="http://feeds.feedburner.com/~r/RelevantCodes/~4/SI-NSLZLAKc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/working-with-webservices/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		<feedburner:origLink>http://relevantcodes.com/working-with-webservices/</feedburner:origLink></item>
		<item>
		<title>Book Review and Giveaway – And I Thought I Knew QTP</title>
		<link>http://feedproxy.google.com/~r/RelevantCodes/~3/0B5PhcONgRs/</link>
		<comments>http://relevantcodes.com/book-review-and-giveaway-and-i-thought-i-knew-qtp/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 16:21:21 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Books]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=4637</guid>
		<description><![CDATA[Review of And I Thought I Knew QTP - a technical novel by Tarun Lalwani. Anshoo Arora (yours truly) had the opportunity to work on this project as the Technical Editor &#038; Reviewer. 3 free copies will be given away to the lucky winners signed by both Tarun and I.]]></description>
			<content:encoded><![CDATA[<p></p><p>
I thought I knew QTP is the only book available that is aimed to explain concepts and theories behind automation using QuickTest Professional. Its quite common to the QTP community to choose an approach provided an issue arises. However, when asked why we choose one particular approach over the other or why an approach works well in a given scenario and fails in another, there are no answers available or poorly drafted ones. This book uncovers the &#8216;why&#8217; by providing ample examples and scenarios.
</p>
<p><a href="http://knowledgeinbox.com/store/index.php?main_page=product_info&#038;cPath=1&#038;products_id=3"><img alt="" class="imgborder" src="/Articles/QTPConceptsUnplugged/front.jpg" title="And I Thought I Knew QTP - QTP Concepts Unplugged" class="aligncenter" width="323" height="323" /></a></p>
<p>
And I Thought I Knew QTP can be described as a journey of an individual who goes through a series of questions that make him wonder if his QTP skills are as good as he thought they were. The questions are the ones we face on an every day basis to questions we have always wanted to seek answers for. The information made available through this book is written by the world renowned QTP expert, <a href="http://www.linkedin.com/in/tarunlalwani">Tarun Lalwani</a>. This book is written with only one vision in mind &#8211; there should be a formal source that aims to get rid of all misinformation that we see on an every day basis when searching answers to questions involving QTP.
</p>
<p>
Because this book is written in the form of a novel with a simple storyline it is very easy to follow through. So, instead of being a completely technical read, this book can be said to be a &#8220;technical novel&#8221; &#8211; it includes technical concepts by not being boring :)
</p>
<h3>Preview</h3>
<p>
The preview of the book is now available <a href="http://knowledgeinbox.com/demos/AndIThoughtIKnewQTP_Preview.pdf">here</a>.
</p>
<h3>Giveaway (Winners now announced)</h3>
<p>
I will be giving out 1 copy of And I Thought I Knew QTP to one VERY lucky person! :)  All you have do to participate is post a comment in the comments section letting us know that you&#8217;re interested. That&#8217;s all! After a random vote, the winnerS will be declared.  <strong>Update:</strong> <strong>2</strong> more lucky winners (total of 3) will be announced, <a href="http://relevantcodes.com/book-review-and-giveaway-and-i-thought-i-knew-qtp/#comments<br />
">compliments of Tarun</a>.
</p>
<h3>Winners</h3>
<p>
Link to the recorded video: http://www.screenr.com/embed/cXls. Anish Pillai, Priyanka &#038; Suni: congratulations! :)
</p>
<h3>Links</h3>
<p>
Links that you will find helpful to order the book, find information and see the previews. I will be updating this section as more links become available.
</p>
<ul>
<li>US readers: Order your copy of And I Thought I Knew QTP From <a href="http://www.lulu.com/product/paperback/and-i-thought-i-knew-qtp---qtp-concepts-unplugged/17580430">Lulu</a></li>
<li><a href="http://knowledgeinbox.com/store/index.php?main_page=product_info&#038;cPath=1&#038;products_id=3">KnowledgeInbox page</a>
<li><a href="http://www.facebook.com/pages/And-I-thought-I-knew-QTP/165462290204637">Facebook page</a></li>
</ul>
<h2>Order your copy</h2>
<ul>
<li><a href="http://www.amazon.com/thought-knew-QTP-Concepts-Unplugged/dp/0983675902/ref=cm_rdp_product/186-9492351-9253163">Amazon</a></li>
<li><a href="http://knowledgeinbox.com/store/index.php?main_page=product_info&#038;cPath=1&#038;products_id=3">KnowledgeInbox Store</a></li>
<li><a href="http://www.lulu.com/shop/tarun-lalwani/and-i-thought-i-knew-qtp-qtp-concepts-unplugged/paperback/product-17580430.html;jsessionid=83E4F8E93C3EB4E2BB271C7A54F85A81">Lulu</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/RelevantCodes/~4/0B5PhcONgRs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/book-review-and-giveaway-and-i-thought-i-knew-qtp/feed/</wfw:commentRss>
		<slash:comments>108</slash:comments>
		<feedburner:origLink>http://relevantcodes.com/book-review-and-giveaway-and-i-thought-i-knew-qtp/</feedburner:origLink></item>
		<item>
		<title>IntelliSense for your Custom Class in 6 Steps</title>
		<link>http://feedproxy.google.com/~r/RelevantCodes/~3/5QHpAeM6j5U/</link>
		<comments>http://relevantcodes.com/intellisense-for-your-custom-class-in-6-steps/#comments</comments>
		<pubDate>Thu, 30 Jun 2011 14:48:51 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Class Intellisense]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Creating Intellisense in QTP]]></category>
		<category><![CDATA[Intellisense]]></category>
		<category><![CDATA[QTP COM Intellisense]]></category>
		<category><![CDATA[QTP Intellisense]]></category>
		<category><![CDATA[QTP Intellisense with .NET]]></category>
		<category><![CDATA[RegAsm]]></category>
		<category><![CDATA[VBC]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=4304</guid>
		<description><![CDATA[I have been researching this topic for a very long time and I finally have a workaround which I have tested for for the last 2 months with great success. The best part about this workaround is, I just need to create wrappers, without including any of the code I have in my QTP function libraries. If I add a new method to my class, all I do to update the Intellisense is add the method (without the QTP code) in my VB lib, create a DLL and Register the library for COM Interop.]]></description>
			<content:encoded><![CDATA[<p></p><p>
I have been using classes in my frameworks for a long time now, and my biggest gripe about QTP is its lack of Intellisense for custom classes. It also happens to be my biggest complaint. I understand this is not high priority for HP to include in its newer versions as there is a very small group of people who use OO techniques in test automation with QTP. There has been another workaround by Yaron, who used WSC to create intellisense.
</p>
<p>
I have been researching this topic and I finally have a workaround which I have tested for for the past few weeks with great success. The best part about this workaround is, I just need to create wrappers, without including any of the code I have in my QTP function libraries. If I add a new method to my class, all I do to update the Intellisense is add the method (without the QTP code) in my VB lib, create a DLL and Register the library for COM Interop.
</p>
<p>
Let&#8217;s get working then!
</p>
<h3>Step 1: Create your QTP Modular/Custom/Generic Class</h3>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #6666CC; font-weight: bold;">Class</span> LoginClass
&nbsp;
<span style="color: #008000;">'#region Private Variables
</span>    <span style="color: #0600FF; font-weight: bold;">Private</span> UserName    <span style="color: #008000;">'As String
</span>    <span style="color: #0600FF; font-weight: bold;">Private</span> UserRole    <span style="color: #008000;">'As String
</span>
<span style="color: #008000;">'#region Public Variables
</span>    <span style="color: #0600FF; font-weight: bold;">Public</span> PageTitle    <span style="color: #008000;">'As String
</span>    <span style="color: #0600FF; font-weight: bold;">Public</span> LinksCount   <span style="color: #008000;">'As Integer
</span>    
<span style="color: #008000;">'#region Public Methods
</span>    <span style="color: #0600FF; font-weight: bold;">Public</span> <span style="color: #0600FF; font-weight: bold;">Sub</span> CheckLinks()
        <span style="color: #0600FF; font-weight: bold;">Dim</span> arrLinks
&nbsp;
        arrLinks = Array(<span style="color: #800000;">&quot;Home&quot;</span>, <span style="color: #800000;">&quot;Register&quot;</span>, <span style="color: #800000;">&quot;Language&quot;</span>, <span style="color: #800000;">&quot;Sign-In&quot;</span>)
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">Call</span> FunctionToCheckLinks(arrLinks)
    <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Sub</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">Public</span> <span style="color: #0600FF; font-weight: bold;">Function</span> IsPageFound() <span style="color: #008000;">'As Boolean
</span>        <span style="color: #0600FF; font-weight: bold;">If</span> Browser(<span style="color: #800000;">&quot;title:=MyApp&quot;</span>).Exist(15) <span style="color: #0600FF; font-weight: bold;">Then</span> IsPageFound = <span style="color: #0600FF; font-weight: bold;">True</span>
    <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Function</span>
&nbsp;
<span style="color: #008000;">'#region Class Constructor &amp; Destructor
</span>    <span style="color: #0600FF; font-weight: bold;">Private</span> <span style="color: #0600FF; font-weight: bold;">Sub</span> <span style="color: #6666CC; font-weight: bold;">Class_Initialize</span>()
        UserName = <span style="color: #800000;">&quot;test&quot;</span>
        UserRole = Global.UserRole
    <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Sub</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">Private</span> <span style="color: #0600FF; font-weight: bold;">Sub</span> <span style="color: #6666CC; font-weight: bold;">Class_Terminate</span>
        <span style="color: #008000;">'code
</span>    <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Sub</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #6666CC; font-weight: bold;">Class</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">Public</span> LoginX: <span style="color: #0600FF; font-weight: bold;">Set</span> LoginX = <span style="color: #0600FF; font-weight: bold;">New</span> LoginClass</pre></div></div>

<h3>Step 2: Converting QTP Class into VB.NET code</h3>
<p>
The next step is converting the method and property names to VB.NET. If you have never used VB.NET, don&#8217;t be scared! The syntax is quite straight-forward. The only thing to note here is, you must return values for Functions and Property Get. Also, the syntax of the Property construct differs slightly in VB.NET. Still, all you are including here are the names. You do NOT have to add the code from your QTP methods.
</p>
<p class="info">Always remember to include the <strong>Microsoft.VisualBasic.ComClass()</strong> flag before the class, and also, remember to make the class <strong>Public</strong></p>
<p class="info">All that goes in the VB.NET code are the names of the properties and methods. Do not include any QTP code here!</p>
<p>
Below is a conversion of the QTP code above, to VB.NET. Notice that I have not included any of the code from my QTP class here:
</p>
<h5>Save the converted code in a .VB format file</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #6666CC; font-weight: bold;">Namespace</span> RelevantCodes
    &lt;Microsoft.VisualBasic.ComClass()&gt; <span style="color: #0600FF; font-weight: bold;">Public</span> <span style="color: #6666CC; font-weight: bold;">Class</span> LoginClass
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">Public</span> <span style="color: #0600FF; font-weight: bold;">Property</span> PageTitle <span style="color: #0600FF; font-weight: bold;">As</span> <span style="color: #6666CC; font-weight: bold;">String</span>
            <span style="color: #0600FF; font-weight: bold;">Get</span>
                return <span style="color: #800000;">&quot;title&quot;</span>
            <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Get</span>
            <span style="color: #0600FF; font-weight: bold;">Set</span>(<span style="color: #0600FF; font-weight: bold;">ByVal</span> value <span style="color: #0600FF; font-weight: bold;">As</span> <span style="color: #6666CC; font-weight: bold;">String</span>)
                <span style="color: #008000;">'code
</span>            <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Set</span>
        <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Property</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">Public</span> <span style="color: #0600FF; font-weight: bold;">Property</span> LinksCount <span style="color: #0600FF; font-weight: bold;">As</span> <span style="color: #6666CC; font-weight: bold;">Integer</span>
            <span style="color: #0600FF; font-weight: bold;">Get</span>
                return 10
            <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Get</span>
            <span style="color: #0600FF; font-weight: bold;">Set</span>(<span style="color: #0600FF; font-weight: bold;">ByVal</span> value <span style="color: #0600FF; font-weight: bold;">As</span> <span style="color: #6666CC; font-weight: bold;">Integer</span>)
            <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Set</span>
        <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Property</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">Public</span> <span style="color: #0600FF; font-weight: bold;">Sub</span> CheckLinks()
        <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Sub</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">Public</span> <span style="color: #0600FF; font-weight: bold;">Function</span> IsPageFound() <span style="color: #0600FF; font-weight: bold;">As</span> <span style="color: #6666CC; font-weight: bold;">Boolean</span>
        <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Function</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #6666CC; font-weight: bold;">Class</span>
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #6666CC; font-weight: bold;">Namespace</span></pre></div></div>

<p>
Also remember that, your <strong>Public variables</strong> become <strong>Public Properties</strong> in VB.NET.
</p>
<h3>Step 3: Creating DLL from VB.NET Class using VBC.exe</h3>
<p>
Once our class is ready and we have saved it in <code>.vb</code> format, let&#8217;s create the DLL using VB&#8217;s command line compiler <code>VBC</code. To understand more about VBC, you can use the "VBC.exe /help" in your command prompt to see a full list of commands available. Using the <code>/target:library</code> creates a .NET code library (DLL), which is what we&#8217;re looking for.
</p>
<pre>
C:\windows\Microsoft.NET\Framework\v2.0.50727\vbc.exe /target:library c:\RelevantCodes.vb
</pre>
<p>
Executing the above syntax in cmd.exe will create a DLL file: RelevantCodes.DLL.
</p>
<p class="alert">The path to your VBC.exe file may be different than the one I have used.</p>
<h3>Step 4: Registering Class using RegAsm.exe</h3>
<p>
Next, we will use the .NET assembly registration tool RegAsm.exe that reads the metadata within an assembly (which we created using VBC.exe) and adds the necessary values to your registry. RegAsm.exe syntax: <code>regasm assemblyFile [options]</code>.
</p>
<pre>
C:\windows\Microsoft.NET\Framework\v2.0.50727\regasm.exe c:\Relevantcodes.dll /codebase
</pre>
<p>
If QTP is already open, save all your tests and resources with libraries and re-open it. Launch it, and use CreateObject to create an instance of your class to see if its working. The syntax will be:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #0600FF; font-weight: bold;">Set</span> InstanceName = <span style="color: #0600FF; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;Namespace.Class&quot;</span>)
&nbsp;
<span style="color: #008000;">'for our class:
</span><span style="color: #0600FF; font-weight: bold;">Set</span> LoginX = <span style="color: #0600FF; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;RelevantCodes.LoginClass&quot;</span>)</pre></div></div>

<p><img class="imgborder" alt="" src="http://relevantcodes.com/Articles/ClassIntellisense/CreateObject.png" title="CreateObject" class="aligncenter" /></p>
<p>
You must see Intellisense for the instance to ensure everything has been a success until now. If this works, rest is just adding a few values to Registry and we&#8217;re done!
</p>
<h3>Step 5: Adding the Class Reference as a QTP Reserved Word</h3>
<p>
If the above works, <b>we&#8217;re almost done!</b> To get the intellisense for your class, we need to navigate to the registry key below and add a few values to the new key you create. Navigate to the following key in regedit.exe:
</p>
<pre>
HKEY_CURRENT_USER\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\
</pre>
<p>
If the above tree does not exist, try this:
</p>
<pre>
HKEY_LOCAL_MACHINE\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\
</pre>
<p>
Once you&#8217;re there, add a new key under reserved objects. You can give this key any name. What I generally do is, I give the same name as my QTP class. My key, then, becomes <strong>LoginClass</strong>. Once the key is created, I create the following entries in the key: ProgID (string), UIName (string) and VisibleMode (DWord).
</p>
<pre>
<b>New Key in \ReservedObjects:</b> Name of your QTP Class

<b>String:</b> ProgID,      <b>Value</b>: Namespace.ClassName
<b>String:</b> UIName,      <b>Value</b>: Name of your reference for your custom QTP class
<b>DWord:</b>  VisibleMode, <b>Value</b>: 2
</pre>
<p>
Therefore, in our case, considering the above, we will have the following values:
</p>
<pre>
<b>New Key in \ReservedObjects:</b> LoginClass

<b>String:</b> ProgID,      <b>Value</b>: RelevantCodes.LoginClass
<b>String:</b> UIName,      <b>Value</b>: LoginX
<b>DWord:</b>  VisibleMode, <b>Value</b>: 2
</pre>
<table style='margin-left:auto; margin-right:auto;'>
<tr>
<td><img alt="" src="http://relevantcodes.com/Articles/ClassIntellisense/ProgID.png" title="ProgID" class="alignleft" /><img alt="" src="http://relevantcodes.com/Articles/ClassIntellisense/UIName.png" title="UIName" class="alignleft" /></td>
</tr>
</table>
<p><img alt="" src="http://relevantcodes.com/Articles/ClassIntellisense/VisibleMode.png" title="VisibleMode" class="aligncenter" /></p>
<p>
The final output of adding the key and all entries to it must look like below:
</p>
<p><img alt="" class="imgborder" src="http://relevantcodes.com/Articles/ClassIntellisense/AllRegValues.png" title="All Registry Values" class="aligncenter" /></p>
<h3>Step 6: Reference the Class Instance with another Keyword</h3>
<p>
Lastly, all you need to do now is add a new variable and reference your Class Instance with it (as shown by <strong>Login</strong> below):
</p>
<pre>
Public LoginX: Set LoginX = New LoginClass
Public Login : Set Login = LoginX
</pre>
<p><img alt="" class="imgborder" src="http://relevantcodes.com/Articles/ClassIntellisense/QTLib.png" title="QTP Library" class="aligncenter" /></p>
<p>
To create your tests and to get Intellisense, remember to associate your library with the test and use the <strong>Login</strong> keyword to see the intellisense.
</p>
<p><img alt="" class="imgborder" src="http://relevantcodes.com/Articles/ClassIntellisense/QTTest.png" title="QTP Test with Intellisense" class="aligncenter" /></p>
<h3>I need to add a new method to my existing class. How do I do that!?!!</h3>
<p>
Well, once you converted the code for your original VB.NET library, DO NOT delete it. Once you add a new Public method to your QTP class, just add it to your VB.NET code, create (update) DLL using VBC.exe and re-register it using RegAsm. The new methods will now be available.
</p>
<h3>Summary</h3>
<p>
In summary, you have to follow the below 6 steps to create Intellisense for your custom QTP class:
</p>
<ol>
<li>Create your QTP class</li>
<li>Convert &#8220;Public&#8221; QTP methods to VB.NET (only the method names required!)</li>
<ul>
<li>Public Class</li>
<li>Microsoft.VisualBasic.ComClass() attribute</li>
</ul>
<li>Use VBC.exe to create .NET library</li>
<ul>
<li>Creates a DLL in the same location as the .VB file</li>
</ul>
<li>Use RegAsm.exe Assembly Registration tool to add necessary values to Registry</li>
<li>Add the Class Instance as a Reserved word in Registry</li>
<li>Reference the class with another Keyword</li>
</ol>
<p>
I hope you will find this helpful. As always, thanks for visiting Relevant Codes :)
</p>
<p class="info">
A lot of time and effort has been put into this article, to research and to test it. The information here is available absolutely free of cost. If you like this article, and if it works for you, please make a donation to a charity of your choice. There is no way we will be able to check if you did, but we know you will. There is a lot of on-going crisis today, and your donation will only help. Thank you.</p>
<img src="http://feeds.feedburner.com/~r/RelevantCodes/~4/5QHpAeM6j5U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/intellisense-for-your-custom-class-in-6-steps/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		<feedburner:origLink>http://relevantcodes.com/intellisense-for-your-custom-class-in-6-steps/</feedburner:origLink></item>
		<item>
		<title>Get Row/Column Of An Object in a WebTable</title>
		<link>http://feedproxy.google.com/~r/RelevantCodes/~3/hzpJDAXoDis/</link>
		<comments>http://relevantcodes.com/get-rowcolumn-of-an-object-in-a-webtable/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 05:03:28 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[QTP/Web]]></category>
		<category><![CDATA[cellIndex]]></category>
		<category><![CDATA[CellInfo]]></category>
		<category><![CDATA[Column]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Object inside Table]]></category>
		<category><![CDATA[QTP Object inside WebTable]]></category>
		<category><![CDATA[Row]]></category>
		<category><![CDATA[rowIndex]]></category>
		<category><![CDATA[td]]></category>
		<category><![CDATA[tr]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=4218</guid>
		<description><![CDATA[This article shows a simple way of finding out in which cell a QTP's Test Object resides.]]></description>
			<content:encoded><![CDATA[<p></p><p>
This article shows a simple way of finding out in which cell a QTP&#8217;s Test Object resides. I find the method shown in this article a more effective and faster way of finding the cell I&#8217;m looking for. I have found that this method performs better than traversing rows/cells of a WebTable using in-built WebTable methods such as ChildItem, ChildItemCount and GetCellData. Also, for test objects, it is also not possible to find an exact match with the methods provided for WebTable &#8211; it is only possible for strings.
</p>
<p>
A workaround to find objects inside WebTable using DOM table/tr/td is shown in this article. The code snippets below show how to retrieve the row and column of a given object from a WebTable.
</p>
<p>
Consider the element &#8220;Doe&#8221; in the table below. We can visually see that the link is in the 5th row (starting at the heading) and the 3rd column.
</p>
<table style='margin-left:auto; margin-right:auto;background: #fff;border-collapse: collapse;text-align: left;margin-bottom:27px;'>
<tr>
<th style='color: #039;padding: 10px 8px;border: 1px solid #ccc;'>ID</td>
<th style='color: #039;padding: 10px 8px;border: 1px solid #ccc;'>First Name</td>
<th style='color: #039;padding: 10px 8px;border: 1px solid #ccc;'>Last Name</td>
<th style='color: #039;padding: 10px 8px;border: 1px solid #ccc;'>Gender</td>
<th style='color: #039;padding: 10px 8px;border: 1px solid #ccc;'>Attending class?</td>
</tr>
<tr>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>12345</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>John</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>Smith</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>M</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>
<input type='checkbox' name='attending'></td>
</tr>
<tr>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>12346</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>Anne</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>Johnson</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>F</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>
<input type='checkbox' name='attending'></td>
</tr>
<tr>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>12347</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>Adam</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>Roberts</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>M</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>
<input type='checkbox' name='attending'></td>
</tr>
<tr>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>12348</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>Mary</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>Doe</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>F</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>
<input type='checkbox' name='attending'></td>
</tr>
<tr>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>12349</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>Tiffany</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>Smith</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>F</td>
<td style='border: 1px solid #ccc;color: #669;padding: 6px 8px;'>
<input type='checkbox' name='attending'></td>
</tr>
</table>
<p>
Executing the following code will provide us our expected output:
</p>
<pre>
Set element = Browser("creationtime:=0").WebElement("innertext:=Doe", "index:=0")

MsgBox "Row# " &#038; element.GetRow()
MsgBox "Col# " &#038; element.GetColumn()
</pre>
<table style='margin-left: auto; margin-right: auto;'>
<tr>
<td><img alt="" src="http://relevantcodes.com/Articles/FindTableCellFromObject/row.png" title="Row Output" class="alignnone" width="154" height="154" /></td>
<td><img alt="" src="http://relevantcodes.com/Articles/FindTableCellFromObject/column.png" title="Column Output" class="alignnone" width="154" height="154" /></td>
</tr>
</table>
<p>
The above example uses WebElement, but <strong>this approach works for all Web objects.</strong> Similarly, the code for finding the 2nd WebCheckBox for Anne Johnson will give us:  <code>Row# 3</code> and <code>Column#5</code> with the following code:
</p>
<pre>
MsgBox Browser("creationtime:=0").WebCheckBox("name:=attending", "index:=1").GetRow
MsgBox Browser("creationtime:=0").WebCheckBox("name:=attending", "index:=1").GetColumn
</pre>
<h2>The Code</h2>
<p>
The following two methods can be used directly for the object and will retrieve &#8216;either&#8217; the row or the column. The below method retrieves the Row for an object residing in a WebTable:
</p>
<h5>Find Row of the Object from WebTable</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #0600FF; font-weight: bold;">Function</span> GetRow(<span style="color: #0600FF; font-weight: bold;">ByVal</span> TObject)
    GetRow = -1
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">Set</span> TObject = TObject.<span style="color: #6666CC; font-weight: bold;">Object</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">Do</span>
        <span style="color: #0600FF; font-weight: bold;">If</span> TObject.nodeName = <span style="color: #800000;">&quot;TR&quot;</span> <span style="color: #0600FF; font-weight: bold;">Then</span>
            GetRow = TObject.rowIndex + 1
            <span style="color: #0600FF; font-weight: bold;">Exit</span> <span style="color: #0600FF; font-weight: bold;">Function</span>
        <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">If</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">Set</span> TObject = TObject.parentNode
    <span style="color: #0600FF; font-weight: bold;">Loop</span> <span style="color: #0600FF; font-weight: bold;">Until</span> TObject.nodeName = <span style="color: #800000;">&quot;HTML&quot;</span>
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Function</span>
&nbsp;
RegisterUserFunc <span style="color: #800000;">&quot;Image&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;Link&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebEdit&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebList&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebCheckBox&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebElement&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebRadioGroup&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span>, <span style="color: #800000;">&quot;GetRow&quot;</span></pre></div></div>

<h5>Example</h5>
<pre>
MsgBox Browser("title:=myBrowser").Link("innertext:=myLink", "index:=1").GetRow
</pre>
<p>
This method returns the column of the object inside a WebTable:
</p>
<h5>Find Column of the Object from WebTable</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #0600FF; font-weight: bold;">Function</span> GetColumn(<span style="color: #0600FF; font-weight: bold;">ByVal</span> TObject)
    GetColumn = -1
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">Set</span> TObject = TObject.<span style="color: #6666CC; font-weight: bold;">Object</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">Do</span>
        <span style="color: #0600FF; font-weight: bold;">If</span> TObject.nodeName = <span style="color: #800000;">&quot;TD&quot;</span> <span style="color: #0600FF; font-weight: bold;">Then</span>
            GetColumn = TObject.cellIndex + 1
            <span style="color: #0600FF; font-weight: bold;">Exit</span> <span style="color: #0600FF; font-weight: bold;">Function</span>
        <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">If</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">Set</span> TObject = TObject.parentNode
    <span style="color: #0600FF; font-weight: bold;">Loop</span> <span style="color: #0600FF; font-weight: bold;">Until</span> TObject.nodeName = <span style="color: #800000;">&quot;HTML&quot;</span>
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Function</span>
&nbsp;
RegisterUserFunc <span style="color: #800000;">&quot;Image&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;Link&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebEdit&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebList&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebCheckBox&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebElement&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebRadioGroup&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span>, <span style="color: #800000;">&quot;GetColumn&quot;</span></pre></div></div>

<h5>Example</h5>
<pre>
MsgBox Browser("title:=myBrowser").Link("innertext:=myLink", "index:=1").GetColumn
</pre>
<p>
The below implementation retrieves both row and column as a Scripting.Dictionary.
</p>
<h5>Find Row &#038; Column Info as Scripting.Dictionary</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #0600FF; font-weight: bold;">Function</span> CellInfo(<span style="color: #0600FF; font-weight: bold;">ByVal</span> TObject)
    <span style="color: #0600FF; font-weight: bold;">Dim</span> dic
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">Set</span> dic = <span style="color: #0600FF; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;Scripting.Dictionary&quot;</span>)
    dic.Add <span style="color: #800000;">&quot;row&quot;</span>, -1
    dic.Add <span style="color: #800000;">&quot;col&quot;</span>, -1
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">Set</span> TObject = TObject.<span style="color: #6666CC; font-weight: bold;">Object</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">Do</span>
        <span style="color: #0600FF; font-weight: bold;">If</span> TObject.nodeName = <span style="color: #800000;">&quot;TD&quot;</span> <span style="color: #0600FF; font-weight: bold;">Then</span> dic(<span style="color: #800000;">&quot;col&quot;</span>) = TObject.cellIndex + 1
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">If</span> TObject.nodeName = <span style="color: #800000;">&quot;TR&quot;</span> <span style="color: #0600FF; font-weight: bold;">Then</span>
            dic(<span style="color: #800000;">&quot;row&quot;</span>) = TObject.rowIndex + 1
            <span style="color: #0600FF; font-weight: bold;">Exit</span> <span style="color: #0600FF; font-weight: bold;">Do</span>
        <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">If</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">Set</span> TObject = TObject.parentNode
    <span style="color: #0600FF; font-weight: bold;">Loop</span> <span style="color: #0600FF; font-weight: bold;">Until</span> TObject.nodeName = <span style="color: #800000;">&quot;HTML&quot;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">Set</span> CellInfo = dic
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Function</span>
&nbsp;
RegisterUserFunc <span style="color: #800000;">&quot;Image&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;Link&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebEdit&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebList&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebRadioGroup&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebElement&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>
RegisterUserFunc <span style="color: #800000;">&quot;WebCheckBox&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span>, <span style="color: #800000;">&quot;CellInfo&quot;</span></pre></div></div>

<h5>Example</h5>
<pre>
Set cells = Browser("title:=myBrowser").Link("innertext:=myLink", "index:=1").CellInfo()

MsgBox cells("row")
MsgBox cells("col")

cells.RemoveAll
Set cells = Nothing
</pre>
<p>
I hope you find this helpful. As always, thanks for visiting Relevant Codes :)</p>
<img src="http://feeds.feedburner.com/~r/RelevantCodes/~4/hzpJDAXoDis" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/get-rowcolumn-of-an-object-in-a-webtable/feed/</wfw:commentRss>
		<slash:comments>68</slash:comments>
		<feedburner:origLink>http://relevantcodes.com/get-rowcolumn-of-an-object-in-a-webtable/</feedburner:origLink></item>
		<item>
		<title>ExcelUtil Class Library (MS Excel Utility Methods)</title>
		<link>http://feedproxy.google.com/~r/RelevantCodes/~3/CFpwQ9_wraU/</link>
		<comments>http://relevantcodes.com/excelutil-class-library-excel-utility-methods/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 20:34:45 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[Excel Utility]]></category>
		<category><![CDATA[ExcelUtil]]></category>
		<category><![CDATA[ExcelUtil Class Library]]></category>
		<category><![CDATA[RelevantCodes]]></category>
		<category><![CDATA[RelevantCodes.ExcelUtil]]></category>
		<category><![CDATA[SetFile]]></category>
		<category><![CDATA[VBScript Excel]]></category>
		<category><![CDATA[VBScript ExcelUtil]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=4100</guid>
		<description><![CDATA[Over the past few years, I have read numerous threads and posts talking about working with Excel using VBScript. Not only have there been numerous threads, but also frameworks that use Excel as the input and/or driver engine. I wanted to work on this mini-project for a long time, and I finally have Version 1.0 of ExcelUtil ready for download! It comes with .CHM documentation.]]></description>
			<content:encoded><![CDATA[<p></p><p>
Over the past few years, I have read numerous threads and posts talking about working with Excel using VBScript. Not only have there been numerous threads, but also frameworks that use Excel as the input and/or driver engine. I wanted to work on this mini-project for a long time, and I finally have Version 1.0 of ExcelUtil ready for download! It comes with <a href="http://relevantcodes.com/Articles/ExcelUtil/Documentation.chm">.CHM documentation</a>.
</p>
<p>
I&#8217;m quite sure I have missed some methods that the community uses, which I have not been able to include in the 1st version. I, however, am open to suggestions and would gladly include your ideas for Version 2.0.
</p>
<p class="centeralign th-box download"><span style='font-weight:bold;'><a class="downloadlink" href="http://relevantcodes.com/wp-content/plugins/download-monitor/download.php?id=11" title="Version1.0 downloaded 1312 times" >Download RelevantCodes.ExcelUtil Class Library (1312)</a></span></p>
<p>
<strong>IMPORTANT:</strong> To use any method of ExcelUtil, <strong>be sure to use <code>SetFile</code></strong>. This will set the Excel WorkBook and WorkSheet ready for use. To use a different WorkBook or WorkSheet, <code>SetFile</code> will have to be called again.
</p>
<h3>SetFile Property</h3>
<h5>Value Type</h5>
<p class="info">Read only.  A [RelevantCodes.ExcelUtil] object.</p>
<h5>Description</h5>
<p class="info">Sets the region instances for Excel WorkBook and WorkSheet. These instances for the Excel source are created only once and used by other methods. <strong>NOTE: For any method to execute, SetFile must be executed first to set the WorkBook and WorkSheet.</strong></p>
<h5>Syntax</h5>
<pre>
ExcelUtil.SetFile WorkBookPath, WorkSheet
</pre>
<h5>Examples</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #008000;">'Example 1
</span>ExcelUtil.SetFile <span style="color: #800000;">&quot;C:\Student.xls&quot;</span>, <span style="color: #800000;">&quot;Sheet1&quot;</span>
&nbsp;
<span style="color: #008000;">'Example 2:  Reading value directly from a file
</span>sCellValue = ExcelUtil.SetFile(<span style="color: #800000;">&quot;C:\Student.xls&quot;</span>, <span style="color: #800000;">&quot;Sheet1&quot;</span>).GetCellValue(1, 1)
&nbsp;
<span style="color: #008000;">'Example 3:  Reading values from 2 different files - Approach 1
</span>ExcelUtil.SetFile <span style="color: #800000;">&quot;C:\Student.xls&quot;</span>, <span style="color: #800000;">&quot;Sheet1&quot;</span>
var1 = ExcelUtil.GetCellValue(1, 1)
ExcelUtil.SetFile <span style="color: #800000;">&quot;C:\Teacher.xls&quot;</span>, <span style="color: #800000;">&quot;Sheet1&quot;</span>
var2 = ExcelUtil.GetCellValue(1, 1)
&nbsp;
<span style="color: #008000;">'Example 4:  Reading values from 2 different files - Approach 2
</span>var1 = ExcelUtil.SetFile(<span style="color: #800000;">&quot;C:\Student.xls&quot;</span>, <span style="color: #800000;">&quot;Sheet1&quot;</span>).GetCellValue(1, 1)
var2 = ExcelUtil.SetFile(<span style="color: #800000;">&quot;C:\Teacher.xls&quot;</span>, <span style="color: #800000;">&quot;Sheet1&quot;</span>).GetCellValue(1, 1)</pre></div></div>

<h3>I found a Bug. What should I do next?</h3>
<p>
Reporting the bug in the comments section will ensure the next version of this class library is more robust! It may not be possible to release the fixed code immediately, but notifying me and the readers of all potential bugs will be highly appreciated! Thank you.
</p>
<h3>I have something cool I would like you to add to ExcelUtil.</h3>
<p>
I would love to hear all ideas and suggestions to improve or enhance this library. Please use the comments section to share them with me and the community. Thank you.
</p>
<p class="centeralign th-box download"><span style='font-weight:bold;'><a class="downloadlink" href="http://relevantcodes.com/wp-content/plugins/download-monitor/download.php?id=11" title="Version1.0 downloaded 1312 times" >Download RelevantCodes.ExcelUtil Class Library (1312)</a></span></p>
<img src="http://feeds.feedburner.com/~r/RelevantCodes/~4/CFpwQ9_wraU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/excelutil-class-library-excel-utility-methods/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		<feedburner:origLink>http://relevantcodes.com/excelutil-class-library-excel-utility-methods/</feedburner:origLink></item>
		<item>
		<title>Executing QC TestSet via OTA API (QCRunTestSet)</title>
		<link>http://feedproxy.google.com/~r/RelevantCodes/~3/vdNAFHh3gK0/</link>
		<comments>http://relevantcodes.com/executing-qc-testset-via-ota-api-qcruntestset/#comments</comments>
		<pubDate>Mon, 10 Jan 2011 12:10:31 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[Quality Center]]></category>
		<category><![CDATA[Custom Run TestSet Object]]></category>
		<category><![CDATA[QC Scheduler]]></category>
		<category><![CDATA[QC Scheduling]]></category>
		<category><![CDATA[QCRunTestSet]]></category>
		<category><![CDATA[Quality Center Scheduling]]></category>
		<category><![CDATA[Remote QC Scheduling]]></category>
		<category><![CDATA[Run TestSet on LocalHost]]></category>
		<category><![CDATA[Run TestSet Remotely]]></category>
		<category><![CDATA[RunTestSet]]></category>
		<category><![CDATA[Schedule Test Set]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=4018</guid>
		<description><![CDATA[Over the past few weeks, I have been actively working on Quality Center (ALM) and its integration with QuickTest Professional. One of the mini-projects I've worked on is creating a custom object that executes a test set and sends an email to the supplied distribution list from ALM TestSet's Automation tab. VBScript code available for download. ]]></description>
			<content:encoded><![CDATA[<p></p><p>
For the past few weeks, I have been actively working on Quality Center (<strong>ALM</strong>) and its integration with QuickTest Professional. One of the mini-projects I&#8217;ve worked on is creating a custom object that executes a test set and sends an email to the supplied distribution list from ALM TestSet&#8217;s Automation tab (as shown below):
</p>
<p><img alt="" class="imgborder" src="http://relevantcodes.com/Articles/QCRunTestSet/EMailTo.PNG" title="EMailTo Field in QC TestSet&#039;s Automation tab" class="aligncenter" width="558" height="448" /></p>
<p>
Note: The above part (EMailTo) can be modified by either hard-coding it, or supplying an input from the Class Constructor.
</p>
<p>
I&#8217;ve extensively used the OTA API Reference to build this object and to understand how the API works. .NET developers having difficulty converting the VBScript code, please post in the comments section and I will mail you the class library (currently only C#).
</p>
<h2>Download (ALM Only)</h2>
<p>
QCRunTestSet is available for download, but comes as-is and currently executes TestSets on the localHost. However, I would be more than happy in helping you to modify this code to suit your needs.
</p>
<p class="centeralign th-box download"><span style='font-weight:bold;'><a class="downloadlink" href="http://relevantcodes.com/wp-content/plugins/download-monitor/download.php?id=10" title=" downloaded 1630 times" >QCRunTestSet.cls.vbs.zip [for ALM] (1630)</a></span></p>
<p>
If you&#8217;re having issues creating an instance of the <code>TSScheduler</code> object, please read <a href="http://www.sqaforums.com/showflat.php?Cat=&#038;Board=UBB20&#038;Number=652599">this thread</a> carefully. You may need to upgrade to a higher patch or perform a client registration on the execution machine.
</p>
<h2>Running QCRunTestSet from CommandLine</h2>
<p>
If you would like to pass parameters from the CommandLine instead of hard-coding them in the code, use WScript to pass arguments and WScript.Arguments to retrieve them in the class library. Example:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #008000;">'Passing arguments
</span>wscript C:\QCRunTestSet.cls.vbs <span style="color: #800000;">&quot;http//&lt;server&gt;:8080/qcbin&quot;</span> myUser myPassword myDomain _
                                                  myProject myTestSetPath myTestSetName</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #008000;">'Retrieving arguments
</span><span style="color: #0600FF; font-weight: bold;">Set</span> args = WScript.Arguments
&nbsp;
QCServer = args(0)
UserName =  args(1)
Password = args(2)
QCDomain = args(3)
QCProject = args(4)
QCTestSetPath = args(5)
QCTestSetName = args(6)</pre></div></div>

<p>
I hope you find this helpful. Thanks for visiting Relevant Codes! :)</p>
<img src="http://feeds.feedburner.com/~r/RelevantCodes/~4/vdNAFHh3gK0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/executing-qc-testset-via-ota-api-qcruntestset/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		<feedburner:origLink>http://relevantcodes.com/executing-qc-testset-via-ota-api-qcruntestset/</feedburner:origLink></item>
		<item>
		<title>IE Error: A script is causing IE to run slowly</title>
		<link>http://feedproxy.google.com/~r/RelevantCodes/~3/9QrvgHCN1GE/</link>
		<comments>http://relevantcodes.com/ie-error-a-script-is-causing-ie-to-run-slowly/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 03:55:25 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[QTP/Web]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[IE Error]]></category>
		<category><![CDATA[MaxScriptStatements]]></category>
		<category><![CDATA[QTP Web]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=3973</guid>
		<description><![CDATA[This tip shows how to disable the Error message: "A script on this page is causing Internet Explorer to run slowly" with using VBScript.]]></description>
			<content:encoded><![CDATA[<p></p><p>
While testing a script on IE8, me and my colleague kept getting the error &#8220;A script on this page is causing Internet Explorer to run slowly&#8221;.  Below is a snapshot of the error:</p>
<p><img alt="" class="imgborder" src="http://relevantcodes.com/Articles/IEScriptRunningSlow/ie_error.JPG" title="IE Error: A script is causing IE to run slowly" class="aligncenter" width="340" height="155" /></p>
<p>After some research, I came across an article on MSDN which already had the solution: <a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;175500">Microsoft KB # 175500</a>. The part we are concerned about is in the section &#8220;Let me fix it myself&#8221;:</p>
<p class="info">
1.Using a Registry Editor such as Regedt32.exe, open this key: <i>HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Styles</i><br />
Note: If the Styles key is not present, create a new key that is called Styles.<br />
2. Create a new DWORD value called &#8220;MaxScriptStatements&#8221; under this key and set the value to the desired number of script statements. If you are unsure of what value you need to set this to, you can set it to a DWORD value of 0xFFFFFFFF to completely avoid the dialog.
</p>
<p>
For the curious ones, the article states that, to encounter the error, the DWord value of <strong>0xFFFFFFFF</strong> must be added.  The value equates to -1, which is what has been used in the function below.
</p>
<h5>Code</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #0600FF; font-weight: bold;">Sub</span> IE_ChangeScriptTimeOut()
	<span style="color: #0600FF; font-weight: bold;">Dim</span> Registry, sKeyPath, sValueName, dwValue, sComputer
	CONST HKEY_CURRENT_USER = &amp;H80000001
&nbsp;
	sKeyPath = <span style="color: #800000;">&quot;Software\Microsoft\Internet Explorer\Styles&quot;</span>
	sValueName = <span style="color: #800000;">&quot;MaxScriptStatements&quot;</span>
	dwValue = -1
	sComputer = <span style="color: #800000;">&quot;.&quot;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">On</span> <span style="color: #0600FF; font-weight: bold;">Error</span> <span style="color: #0600FF; font-weight: bold;">Resume</span> <span style="color: #0600FF; font-weight: bold;">Next</span>
		<span style="color: #0600FF; font-weight: bold;">Set</span> Registry = <span style="color: #0600FF; font-weight: bold;">GetObject</span>(<span style="color: #800000;">&quot;winmgmts:{impersonationLevel=impersonate}!\\&quot;</span> &amp;_ 
				sComputer &amp; <span style="color: #800000;">&quot;\root\default:StdRegProv&quot;</span>)
&nbsp;
		Registry.CreateKey HKEY_CURRENT_USER, sKeyPath
		Registry.SetDWORDValue HKEY_CURRENT_USER, sKeyPath, sValueName, CLng(dwValue)
	<span style="color: #0600FF; font-weight: bold;">On</span> <span style="color: #0600FF; font-weight: bold;">Error</span> <span style="color: #0600FF; font-weight: bold;">Goto</span> 0
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">Set</span> Registry = <span style="color: #0600FF; font-weight: bold;">Nothing</span>
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Sub</span></pre></div></div>

<p>
Thanks for reading, and thanks for visiting Relevant Codes! :)</p>
<img src="http://feeds.feedburner.com/~r/RelevantCodes/~4/9QrvgHCN1GE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/ie-error-a-script-is-causing-ie-to-run-slowly/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://relevantcodes.com/ie-error-a-script-is-causing-ie-to-run-slowly/</feedburner:origLink></item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 16/58 queries in 0.296 seconds using disk: basic
Object Caching 763/915 objects using disk: basic
Content Delivery Network via N/A

Served from: relevantcodes.com @ 2012-05-26 03:51:11 -->

