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

<channel>
	<title>Relevant Codes (by Anshoo Arora) &#187; QTP/Windows</title>
	<atom:link href="http://relevantcodes.com/category/qtpwindows/feed/" rel="self" type="application/rss+xml" />
	<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>
		<item>
		<title>QTP: Working With Multiple Windows Applications</title>
		<link>http://relevantcodes.com/qtp-working-with-multiple-windows-applications/</link>
		<comments>http://relevantcodes.com/qtp-working-with-multiple-windows-applications/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 00:39:33 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[QTP/Windows]]></category>
		<category><![CDATA[AddCustom]]></category>
		<category><![CDATA[AddNew]]></category>
		<category><![CDATA[clsWindow]]></category>
		<category><![CDATA[LaunchAdd]]></category>
		<category><![CDATA[Multiple Windows Applications]]></category>
		<category><![CDATA[QTP Multiple Windows]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=1983</guid>
		<description><![CDATA[There are several instances where automation developers have to work with applications containing multiple windows, in a Standard Windows Environment. This technique uses a similar methodology as demonstrated in the article for Web-based apps, but the crux of this technique differs. It can be used by automation developers testing most types of windows applications.]]></description>
			<content:encoded><![CDATA[<p></p><p>
After writing the article <a href="http://relevantcodes.com/qtp-working-with-multiple-browser-applications-revised/">Working with Multiple Browser Applications</a> with QTP, I thought, there are several instances where automation developers have to work with applications containing multiple windows, in a Standard Windows Environment. This technique uses a similar methodology as demonstrated in the article for Web-based apps, but the crux of this technique differs. It can be used by automation developers testing most types of windows applications, and it contains concepts that are showcased in my generic automation framework RelevantCodes[1]One, which will be released in the coming few weeks.
</p>
<p>
With the help of this technique, regardless of how many windows applications are opened through QTP, we would never have to keep track of the Window&#8217;s title or any of its recognition properties (unless we want to). In other words, regardless of a window&#8217;s dynamic nature, this concept will enable you to give the window a name of your choice, and use that name to identify the window, instead of keeping track of changes in its properties.
</p>
<p>
What this concept contains:</p>
<ul>
<li>LaunchAdd: Launch a new window and retain its reference throughout the test cycle</li>
<li>AddNew: Automatically add a <i>new open window</i> to the collection without you having to specify its properties (see list of dependencies)</li>
<li>AddCustom: Adds a custom window by specifying its properties as an array</li>
<li>All identifiers stay intact until the test finishes.</li>
</ul>
<p class="centeralign">
<a href="http://relevantcodes.com/Articles/MultipleWindowApps/clsWindow.txt">View clsWindow.txt</a> | <a href="http://relevantcodes.com/Articles/MultipleWindowApps/clsWindow.zip">Download clsWindow.zip</a> | <a href="http://relevantcodes.com/Articles/MultipleWindowApps/Demo92.zip">Download Demo v9.2</a> | <a href="http://relevantcodes.com/Articles/MultipleWindowApps/Demo95.zip">Download Demo v9.5</a>
</p>
<p>
This concept contains 3 main methods, as described above. You can download the Class clsWindow for complete documentation, but for a quick overview, please continue reading.
</p>
<p>
First, and the one that may be used the most often is a method called AddCustom. It enables users to add a window of their choice to the collection, with the name they want to specify. From that point onwards, regardless of the number of changes that particular window goes through, it will not be required to update its properties. The name you give the window can be used throughout the test cycle. Below is the code snippet for AddCustom:
</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;">Public</span> <span style="color: #0600FF; font-weight: bold;">Sub</span> AddCustom(arrPropertyValue, sName)
    <span style="color: #0600FF; font-weight: bold;">Dim</span> lngHwnd
&nbsp;
    Me.sName = sName
    Me.arrPropertyValue = arrPropertyValue
&nbsp;
    <span style="color: #008000;">'Retrieve the handle of the specified window
</span>    lngHwnd = GetCustomWindowHwnd()
&nbsp;
    <span style="color: #008000;">'If the handle is valid, then add it to the global collection
</span>    <span style="color: #0600FF; font-weight: bold;">If</span> lngHwnd &lt;&gt; -1 <span style="color: #0600FF; font-weight: bold;">Then</span>
        colObject.Add sName, Window(<span style="color: #800000;">&quot;hwnd:=&quot;</span> &amp; lngHwnd)
        <span style="color: #0600FF; font-weight: bold;">If</span> <span style="color: #0600FF; font-weight: bold;">Not</span> oDict.Exists(lngHwnd) <span style="color: #0600FF; font-weight: bold;">Then</span>
            oDict.Add lngHwnd, lngHwnd
        <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">If</span>
    <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">If</span>
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Sub</span></pre></div></div>

<p>
Next is a method called LaunchAdd, which is dependent upon GetRecentlyOpenWindowHwnd. This will enable you to automatically add a newly launched window to the collection, with the name you specify:
</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;">Public</span> <span style="color: #0600FF; font-weight: bold;">Sub</span> LaunchAdd(sFilePath, sName)
    <span style="color: #0600FF; font-weight: bold;">Dim</span> lngHwnd
&nbsp;
    Me.sFilePath = sFilePath
    Me.sName = sName
&nbsp;
    <span style="color: #008000;">'Retrieve the window handle of the recently launched window
</span>    lngHwnd = GetRecentlyOpenWindowHwnd()
&nbsp;
    <span style="color: #008000;">'If the handle is valid, then add it to the global collection object
</span>    <span style="color: #0600FF; font-weight: bold;">If</span> lngHwnd &lt;&gt; -1 <span style="color: #0600FF; font-weight: bold;">Then</span>
        colObject.Add sName, Window(<span style="color: #800000;">&quot;hwnd:=&quot;</span> &amp; lngHwnd)
        <span style="color: #0600FF; font-weight: bold;">If</span> <span style="color: #0600FF; font-weight: bold;">Not</span> oDict.Exists(lngHwnd) <span style="color: #0600FF; font-weight: bold;">Then</span>
            oDict.Add lngHwnd, lngHwnd
        <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">If</span>
    <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">If</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;">Private</span> <span style="color: #0600FF; font-weight: bold;">Function</span> GetRecentlyOpenWindowHwnd() <span style="color: #008000;">'As Integer
</span>    <span style="color: #0600FF; font-weight: bold;">Dim</span> sFilePath, sName, oDesc, oParent, mHwndDict, x, iTimeElapsed
&nbsp;
    sFilePath = Me.sFilePath
    sName = Me.sName
&nbsp;
    GetRecentlyOpenWindowHwnd = -1
&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>
&nbsp;
        <span style="color: #008000;">'Create a description object
</span>        <span style="color: #0600FF; font-weight: bold;">Set</span> oDesc = Description.Create
&nbsp;
        <span style="color: #008000;">'oParent holds references to all the open windows
</span>        <span style="color: #0600FF; font-weight: bold;">Set</span> oParent = Desktop.ChildObjects(oDesc)
&nbsp;
        <span style="color: #008000;">'Scripting.Dictionary: holds all open windows' handles
</span>        <span style="color: #0600FF; font-weight: bold;">Set</span> mHwndDict = <span style="color: #0600FF; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;Scripting.Dictionary&quot;</span>)
&nbsp;
        <span style="color: #008000;">'Loop until all the handles are successfully added to the collection
</span>        <span style="color: #0600FF; font-weight: bold;">For</span> x = 0 <span style="color: #0600FF; font-weight: bold;">to</span> oParent.Count - 1
            mHwndDict.Add oParent(x).GetROProperty(<span style="color: #800000;">&quot;hwnd&quot;</span>), x
            <span style="color: #0600FF; font-weight: bold;">If</span> <span style="color: #0600FF; font-weight: bold;">Not</span> oDict.Exists(oParent(x).GetROProperty(<span style="color: #800000;">&quot;hwnd&quot;</span>)) <span style="color: #0600FF; font-weight: bold;">Then</span>
                oDict.Add oParent(x).GetROProperty(<span style="color: #800000;">&quot;hwnd&quot;</span>), oParent(x).GetROProperty(<span style="color: #800000;">&quot;hwnd&quot;</span>)
            <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">If</span>
        <span style="color: #0600FF; font-weight: bold;">Next</span>
&nbsp;
        <span style="color: #008000;">'Launch the target application
</span>        SystemUtil.Run sFilePath
&nbsp;
        <span style="color: #008000;">'Loop max 10 seconds for the window to open
</span>        <span style="color: #0600FF; font-weight: bold;">Do</span>
            <span style="color: #0600FF; font-weight: bold;">Set</span> oParent = Desktop.ChildObjects(oDesc)
            <span style="color: #0600FF; font-weight: bold;">For</span> x = 0 <span style="color: #0600FF; font-weight: bold;">to</span> oParent.Count - 1
                <span style="color: #0600FF; font-weight: bold;">Select</span> <span style="color: #0600FF; font-weight: bold;">Case</span> oParent(x).GetTOProperty(<span style="color: #800000;">&quot;micclass&quot;</span>)
	       <span style="color: #0600FF; font-weight: bold;">Case</span> <span style="color: #800000;">&quot;Window&quot;</span>, <span style="color: #800000;">&quot;Dialog&quot;</span>
                        <span style="color: #0600FF; font-weight: bold;">If</span> <span style="color: #0600FF; font-weight: bold;">Not</span> mHwndDict.Exists(oParent(x).GetROProperty(<span style="color: #800000;">&quot;hwnd&quot;</span>)) <span style="color: #0600FF; font-weight: bold;">Then</span>
                            GetRecentlyOpenWindowHwnd = oParent(x).GetROProperty(<span style="color: #800000;">&quot;hwnd&quot;</span>)
                            <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>
                <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Select</span>
            <span style="color: #0600FF; font-weight: bold;">Next</span>
            Wait(1)
            iTimeElapsed = iTimeElapsed + 1
        <span style="color: #0600FF; font-weight: bold;">Loop</span> <span style="color: #0600FF; font-weight: bold;">Until</span> iTimeElapsed = 10 
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">Set</span> oDesc = <span style="color: #0600FF; font-weight: bold;">Nothing</span>
        <span style="color: #0600FF; font-weight: bold;">Set</span> oParent = <span style="color: #0600FF; font-weight: bold;">Nothing</span>
        <span style="color: #0600FF; font-weight: bold;">Set</span> mHwndDict = <span style="color: #0600FF; font-weight: bold;">Nothing</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;">Goto</span> 0
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Function</span></pre></div></div>

<p>
Lastly, and a tricky one is AddNew. This will automatically add any new open window after LaunchAdd has been executed. Like the other 2 methods, this method will also enable preservation of a window&#8217;s property until the end of the test.
</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;">Public</span> <span style="color: #0600FF; font-weight: bold;">Sub</span> AddNew(sName, iTimeOutBeforeWindowOpens)
    <span style="color: #0600FF; font-weight: bold;">Dim</span> oDesc, oParent, x, iTimeElapsed, lngHwnd
&nbsp;
    <span style="color: #008000;">'Set default application timeout
</span>    <span style="color: #0600FF; font-weight: bold;">If</span> iTimeOutBeforeWindowOpens = <span style="color: #800000;">&quot;&quot;</span> <span style="color: #0600FF; font-weight: bold;">Then</span>
        iTimeOutBeforeWindowOpens = 1
    <span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">If</span>
&nbsp;
    <span style="color: #008000;">'Create a Description Object
</span>    <span style="color: #0600FF; font-weight: bold;">Set</span> oDesc = Description.Create
&nbsp;
    <span style="color: #008000;">'oParent holds all open windows
</span>    <span style="color: #0600FF; font-weight: bold;">Set</span> oParent = Desktop.ChildObjects(oDesc)
&nbsp;
    <span style="color: #008000;">'Wait iTimeOutBeforeWindowOpens number of seconds
</span>    <span style="color: #008000;">'Default iTimeOutBeforeWindow Opens = 1
</span>    Wait(iTimeOutBeforeWindowOpens)
&nbsp;
    <span style="color: #008000;">'Loop for 5 seconds
</span>    <span style="color: #008000;">'If within 5 seconds, a new unadded window is found, the window will
</span>    <span style="color: #008000;">' be added to the collection and this procedure will end.
</span>    <span style="color: #0600FF; font-weight: bold;">Do</span> 
        <span style="color: #008000;">'Create a new collection
</span>        <span style="color: #0600FF; font-weight: bold;">Set</span> oParent = Desktop.ChildObjects(oDesc)
&nbsp;
        <span style="color: #008000;">'Loop for all objects in the collection and find a one that does
</span>        <span style="color: #008000;">'not exist in the global dictionary: colWindows
</span>        <span style="color: #0600FF; font-weight: bold;">For</span> x = 0 <span style="color: #0600FF; font-weight: bold;">to</span> oParent.Count - 1
            <span style="color: #0600FF; font-weight: bold;">If</span> <span style="color: #0600FF; font-weight: bold;">Not</span> oDict.Exists(oParent(x).GetROProperty(<span style="color: #800000;">&quot;hwnd&quot;</span>)) <span style="color: #0600FF; font-weight: bold;">Then</span>
                lngHwnd = oParent(x).GetROProperty(<span style="color: #800000;">&quot;hwnd&quot;</span>)
                oDict.Add lngHwnd, lngHwnd
                colObject.Add sName, Window(<span style="color: #800000;">&quot;hwnd:=&quot;</span> &amp; lngHwnd)
                <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>
        <span style="color: #0600FF; font-weight: bold;">Next</span>
        Wait(1)
        iTimeElapsed = iTimeElapsed + 1
    <span style="color: #0600FF; font-weight: bold;">Loop</span> <span style="color: #0600FF; font-weight: bold;">Until</span> iTimeElapsed = 5 <span style="color: #008000;">'This timeout can be minized to 1, if desired
</span><span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Sub</span></pre></div></div>

<p class="centeralign">
<a href="http://relevantcodes.com/Articles/MultipleWindowApps/clsWindow.txt">View clsWindow.txt</a> | <a href="http://relevantcodes.com/Articles/MultipleWindowApps/clsWindow.zip">Download clsWindow.zip</a> | <a href="http://relevantcodes.com/Articles/MultipleWindowApps/Demo92.zip">Download Demo v9.2</a> | <a href="http://relevantcodes.com/Articles/MultipleWindowApps/Demo95.zip">Download Demo v9.5</a>
</p>
<p>
Technically, the concepts in this article can be easily transferred to any technology, just like the concepts were transferred from Web to Windows, which is quite a huge transition. Once this concept is implemented properly, it should help eliminate the need to constantly identify the windows we need to work with.
</p>
<p>
I hope you find this helpful :)</p>
]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/qtp-working-with-multiple-windows-applications/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Automating Windows Calculator Part 1</title>
		<link>http://relevantcodes.com/automating-windows-calculator-part-1/</link>
		<comments>http://relevantcodes.com/automating-windows-calculator-part-1/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 23:14:12 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[QTP/Windows]]></category>
		<category><![CDATA[Automating Application]]></category>
		<category><![CDATA[Calculator]]></category>
		<category><![CDATA[Functions]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=1748</guid>
		<description><![CDATA[In this article, we will see a few interesting ways to automate the Windows Calculator. The concepts in this article should  serve as an excellent way of understanding the automation process with QTP, how functions can help increase code reuse, and why at times its extremely important to validate user-input. A must read!]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://relevantcodes.com/Articles/AutomatingCalculator/Calculator.PNG"><img alt="" src="http://relevantcodes.com/Articles/AutomatingCalculator/Calculator.PNG" title="Windows Calculator" class="alignright" width="260" height="260" /></a></p>
<p>
In this article, we will see a few interesting ways to automate the Windows Calculator. To ensure the concepts in this article are not too overwhelming, I have divided it into 2 parts. The first one discusses a few basics and creates a routine that can be used to easily set values to, and retrieve from it. The concepts in this article should also serve as an excellent way of understanding the automation process with QTP, how functions can help increase code reuse, and why at times its extremely important to validate user-input.
</p>
<p>[Off-topic] <i> If you find this article helpful, and would like to see other types of applications automated as well, please post in the comments section and let me know what other applications would be excellent for conceptual purposes and I will try my best to include them in the future articles. </i> [/Off-topic]</p>
<p>
Let&#8217;s launch the calculator through code:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;">SystemUtil.Run <span style="color: #800000;">&quot;calc.exe&quot;</span></pre></div></div>

<p>
Now, we have the calculator object launched. Let&#8217;s see if we can create a few statements in QTP that act as users to perform some calculations. For this example, let&#8217;s calculate 10*20:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;">Window(<span style="color: #800000;">&quot;nativeclass:=SciCalc&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>).WinButton(<span style="color: #800000;">&quot;text:=1&quot;</span>).Click	<span style="color: #008000;">'1 -&gt;   1
</span>Window(<span style="color: #800000;">&quot;nativeclass:=SciCalc&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>).WinButton(<span style="color: #800000;">&quot;text:=0&quot;</span>).Click	<span style="color: #008000;">'0 -&gt;	10
</span>Window(<span style="color: #800000;">&quot;nativeclass:=SciCalc&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>).WinButton(<span style="color: #800000;">&quot;text:=\*&quot;</span>).Click	<span style="color: #008000;">'* -&gt;	10*
</span>Window(<span style="color: #800000;">&quot;nativeclass:=SciCalc&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>).WinButton(<span style="color: #800000;">&quot;text:=2&quot;</span>).Click	<span style="color: #008000;">'2 -&gt;	10*2
</span>Window(<span style="color: #800000;">&quot;nativeclass:=SciCalc&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>).WinButton(<span style="color: #800000;">&quot;text:=0&quot;</span>).Click	<span style="color: #008000;">'0 -&gt;	10*20
</span>Window(<span style="color: #800000;">&quot;nativeclass:=SciCalc&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>).WinButton(<span style="color: #800000;">&quot;text:==&quot;</span>).Click	<span style="color: #008000;">'= -&gt;	10*20=
</span>
MsgBox Window(<span style="color: #800000;">&quot;nativeclass:=SciCalc&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>)_
        .WinEdit(<span style="color: #800000;">&quot;nativeclass:=Edit&quot;</span>).GetROProperty(<span style="color: #800000;">&quot;text&quot;</span>)</pre></div></div>

<p class="alert">Notice we have substitued a &#8220;\&#8221; before *. This is because * is a special Regex character and all special regex characters must have &#8220;\&#8221; before them to identify them correctly. You can also execute the same code without the &#8220;\&#8221; and test the result.</p>
<p>
The above code can be simplified using a With..End With statement:
</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;">With</span> Window(<span style="color: #800000;">&quot;nativeclass:=SciCalc&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>)
	.WinButton(<span style="color: #800000;">&quot;text:=1&quot;</span>).Click    <span style="color: #008000;">'1 -&gt;    1
</span>	.WinButton(<span style="color: #800000;">&quot;text:=0&quot;</span>).Click    <span style="color: #008000;">'0 -&gt;	10
</span>	.WinButton(<span style="color: #800000;">&quot;text:=\*&quot;</span>).Click   <span style="color: #008000;">'* -&gt;	10*
</span>	.WinButton(<span style="color: #800000;">&quot;text:=2&quot;</span>).Click    <span style="color: #008000;">'2 -&gt;	10*2
</span>	.WinButton(<span style="color: #800000;">&quot;text:=0&quot;</span>).Click    <span style="color: #008000;">'0 -&gt;	10*20
</span>	.WinButton(<span style="color: #800000;">&quot;text:==&quot;</span>).Click    <span style="color: #008000;">'= -&gt;	10*20=
</span>
	MsgBox .WinEdit(<span style="color: #800000;">&quot;nativeclass:=Edit&quot;</span>).GetROProperty(<span style="color: #800000;">&quot;text&quot;</span>)
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">With</span></pre></div></div>

<p>
I covered the above With..End With block because at times when properties change, you only have to update the property of the Window object once, and not 8 times as you would in the previous code snippet.
</p>
<p>
We can still simplify this process further, by creating a function. With the help of the function, we can avoid writing long and the same lines of code each time we need to perform the <i>same operation</i>. This saves time, and when you need to make changes, it simplifies maintenance and increases code reuse. Let&#8217;s create two functions, then. One will type the key, and the other will retrieve the output from the calculator:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #008000;">'vKey: Key you want to press
</span><span style="color: #0600FF; font-weight: bold;">Function</span> TypeKey(vKey)
	<span style="color: #008000;">'* is a special regex character
</span>	<span style="color: #008000;">'We will put a &quot;\&quot; before it to let QTP know that we are using a literal, not a regex
</span>	<span style="color: #0600FF; font-weight: bold;">Select</span> <span style="color: #0600FF; font-weight: bold;">Case</span> vKey
		<span style="color: #0600FF; font-weight: bold;">Case</span> <span style="color: #800000;">&quot;*&quot;</span>
			vKey = <span style="color: #800000;">&quot;\&quot;</span> &amp; vKey
	<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Select</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">With</span> Window(<span style="color: #800000;">&quot;nativeclass:=SciCalc&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>)
		.WinButton(<span style="color: #800000;">&quot;text:=&quot;</span> &amp; vKey).Click
	<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">With</span>
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Function</span></pre></div></div>


<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> CalculatedValue
	CalculatedValue = Window(<span style="color: #800000;">&quot;nativeclass:=SciCalc&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>)_
            .WinEdit(<span style="color: #800000;">&quot;nativeclass:=Edit&quot;</span>).GetROProperty(<span style="color: #800000;">&quot;text&quot;</span>)
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Function</span></pre></div></div>

<p>
Create a function library and copy both functions in it. Then, go to &#8220;File&#8221; menu and click &#8220;Associate Library [LibraryName] with &#8216;Test&#8217;&#8221;. Right now, your library should look something like this:
</p>
<div class="wp-caption aligncenter" style="width: 569px">
	<a href="http://relevantcodes.com/Articles/AutomatingCalculator/FunctionLibrary.PNG"><img alt="Function Library with TypeKey and CalculatedValue" src="http://relevantcodes.com/Articles/AutomatingCalculator/FunctionLibrary.PNG" title="Function Library with TypeKey and CalculatedValue" width="569" height="365" /></a>
	<p class="wp-caption-text">Function Library with TypeKey and CalculatedValue</p>
</div>
<p>
Let&#8217;s now use the function that we have created above to perform the same calculation. In your test, you will have:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;">TypeKey <span style="color: #800000;">&quot;1&quot;</span>
TypeKey <span style="color: #800000;">&quot;0&quot;</span>
TypeKey <span style="color: #800000;">&quot;*&quot;</span>
TypeKey <span style="color: #800000;">&quot;2&quot;</span>
TypeKey <span style="color: #800000;">&quot;0&quot;</span>
TypeKey <span style="color: #800000;">&quot;=&quot;</span>
&nbsp;
MsgBox CalculatedValue</pre></div></div>

<p>
Notice how we have compacted our code from the very first code snippet to the one above. </p>
<p>
We have already talked about substituting a &#8220;\&#8221; before a special regex character. The calculator has 5 keys that include such characters. They are:
</p>
<pre>
*
+
+/-
.
M+
</pre>
<p>
We need to incorporate all special regex characters above in our Select-Case block. Thus, we&#8217;re now ready to manipulate the TypeKey function above, and create one that will input all sorts of keys from the users, make sure the special characters are followed by a &#8220;\&#8221; sign, and retrieve the output from the calculator.
</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;">Function</span> TypeKey(vKey)
	<span style="color: #008000;">'Use a Select block for special regex characters
</span>	<span style="color: #0600FF; font-weight: bold;">Select</span> <span style="color: #0600FF; font-weight: bold;">Case</span> vKey
		<span style="color: #0600FF; font-weight: bold;">Case</span> <span style="color: #800000;">&quot;*&quot;</span>, <span style="color: #800000;">&quot;+&quot;</span>, <span style="color: #800000;">&quot;+/-&quot;</span>, <span style="color: #800000;">&quot;.&quot;</span>
			vKey = <span style="color: #800000;">&quot;\&quot;</span> &amp; vKey
		<span style="color: #0600FF; font-weight: bold;">Case</span> <span style="color: #800000;">&quot;M+&quot;</span>
			vKey = <span style="color: #800000;">&quot;M\+&quot;</span>
	<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Select</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">With</span> Window(<span style="color: #800000;">&quot;nativeclass:=SciCalc&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>)
		.WinButton(<span style="color: #800000;">&quot;text:=&quot;</span> &amp; vKey).Click
	<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">With</span>
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Function</span></pre></div></div>

<p>
Our function is ready! It is now capable of taking any sort of input from the user and retrieve the output. Let&#8217;s put the above function in use:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;">TypeKey <span style="color: #800000;">&quot;1&quot;</span>
TypeKey <span style="color: #800000;">&quot;*&quot;</span>
TypeKey <span style="color: #800000;">&quot;2&quot;</span>
TypeKey <span style="color: #800000;">&quot;+&quot;</span>
TypeKey <span style="color: #800000;">&quot;3&quot;</span>
TypeKey <span style="color: #800000;">&quot;=&quot;</span>
&nbsp;
MsgBox CalculatedValue</pre></div></div>

<p>
Now, we will begin with the tricky part. Well, not really. This is still the first topic, so we&#8217;ll keep it quite easy. We still haven&#8217;t created any sort of validation, yet. We are going to manipulate this code a few times and compact it further. We will do this to ensure that our script does not stop because of a user error. For example, if you type &#8220;10&#8243; instead of &#8220;1&#8243;, then &#8220;0&#8243;, QTP will error out. That is because the calculator does not have any key greater than 9. In other words:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;"><span style="color: #008000;">'This is Correct
</span>TypeKey <span style="color: #800000;">&quot;1&quot;</span>    <span style="color: #008000;">'1 exists in the calculator
</span>TypeKey <span style="color: #800000;">&quot;0&quot;</span>    <span style="color: #008000;">'0 exists in the calculator
</span>
<span style="color: #008000;">'This is incorrect
</span>TypeKey <span style="color: #800000;">&quot;10&quot;</span>   '10 does <span style="color: #0600FF; font-weight: bold;">not</span> exist <span style="color: #0600FF; font-weight: bold;">in</span> the Calculator</pre></div></div>

<p>
To avoid occurrence of such user errors, we can begin by creating an array of all keys available to the calculator:
</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;">Dim</span> arrCalcKeys
&nbsp;
arrCalcKeys = Array(<span style="color: #800000;">&quot;Backspace&quot;</span>, <span style="color: #800000;">&quot;CE&quot;</span>, <span style="color: #800000;">&quot;C&quot;</span>, <span style="color: #800000;">&quot;MC&quot;</span>, <span style="color: #800000;">&quot;MR&quot;</span>, <span style="color: #800000;">&quot;MS&quot;</span>, <span style="color: #800000;">&quot;M+&quot;</span>, <span style="color: #800000;">&quot;sqt&quot;</span>, <span style="color: #800000;">&quot;%&quot;</span>, <span style="color: #800000;">&quot;1/x&quot;</span>, <span style="color: #800000;">&quot;=&quot;</span>, <span style="color: #800000;">&quot;/&quot;</span>,_
                  <span style="color: #800000;">&quot;*&quot;</span>, <span style="color: #800000;">&quot;-&quot;</span>, <span style="color: #800000;">&quot;+&quot;</span>, <span style="color: #800000;">&quot;.&quot;</span>, <span style="color: #800000;">&quot;+/-&quot;</span>, <span style="color: #800000;">&quot;1&quot;</span>, <span style="color: #800000;">&quot;2&quot;</span>, <span style="color: #800000;">&quot;3&quot;</span>, <span style="color: #800000;">&quot;4&quot;</span>, <span style="color: #800000;">&quot;5&quot;</span>, <span style="color: #800000;">&quot;6&quot;</span>, <span style="color: #800000;">&quot;7&quot;</span>, <span style="color: #800000;">&quot;8&quot;</span>, <span style="color: #800000;">&quot;9&quot;</span>, <span style="color: #800000;">&quot;0&quot;</span>)</pre></div></div>

<p>
Now, we can run all our validations against this array. If a user enters any key that is outside this array, that means, there is an error which must be handled. This can help us create a function that will be used solely for purposes of validation. Let&#8217;s call it, IsKeyLoaded.
</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;">Function</span> IsKeyLoaded(vKey)
	<span style="color: #0600FF; font-weight: bold;">Dim</span> arrCalcKeys, iCalcKey
&nbsp;
	IsKeyLoaded = <span style="color: #0600FF; font-weight: bold;">False</span>
&nbsp;
	<span style="color: #008000;">'Array with all Calculator Keys
</span>	arrCalcKeys = Array(<span style="color: #800000;">&quot;Backspace&quot;</span>, <span style="color: #800000;">&quot;CE&quot;</span>, <span style="color: #800000;">&quot;C&quot;</span>, <span style="color: #800000;">&quot;MC&quot;</span>, <span style="color: #800000;">&quot;MR&quot;</span>, <span style="color: #800000;">&quot;MS&quot;</span>, <span style="color: #800000;">&quot;M+&quot;</span>, <span style="color: #800000;">&quot;sqt&quot;</span>, _
			<span style="color: #800000;">&quot;%&quot;</span>, <span style="color: #800000;">&quot;1/x&quot;</span>, <span style="color: #800000;">&quot;=&quot;</span>, <span style="color: #800000;">&quot;/&quot;</span>, <span style="color: #800000;">&quot;*&quot;</span>, <span style="color: #800000;">&quot;-&quot;</span>, <span style="color: #800000;">&quot;+&quot;</span>, <span style="color: #800000;">&quot;.&quot;</span>, <span style="color: #800000;">&quot;+/-&quot;</span>, <span style="color: #800000;">&quot;1&quot;</span>, <span style="color: #800000;">&quot;2&quot;</span>, _
			<span style="color: #800000;">&quot;3&quot;</span>, <span style="color: #800000;">&quot;4&quot;</span>, <span style="color: #800000;">&quot;5&quot;</span>, <span style="color: #800000;">&quot;6&quot;</span>, <span style="color: #800000;">&quot;7&quot;</span>, <span style="color: #800000;">&quot;8&quot;</span>, <span style="color: #800000;">&quot;9&quot;</span>, <span style="color: #800000;">&quot;0&quot;</span>)
&nbsp;
	<span style="color: #008000;">'Loop through all Array items and attempt to find vKey
</span>	<span style="color: #0600FF; font-weight: bold;">For</span> <span style="color: #0600FF; font-weight: bold;">Each</span> iCalcKey <span style="color: #0600FF; font-weight: bold;">in</span> arrCalcKeys
		<span style="color: #008000;">'If the Array Item matches with &quot;vKey&quot;, Exit Loop. Return True.
</span>		<span style="color: #0600FF; font-weight: bold;">If</span> CStr(iCalcKey) = CStr(vKey) <span style="color: #0600FF; font-weight: bold;">Then</span>
			IsKeyLoaded = <span style="color: #0600FF; font-weight: bold;">True</span>
			<span style="color: #0600FF; font-weight: bold;">Exit</span> <span style="color: #0600FF; font-weight: bold;">For</span>
		<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">If</span>
	<span style="color: #0600FF; font-weight: bold;">Next</span>
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Function</span></pre></div></div>

<p>
IsKeyLoaded contains the array we created above, and a For-Loop that will check the Input Key against the Keys in the Array. If the 2 keys match, we&#8217;re good. If the 2 keys don&#8217;t match, that means, the user entered an invalid value. But, we&#8217;re in the clear because we now have the error handling for that! This also means that because of an invalid input, our script won&#8217;t stop.
</p>
<p>
You won&#8217;t believe we have come this far, so quickly. Remember the &#8220;TypeKey&#8221; function we created earlier? We will incorporate &#8220;IsKeyLoaded&#8221; in &#8220;TypeKey&#8221; and create our validation component.
</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;">Function</span> TypeKey(vKey)
	<span style="color: #008000;">'If Key is not found then, Exit Function
</span>	<span style="color: #0600FF; font-weight: bold;">If</span> <span style="color: #0600FF; font-weight: bold;">Not</span> IsKeyLoaded(vKey) <span style="color: #0600FF; font-weight: bold;">Then</span>
		<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: #008000;">'Rest of the function remains the same:
</span>	<span style="color: #0600FF; font-weight: bold;">Select</span> <span style="color: #0600FF; font-weight: bold;">Case</span> vKey
		<span style="color: #0600FF; font-weight: bold;">Case</span> <span style="color: #800000;">&quot;*&quot;</span>, <span style="color: #800000;">&quot;+&quot;</span>, <span style="color: #800000;">&quot;+/-&quot;</span>, <span style="color: #800000;">&quot;.&quot;</span>
			vKey = <span style="color: #800000;">&quot;\&quot;</span> &amp; vKey
		<span style="color: #0600FF; font-weight: bold;">Case</span> <span style="color: #800000;">&quot;M+&quot;</span>
			vKey = <span style="color: #800000;">&quot;M\+&quot;</span>
	<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Select</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">With</span> Window(<span style="color: #800000;">&quot;nativeclass:=SciCalc&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>)
		.WinButton(<span style="color: #800000;">&quot;text:=&quot;</span> &amp; vKey).Click
	<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">With</span>
<span style="color: #0600FF; font-weight: bold;">End</span> <span style="color: #0600FF; font-weight: bold;">Function</span></pre></div></div>

<p>
Let&#8217;s put the code we have created now into practice. We will also use the &#8220;CalculatedValue&#8221; function we created earlier. For the sake of experiment, let&#8217;s provide our function with 2 invalid keys: A and B. Notice how these keys will not be entered to the Calculator, instead, the function will exit if it finds anything that is outside the scope of our array: arrCalcKeys.
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:Consolas,Monaco,Courier,Verdana;">TypeKey <span style="color: #800000;">&quot;A&quot;</span>	<span style="color: #008000;">'Ignore
</span>TypeKey <span style="color: #800000;">&quot;1&quot;</span>	<span style="color: #008000;">'Valid
</span>TypeKey <span style="color: #800000;">&quot;B&quot;</span>	<span style="color: #008000;">'Ignore
</span>TypeKey <span style="color: #800000;">&quot;2&quot;</span>	<span style="color: #008000;">'Valid
</span>TypeKey <span style="color: #800000;">&quot;3&quot;</span>	<span style="color: #008000;">'Valid
</span>TypeKey <span style="color: #800000;">&quot;+&quot;</span>	<span style="color: #008000;">'Valid
</span>TypeKey <span style="color: #800000;">&quot;1&quot;</span>	<span style="color: #008000;">'Valid
</span>TypeKey <span style="color: #800000;">&quot;=&quot;</span>	<span style="color: #008000;">'Valid
</span>
MsgBox CalculatedValue '124</pre></div></div>

<div class="wp-caption aligncenter" style="width: 441px">
	<a href="http://relevantcodes.com/Articles/AutomatingCalculator/ExecResult1.PNG"><img alt="Execution Result" src="http://relevantcodes.com/Articles/AutomatingCalculator/ExecResult1.PNG" title="Execution Result" width="441" height="397" /></a>
	<p class="wp-caption-text">Execution Result</p>
</div>
<p>
Our script didn&#8217;t stop regardless of the user error, and was able to retrieve the output nonetheless. However, <strong>there is a BIG short-coming in our code</strong>. If the user meant to type &#8220;0&#8243; and they typed &#8220;A&#8221;, &#8220;B&#8221; or any other key, that means, even though our script didn&#8217;t stop, it calculated an incorrect value. A small error at times can go unnoticed, and it can become a cause of concern for the automation developer as well as the user. Thus, we must incorporate additional logic that will let us know if an invalid key was calculated; and the output should be discarded. We will see that, and much more in the coming article!
</p>
<p>
In this article we learned how we can begin from scratch and create a highly usable code block. In the next article, we will cover advanced techniques, and you will see how code can be compacted further, and how code reuse can be enhanced. Above all, we will learn why we must be sharp in reporting errors that a user might miss. Sometimes, our extra attention to detail can save an entire team a lot of time. Plus, your role automatically becomes more important to the success of the team.</p>
]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/automating-windows-calculator-part-1/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</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 (User agent is rejected)
Database Caching 9/25 queries in 0.057 seconds using disk: basic
Object Caching 359/388 objects using disk: basic
Content Delivery Network via N/A

Served from: relevantcodes.com @ 2012-05-19 10:51:31 -->
