<?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; VBScript</title>
	<atom:link href="http://relevantcodes.com/category/vbscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://relevantcodes.com</link>
	<description>A Test Development Resource for HP QuickTest Professional.</description>
	<lastBuildDate>Fri, 30 Dec 2011 07:07:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>ExcelUtil Class Library (MS Excel Utility Methods)</title>
		<link>http://relevantcodes.com/excelutil-class-library-excel-utility-methods/</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 alert"><span style='font-weight:bold; font-family:Calibri; font-size:1.2em;'><a class="downloadlink" href="http://relevantcodes.com/wp-content/plugins/download-monitor/download.php?id=11" title="Version1.0 downloaded 1066 times" >Download RelevantCodes.ExcelUtil Class Library (1066)</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, monospace;"><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 alert"><span style='font-weight:bold; font-family:Calibri; font-size:1.2em;'><a class="downloadlink" href="http://relevantcodes.com/wp-content/plugins/download-monitor/download.php?id=11" title="Version1.0 downloaded 1066 times" >Download RelevantCodes.ExcelUtil Class Library (1066)</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/excelutil-class-library-excel-utility-methods/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Executing QC TestSet via OTA API (QCRunTestSet)</title>
		<link>http://relevantcodes.com/executing-qc-testset-via-ota-api-qcruntestset/</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[VBScript]]></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="" 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 alert"><span style='font-weight:bold; font-family:Calibri; font-size:1.2em;'><a class="downloadlink" href="http://relevantcodes.com/wp-content/plugins/download-monitor/download.php?id=10" title=" downloaded 1244 times" >QCRunTestSet.cls.vbs.zip [for ALM] (1244)</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, monospace;"><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, monospace;"><span style="color: #008000;">'Retrieving arguments
</span><span style="color: #0000A6;">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>
]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/executing-qc-testset-via-ota-api-qcruntestset/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>IE Error: A script is causing IE to run slowly</title>
		<link>http://relevantcodes.com/ie-error-a-script-is-causing-ie-to-run-slowly/</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="" 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>
<h2>The VBScript:</h2>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Sub</span> IE_ChangeScriptTimeOut()
	<span style="color: #0000A6;">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: #0000A6;">On</span> <span style="color: #0000A6;">Error</span> <span style="color: #0000A6;">Resume</span> <span style="color: #0000A6;">Next</span>
		<span style="color: #0000A6;">Set</span> Registry = <span style="color: #0000A6;">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: #0000A6;">On</span> <span style="color: #0000A6;">Error</span> <span style="color: #0000A6;">Goto</span> 0
&nbsp;
	<span style="color: #0000A6;">Set</span> Registry = <span style="color: #0000A6;">Nothing</span>
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span></pre></div></div>

<p>
Thanks for reading, and thanks for visiting Relevant Codes! :)</p>
]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/ie-error-a-script-is-causing-ie-to-run-slowly/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>GUI Objects VBScript &amp; Try Catch Finally</title>
		<link>http://relevantcodes.com/gui-objects-vbscript-try-catch-finally/</link>
		<comments>http://relevantcodes.com/gui-objects-vbscript-try-catch-finally/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 06:31:17 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[Catch]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Classes]]></category>
		<category><![CDATA[Error Handling]]></category>
		<category><![CDATA[Finally]]></category>
		<category><![CDATA[Module]]></category>
		<category><![CDATA[QTP Module]]></category>
		<category><![CDATA[Test Module]]></category>
		<category><![CDATA[Try]]></category>
		<category><![CDATA[Try..Catch..Finally]]></category>
		<category><![CDATA[Wrapper]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=3802</guid>
		<description><![CDATA[VBScript's error handling techniques have been quite limited to <code>On Error</code> statements, and even though they satisfy our needs to some extent, the lack of incorporating dynamic behavior with them has been a much bigger issue. In this article, I will implement a Try-Catch-Finally statement with QTP's GUI objects along with an On Error statement. This will enable dynamic behavior and enable us to easily divide flow depending on different error conditions.]]></description>
			<content:encoded><![CDATA[<p></p><p>
VBScript&#8217;s error handling techniques have been quite limited to <code>On Error</code> statements, and even though they satisfy our needs to some extent, the lack of incorporating dynamic behavior with them has been a much bigger issue. Lee Harvey <a href="http://my.opera.com/Lee_Harvey/blog/2007/04/21/try-catch-finally-in-vbscript-sure">here</a> shows how <code>Classes</code> can be extended to create simple Try-Catch-Finally statements with just a bit more work. I like this technique, and have been using it for the past few days with success. We know that this is not the best solution that is possible with a scripting language (JS does a lot better!), but its a start and a terrific workaround (thanks Lee!).
</p>
<h2>Creating Try-Catch-Finally Statements in VBScript</h2>
<p>
If you have already read Lee&#8217;s article, you can skip this part. Otherwise, a simple implementation is shown below:
</p>
<ol>
<li>As soon as the class executes, it enters Class_Initialize &#8211; Try statement</li>
<li>It encounters an error &#8211; Err.Raise 1002</li>
<li>As soon as it encounters an error, the Try statement (Class_Initialize) immediately ends execution</li>
<li>Because we&#8217;re releasing the Class reference using <code>Set NewTryCatchFinally = Nothing</code> right after initializing it, the Class enters Class_Terminate</li>
<li>Class_Terminate calls the Catch subroutine</li>
<li>When Catch runs in its entirety, Class_Terminate executes</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">On</span> <span style="color: #0000A6;">Error</span> <span style="color: #0000A6;">Resume</span> <span style="color: #0000A6;">Next</span>
&nbsp;
<span style="color: #0000A6;">Class</span> TryCatchFinally
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Sub</span> Class_Initialize <span style="color: #008000;">'Try
</span>        <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Entering Try..&quot;</span> &amp; vbNewLine
&nbsp;
        Err.Raise 1002 <span style="color: #008000;">'Raise a Syntax Error
</span>        MsgBox <span style="color: #800000;">&quot;This line will not execute&quot;</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Sub</span> Catch <span style="color: #008000;">'Catch
</span>        <span style="color: #0000A6;">If</span> Err.Number = 0 <span style="color: #0000A6;">Then</span> <span style="color: #0000A6;">Exit</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
        <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Entering Catch..&quot;</span>
        <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Error caught: &quot;</span> &amp; Err.Description  &amp; vbNewLine
&nbsp;
        Err.Clear
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Sub</span> Class_Terminate : Catch <span style="color: #008000;">'Finally
</span>        <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Exiting..&quot;</span> 
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span>
&nbsp;
<span style="color: #0000A6;">Set</span> NewTryCatchFinally = <span style="color: #0000A6;">New</span> TryCatchFinally : <span style="color: #0000A6;">Set</span> NewTryCatchFinally = <span style="color: #0000A6;">Nothing</span></pre></div></div>

<p>
The output of executing the above code should be the following:
</p>
<div class="wp-caption aligncenter" style="width: 299px">
	<img alt="" src="http://relevantcodes.com/Articles/VBScriptTryCatchFinallyIntro/TryCatchPrintLog.png" title="Try Catch Finally Implementation in VBScript" width="299" height="221" />
	<p class="wp-caption-text">Try Catch Finally Implementation in VBScript</p>
</div>
<p>
We can see from the result that as soon as the Class executes, it enters <code>Class_Initialize</code>. When it encounters an error, it enters into <code>Class_Terminate</code> which contains (and executes) the <code>Catch</code> method. A simple yet such an elegant solution!
</p>
<h2>Utility Class &#038; Error Handler</h2>
<p>
In this article, I will try to implement the technique with QTP&#8217;s GUI objects. To begin, let&#8217;s create a simple Login class with the following methods:
</p>
<ol>
<li><code>CheckPage</code></li>
<li><code>CheckImage</code></li>
<li><code>Login</code></li>
</ol>
<p>
The Login Class outputs the result through the <code>Result</code> property:
</p>
<h5>A simple Login Class</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">On</span> <span style="color: #0000A6;">Error</span> <span style="color: #0000A6;">Resume</span> <span style="color: #0000A6;">Next</span>
&nbsp;
<span style="color: #0000A6;">Class</span> Login
&nbsp;
    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Property</span> <span style="color: #0000A6;">Get</span> Result
        Result = CheckPage <span style="color: #0000A6;">And</span> CheckImage <span style="color: #0000A6;">And</span> Login
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Property</span>
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Function</span> CheckPage
        MsgBox <span style="color: #800000;">&quot;CheckPage&quot;</span> : CheckPage = <span style="color: #0000A6;">True</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Function</span> CheckImage
        CheckImage = <span style="color: #0000A6;">False</span>
&nbsp;
        Browser(<span style="color: #800000;">&quot;title:=Welcome.*&quot;</span>).WebElement(<span style="color: #800000;">&quot;text:=Relevant Codes&quot;</span>).Click <span style="color: #008000;">'Error here!
</span>
        MsgBox <span style="color: #800000;">&quot;Error!&quot;</span> <span style="color: #008000;">'This will not execute
</span>    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
    <span style="color: #008000;">'This will not execute
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Function</span> Login
        MsgBox <span style="color: #800000;">&quot;Login&quot;</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span>
&nbsp;
<span style="color: #0000A6;">Dim</span> NewLogin : <span style="color: #0000A6;">Set</span> NewLogin = <span style="color: #0000A6;">New</span> Login</pre></div></div>

<p class="info">
Above, the Result property of the Login Class is called, which first executes CheckPage, then CheckImage and finally Login. Note that Login will only execute if CheckImage runs without any errors.
</p>
<p>
Let&#8217;s create another class that calls the methods of the original Login class, but with the error handling capabilities of a Try-Catch-Finally statement. This is also the Class that will always execute the original Login class because that&#8217;s where the actual error handling using Try-Catch-Finally exists.
</p>
<h5>Try..Catch..Finally with Class_Initialize &#038; Class_Terminate</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Class</span> LoginHandler <span style="color: #008000;">'LoginHandler start
</span>
    <span style="color: #0000A6;">Public</span> bResult
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Sub</span> Class_Initialize
         <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Entering Try..&quot;</span> &amp; vbNewLine
         bResult = NewLogin.Result
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Sub</span> CatchErr
        <span style="color: #0000A6;">If</span> Err.Number = 0 <span style="color: #0000A6;">Then</span> <span style="color: #0000A6;">Exit</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
        <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Entering Catch..&quot;</span>
        <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Error: &quot;</span> &amp; Err.Description &amp; vbNewLine
&nbsp;
        Err.Clear
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Sub</span> Class_Terminate : CatchErr
        <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Exiting..&quot;</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span> <span style="color: #008000;">'LoginHandler end
</span>
<span style="color: #0000A6;">Set</span> NewLoginHandler = <span style="color: #0000A6;">New</span> LoginHandler
<span style="color: #0000A6;">Set</span> NewLoginHandler = <span style="color: #0000A6;">Nothing</span>
<span style="color: #0000A6;">Set</span> NewLogin = <span style="color: #0000A6;">Nothing</span></pre></div></div>

<p class="info">
The <code>Class_Initialize</code> method of the class calls the Result property of the Login class. Once Result is called, it executes all the methods called by the Result property. If any error occurs, CatchErr is immediately called by <code>Class_Terminate</code> and later, all code inside the Class_Terminate method is executed.
</p>
<p>
Output of executing Login Class &#038; LoginHandler:
</p>
<div class="wp-caption aligncenter" style="width: 500px">
	<img alt="" src="http://relevantcodes.com/Articles/VBScriptTryCatchFinallyIntro/PrintLog1.png" title="Print Log" width="500" height="199" />
	<p class="wp-caption-text">QTP Print Log</p>
</div>
<h2>Method Containing the Error?</h2>
<p>
By including the name of each method in the very first line, we can also find out where exactly the error occurred. This, however, I agree is quite time consuming and repetitive work. The only alternative to this has been shown by PowerDebug Beta. However, if you would like to extend the above approach in VBScript, this *may be* the only possible approach. I&#8217;ve been wrong before, and would love to see a technique which simplifies the method naming approach used below.
</p>
<h5>Login Class with Method Names</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">On</span> <span style="color: #0000A6;">Error</span> <span style="color: #0000A6;">Resume</span> <span style="color: #0000A6;">Next</span>
&nbsp;
<span style="color: #0000A6;">Class</span> Login <span style="color: #008000;">'Login start
</span>
    <span style="color: #0000A6;">Public</span> sMethod
&nbsp;
    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Property</span> <span style="color: #0000A6;">Get</span> Result
        Result = CheckPage <span style="color: #0000A6;">And</span> CheckImage <span style="color: #0000A6;">And</span> Login
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Property</span>
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Function</span> CheckPage
        Me.sMethod = <span style="color: #800000;">&quot;CheckPage&quot;</span>
&nbsp;
        MsgBox <span style="color: #800000;">&quot;CheckPage&quot;</span> : CheckPage = <span style="color: #0000A6;">True</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Function</span> CheckImage
        Me.sMethod = <span style="color: #800000;">&quot;CheckImage&quot;</span>
&nbsp;
        CheckImage = <span style="color: #0000A6;">False</span>
        Browser(<span style="color: #800000;">&quot;title:=Welcome.*&quot;</span>).WebElement(<span style="color: #800000;">&quot;text:=Relevant Codes&quot;</span>).Click <span style="color: #008000;">'Error here!
</span>        
        MsgBox <span style="color: #800000;">&quot;Error!&quot;</span> <span style="color: #008000;">'This will not execute
</span>    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Function</span> Login <span style="color: #008000;">'This will not execute
</span>        Me.sMethod = <span style="color: #800000;">&quot;Login&quot;</span>
&nbsp;
        Msgbox <span style="color: #800000;">&quot;Login&quot;</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span> <span style="color: #008000;">'Login end
</span><span style="color: #0000A6;">Dim</span> NewLogin : <span style="color: #0000A6;">Set</span> NewLogin = <span style="color: #0000A6;">New</span> Login</pre></div></div>

<h5>LoginHandler (Outputs Method Names)</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Class</span> LoginHandler <span style="color: #008000;">'LoginHandler start
</span>
    <span style="color: #0000A6;">Public</span> bResult
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Sub</span> Class_Initialize
         <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Entering Try..&quot;</span> &amp; vbNewLine
         bResult = NewLogin.Result
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Sub</span> CatchErr
        <span style="color: #0000A6;">If</span> Err.Number = 0 <span style="color: #0000A6;">Then</span> <span style="color: #0000A6;">Exit</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
        <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Entering Catch..&quot;</span>
        <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Error occured in: &quot;</span> &amp; NewLogin.sMethod
        <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Error: &quot;</span> &amp; Err.Description &amp; vbNewLine
&nbsp;
        Err.Clear
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Sub</span> Class_Terminate : CatchErr
        <span style="color: #0000A6;">Print</span> <span style="color: #800000;">&quot;Exiting..&quot;</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span> <span style="color: #008000;">'LoginHandler end
</span>
<span style="color: #0000A6;">Set</span> NewLoginHandler = <span style="color: #0000A6;">New</span> LoginHandler
<span style="color: #0000A6;">Set</span> NewLoginHandler = <span style="color: #0000A6;">Nothing</span>
<span style="color: #0000A6;">Set</span> NewLogin = <span style="color: #0000A6;">Nothing</span></pre></div></div>

<p>
View <a href="http://relevantcodes.com/Articles/VBScriptTryCatchFinallyIntro/Example2.txt">code</a>.
</p>
<p>
Output of executing Login Class &#038; LoginHandler:
</p>
<div class="wp-caption aligncenter" style="width: 502px">
	<img alt="" src="http://relevantcodes.com/Articles/VBScriptTryCatchFinallyIntro/PrintLog2.png" title="Print Log" width="502" height="211" />
	<p class="wp-caption-text">QTP Print Log</p>
</div>
<h2>Notes</h2>
<p>
After using this technique for several days and manipulating it a bit from the original work, I have found some success. However, implementing Handler classes when the scope of the utility class increases can be quite challenging, and time consuming. I still believe its a start to something that we haven&#8217;t witnessed in QTP (yet). To be honest, I still prefer the old way of doing things where I can branch out most of my GUI code in conditional statements &#8211; which has enabled me to always create more comprehensive reporting.</p>
<p>
My recommendation would be to use this approach with VBScript code instead of using it for QTP&#8217;s GUI objects, ofcourse, until absolutely necessary. Most of my usage for the above technique has been with pure VBScript code which has enabled me to create better error handling. In an upcoming article, I will show how this technique will be used to handle Excel errors and recover from them in the <code>CatchErr</code> method using a Select Case block. Including Try-Catch-Finally statements within Try-Catch-Finally statements is quite easily done in .NET, but its very complex to perform the same operation with VBScript. I&#8217;m still happy to have a start though.
</p>
<p class="alert">
If you have any questions, please ask them in the comments section.</p>
]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/gui-objects-vbscript-try-catch-finally/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Eval Function &amp; Execute Statement</title>
		<link>http://relevantcodes.com/eval-function-execute-statement/</link>
		<comments>http://relevantcodes.com/eval-function-execute-statement/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 20:24:29 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[Eval]]></category>
		<category><![CDATA[Eval Function]]></category>
		<category><![CDATA[Execute]]></category>
		<category><![CDATA[Execute Statement]]></category>
		<category><![CDATA[QTP Eval Function]]></category>
		<category><![CDATA[QTP Execute]]></category>
		<category><![CDATA[QTP Execute Statement]]></category>
		<category><![CDATA[VBScript Eval]]></category>
		<category><![CDATA[VBScript Eval Function]]></category>
		<category><![CDATA[VBScript Execute]]></category>
		<category><![CDATA[VBScript Execute Statement]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=3447</guid>
		<description><![CDATA[This topic covers the concepts of the <code>Eval</code> function and <code>Execute</code> statement in depth. The function Eval is used to evaluate a string and return the result of the string expression. The Execute statement on the other hand, executes one or more statements in a string expression and enables creation of dynamic code. Multiple statements can be executed at once by using colons or line breaks to separate them.]]></description>
			<content:encoded><![CDATA[<p></p><p><img alt="" src="/Articles/EvalExecute/EvalExecuteThumb.png" title="Eval Function &#038; Execute Statement" class="alignright" width="200" height="72" /></p>
<p>
This topic covers the concepts of the <code>Eval</code> function and <code>Execute</code> statement in depth. The function Eval is used to evaluate a string and return the result of the string expression. The Execute statement on the other hand, executes one or more statements in a string expression and enables creation of dynamic code. Multiple statements can be executed at once by using colons or line breaks to separate them.
</p>
<p>If you have had a chance to use <a href="http://relevantcodes.com/relevantcodes1one-qtp-automation-framework/">RelevantCodes[1]One</a> and went over the function libraries, you would find that all the events are controlled through <code>Execute</code> statement &#8211; data is retrieved from the Excel Table and parsed by the function libraries into string statements. Once parsed, these string statements are then executed in the same manner as QTP code.
</p>
<h5>Eval Syntax</h5>
<pre>
Eval(expression)
</pre>
<h5>Execute Syntax</h5>
<pre>
Execute statement
</pre>
<h2>Difference between Eval &#038; Execute</h2>
<p>
In the syntax above, both <code>expression</code> and <code>statement</code> arguments are string statements. Both expressions can be derived from VBScript or QTP code; the difference lies in the fact that Eval will always return the result of the string evaluation whereas Execute will execute a string statement for execution and will not retrieve the result (but there are workarounds). The following example demonstrates the main difference between the 2 functions:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">x = 9
y = 10
&nbsp;
bIsEqual = Eval(<span style="color: #800000;">&quot;x = y&quot;</span>)
Execute <span style="color: #800000;">&quot;x = y&quot;</span>
&nbsp;
MsgBox <span style="color: #800000;">&quot;bIsEqual: &quot;</span> &amp; bIsEqual
MsgBox <span style="color: #800000;">&quot;X is no longer 9. It is: &quot;</span> &amp; x</pre></div></div>

<table style='text-align:center; margin-left:auto; margin-right:auto;'>
<tr>
<td><div class="wp-caption alignnone" style="width: 104px">
	<img alt="bIsEqual" src="/Articles/EvalExecute/bIsEqual.png" title="bIsEqual" width="104" height="107" />
	<p class="wp-caption-text">Eval Function</p>
</div></td>
<td><div class="wp-caption alignnone" style="width: 146px">
	<img alt="New x" src="/Articles/EvalExecute/NewX.png" title="New x" width="146" height="107" />
	<p class="wp-caption-text">Execute Statement</p>
</div></td>
</tr>
</table>
<p>
Note that Eval returned a Boolean value whereas Execute function performed a variable assignment. This article will cover the usage of both techniques in depth through both VBS and QTP code.
</p>
<p>
You must be wondering why the syntax for Eval and Execute is different. The reason is that, Eval is a function. Execute is not. That means, the following statement for Eval: <code>bIsEqual = Eval("x = y")</code>, when written for Execute will return a Null value for bResult (but assignment will be performed for <code>x</code>):
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">bResult = Execute(<span style="color: #800000;">&quot;x = y&quot;</span>)
MsgBox bResult
MsgBox <span style="color: #800000;">&quot;x: &quot;</span> &amp; x</pre></div></div>

<table style="margin-left:auto; margin-right:auto; text-align:center;">
<tr>
<td><div class="wp-caption aligncenter" style="width: 104px">
	<img alt="Null Output" src="/Articles/EvalExecute/Null.png" title="Null Output" width="104" height="95" />
	<p class="wp-caption-text">Null Output</p>
</div></td>
<td><div class="wp-caption alignnone" style="width: 104px">
	<img alt="Value Assigned" src="/Articles/EvalExecute/X.png" title="Value Assigned" width="104" height="107" />
	<p class="wp-caption-text">X = 10</p>
</div></td>
</tr>
</table>
<p>
With that said, let me share the workaround that can be used with Execute to return the evaluation just like we did with <code>Eval</code>. Here&#8217;s how:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">x = 9
y = 10
&nbsp;
Execute <span style="color: #800000;">&quot;z = (x = y)&quot;</span>
MsgBox <span style="color: #800000;">&quot;z: &quot;</span> &amp; z</pre></div></div>

<div class="wp-caption aligncenter" style="width: 104px">
	<img alt="Return Z (False)" src="/Articles/EvalExecute/ZFalse.png" title="Return Z (False)" width="104" height="107" />
	<p class="wp-caption-text">Return Z (False)</p>
</div>
<p>
More on this concept will be covered in the section <span class="emphasis_u">Evaluating Statements</span>.
</p>
<h2>Creating Variables</h2>
<p>
Let&#8217;s assume a scenario where you need to create the following variables: <code>var_1</code>, &#8230; , <code>var_5</code>. One way is to manually create the variables:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">var_1 = 1 : var_2 = 2 : var_3 = 3 : var_4 = 4: var_5 = 5</pre></div></div>

<p>
A quicker, and more dynamic way to achieve the same is through Execute statement:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">For</span> ix = 1 <span style="color: #0000A6;">to</span> 5
    Execute <span style="color: #800000;">&quot;var_&quot;</span> &amp; ix &amp; <span style="color: #800000;">&quot; = &quot;</span> &amp; ix
<span style="color: #0000A6;">Next</span></pre></div></div>

<h2>Option Explicit</h2>
<p>
Using the Execute statement also has a direct impact if the <code>Option Explicit</code> statement is ON. If a variable is not explicitly declared, an error will be occurred:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Option</span> <span style="color: #0000A6;">Explicit</span>
&nbsp;
iNumber = 9</pre></div></div>

<div class="wp-caption aligncenter" style="width: 274px">
	<img alt="Run Error" src="/Articles/EvalExecute/OptionExplicit.png" title="Run Error" width="274" height="153" />
	<p class="wp-caption-text">Run Error</p>
</div>
<p>
When the same variable is created through Execute, the error will not occur and the variable will be created and value assigned successfully.
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Option</span> <span style="color: #0000A6;">Explicit</span>
&nbsp;
Execute <span style="color: #800000;">&quot;iNumber = 9&quot;</span></pre></div></div>

<h2>Evaluating Statements</h2>
<p>
Instead of equating variables and values, we will evaluate statements using Eval. A quick example using <code>IsObject</code> is demonstrated below:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Set</span> dicObject = <span style="color: #0000A6;">CreateObject</span>(<span style="color: #800000;">&quot;Scripting.Dictionary&quot;</span>)
&nbsp;
<span style="color: #0000A6;">If</span> Eval(<span style="color: #800000;">&quot;IsObject(dicObject)&quot;</span>) <span style="color: #0000A6;">Then</span>
    MsgBox <span style="color: #800000;">&quot;Object created.&quot;</span>
    <span style="color: #0000A6;">Set</span> dicObject = <span style="color: #0000A6;">Nothing</span>
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span></pre></div></div>

<p>
Similarly, the above code can be broken into a <code>Select Case</code> block and variables can be evaluated by their type:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Sub</span> RetType(<span style="color: #0000A6;">ByVal</span> var)
	<span style="color: #0000A6;">Select</span> <span style="color: #0000A6;">Case</span> Eval(<span style="color: #800000;">&quot;TypeName(var)&quot;</span>)
		<span style="color: #0000A6;">Case</span> <span style="color: #800000;">&quot;Integer&quot;</span> : MsgBox <span style="color: #800000;">&quot;Integer&quot;</span>
		<span style="color: #0000A6;">Case</span> <span style="color: #800000;">&quot;Dictionary&quot;</span> : MsgBox <span style="color: #800000;">&quot;Dictionary&quot;</span>
		<span style="color: #0000A6;">Case</span> <span style="color: #800000;">&quot;Nothing&quot;</span> : MsgBox <span style="color: #800000;">&quot;Nothing&quot;</span>
		<span style="color: #0000A6;">Case</span> <span style="color: #800000;">&quot;String&quot;</span> : MsgBox <span style="color: #800000;">&quot;String&quot;</span>
	<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Select</span>
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
<span style="color: #0000A6;">Call</span> RetType(<span style="color: #0000A6;">Nothing</span>)
<span style="color: #0000A6;">Call</span> RetType(<span style="color: #800000;">&quot;Test&quot;</span>)
<span style="color: #0000A6;">Call</span> RetType(<span style="color: #0000A6;">CreateObject</span>(<span style="color: #800000;">&quot;Scripting.Dictionary&quot;</span>))
<span style="color: #0000A6;">Call</span> RetType(1)</pre></div></div>

<p>
Upon execution, the following results will be retrieved:
</p>
<table style='text-align:center; margin-left:auto; margin-right:auto;'>
<tr>
<td><div class="wp-caption alignnone" style="width: 104px">
	<img alt="Nothing" src="/Articles/EvalExecute/Nothing.png" title="Nothing" width="104" height="107" />
	<p class="wp-caption-text">RetType(Nothing)</p>
</div></td>
<td><div class="wp-caption alignnone" style="width: 104px">
	<img alt="String" src="/Articles/EvalExecute/String.png" title="String" width="104" height="107" />
	<p class="wp-caption-text">RetType('Test')</p>
</div></td>
<td><div class="wp-caption alignnone" style="width: 104px">
	<img alt="Dictionary" src="/Articles/EvalExecute/Dict.png" title="Dictionary" width="104" height="107" />
	<p class="wp-caption-text">RetType(CreateObject)</p>
</div></td>
<td><div class="wp-caption alignnone" style="width: 104px">
	<img alt="Integer" src="/Articles/EvalExecute/Integer.png" title="Integer" width="104" height="107" />
	<p class="wp-caption-text">RetType(1)</p>
</div></td>
</tr>
</table>
<p>
The same function above can be written through Execute. However, with Execute, we cannot directly retrieve the result (it is not a function, unlike Eval). Therefore, we will create a variable and store the result with the variable; the variable will then be used as the <code>Select Case</code> condition.
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Sub</span> RetType(<span style="color: #0000A6;">ByVal</span> var)
	<span style="color: #0000A6;">Dim</span> bResult
&nbsp;
	Execute <span style="color: #800000;">&quot;bResult = TypeName(var)&quot;</span>
&nbsp;
	<span style="color: #0000A6;">Select</span> <span style="color: #0000A6;">Case</span> bResult
		<span style="color: #0000A6;">Case</span> <span style="color: #800000;">&quot;Integer&quot;</span> : MsgBox <span style="color: #800000;">&quot;Integer&quot;</span>
		<span style="color: #0000A6;">Case</span> <span style="color: #800000;">&quot;Dictionary&quot;</span> : MsgBox <span style="color: #800000;">&quot;Dictionary&quot;</span>
		<span style="color: #0000A6;">Case</span> <span style="color: #800000;">&quot;Nothing&quot;</span> : MsgBox <span style="color: #800000;">&quot;Nothing&quot;</span>
		<span style="color: #0000A6;">Case</span> <span style="color: #800000;">&quot;String&quot;</span> : MsgBox <span style="color: #800000;">&quot;String&quot;</span>
	<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Select</span>
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
<span style="color: #0000A6;">Call</span> RetType(<span style="color: #0000A6;">Nothing</span>)
<span style="color: #0000A6;">Call</span> RetType(<span style="color: #800000;">&quot;Test&quot;</span>)
<span style="color: #0000A6;">Call</span> RetType(<span style="color: #0000A6;">CreateObject</span>(<span style="color: #800000;">&quot;Scripting.Dictionary&quot;</span>))
<span style="color: #0000A6;">Call</span> RetType(1)</pre></div></div>

<p>
In short then, with Execute, bResult is represented within the string statement:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">Execute <span style="color: #800000;">&quot;bResult = TypeName(var)&quot;</span></pre></div></div>

<p>
With Eval, bResult is an actual variable that holds the result like below:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">bResult = Eval(<span style="color: #800000;">&quot;TypeName(var)&quot;</span>)</pre></div></div>

<h2>Creating Functions</h2>
<p>
I have seen this approach rarely used, but its important to know all the possibilities nonetheless. Yes, we can create Functions (really!) at run-time, as long as everything is a string and new lines are separated by colons or a line feed. This approach is generally fine for small functions, but it can get really confusing and hard to debug as the lines of code increase.
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">sFunc = <span style="color: #800000;">&quot;Function ExecuteTest&quot;</span>
sFunc = sFunc &amp; vbLf
sFunc = sFunc &amp; <span style="color: #800000;">&quot;MsgBox &quot;</span><span style="color: #800000;">&quot;ExecuteTest executed.&quot;</span><span style="color: #800000;">&quot;&quot;</span>
sFunc = sFunc &amp; vbLf
sFunc = sFunc &amp; <span style="color: #800000;">&quot;End Function&quot;</span>
&nbsp;
Execute sFunc
&nbsp;
<span style="color: #0000A6;">Call</span> ExecuteTest()</pre></div></div>

<div class="wp-caption aligncenter" style="width: 110px">
	<img alt="Function ExecuteTest()" src="/Articles/EvalExecute/ExecuteTest.png" title="Function ExecuteTest()" width="110" height="107" />
	<p class="wp-caption-text">Function ExecuteTest()</p>
</div>
<p>
We can write the same code above using colons (<code>:</code>) as well:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">Execute <span style="color: #800000;">&quot;Function ExecuteTest : MsgBox &quot;</span><span style="color: #800000;">&quot;ExecuteTest executed.&quot;</span><span style="color: #800000;">&quot; : End Function&quot;</span>
&nbsp;
<span style="color: #0000A6;">Call</span> ExecuteTest()</pre></div></div>

<h2>Executing QTP Statements</h2>
<p>
This is where the Execute statement absolutely shines. As stated before, <a href="http://relevantcodes.com/relevantcodes1one-qtp-automation-framework/">RelevantCodes[1]One</a> executes all keywords through Execute statement. It retrieves keywords from the Excel Table as strings at runtime, creates the relevant hierarchies, and executes the hierarchies with chosen events. The concept here and the concepts covered by previous examples will remain the same: we create action strings and execute them. The last example in this topic will show how different strings can be joined together to implement events on supplied test-objects (as strings).
</p>
<p>
Let&#8217;s start with a quick example to check if our target browser exists:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">strBrowser = <span style="color: #800000;">&quot;Browser(&quot;</span><span style="color: #800000;">&quot;title:=Google&quot;</span><span style="color: #800000;">&quot;)&quot;</span>
&nbsp;
Execute <span style="color: #800000;">&quot;MsgBox &quot;</span> &amp; strBrowser &amp; <span style="color: #800000;">&quot;.Exist(0)&quot;</span></pre></div></div>

<p>
The above code can also be written with Eval as well, enabling us to retrieve the value directly instead of using the workaround approach if we were to use Execute:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">strBrowser = <span style="color: #800000;">&quot;Browser(&quot;</span><span style="color: #800000;">&quot;title:=Google&quot;</span><span style="color: #800000;">&quot;)&quot;</span>
&nbsp;
bExist = Eval(strBrowser &amp; <span style="color: #800000;">&quot;.Exist(0)&quot;</span>)</pre></div></div>

<p>
The above statement when executed does the same operation as it would when you would verify the browser existence using an inline DP statement: <code>Msgbox Browser("title:=Google").Exist(0)</code>. Let&#8217;s consider another example where we create an event on a WebEdit object by creating an object hierarchy through strings at run time.
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">strBrowser = <span style="color: #800000;">&quot;Browser(&quot;</span><span style="color: #800000;">&quot;title:=Google&quot;</span><span style="color: #800000;">&quot;)&quot;</span>  <span style="color: #008000;">'Browser(&quot;title:=Google&quot;)
</span>strPage    = <span style="color: #800000;">&quot;Page(&quot;</span><span style="color: #800000;">&quot;title:=Google&quot;</span><span style="color: #800000;">&quot;)&quot;</span>     <span style="color: #008000;">'Page(&quot;title:=Google&quot;)
</span>strText    = <span style="color: #800000;">&quot;WebEdit(&quot;</span><span style="color: #800000;">&quot;name:=q&quot;</span><span style="color: #800000;">&quot;)&quot;</span>        <span style="color: #008000;">'WebEdit(&quot;name:=q&quot;)
</span>strEvent   = <span style="color: #800000;">&quot;Set &quot;</span><span style="color: #800000;">&quot;Execute Test&quot;</span><span style="color: #800000;">&quot;&quot;</span>        <span style="color: #008000;">'Set &quot;Execute Test&quot;
</span>
Execute strBrowser &amp; <span style="color: #800000;">&quot;.&quot;</span> &amp; strPage &amp; <span style="color: #800000;">&quot;.&quot;</span> &amp; strText &amp; <span style="color: #800000;">&quot;.&quot;</span> &amp; strEvent</pre></div></div>

<p>
It does look interesting, doesn&#8217;t it? The same approach can be used if you are using Object Repository instead of Descriptive Programming; simply replace the Programmatic Descriptions with the Logical Descriptions in your OR, parse everything in a string and Execute it!
</p>
<p>
The example below shows how QTP code can be executed into succession through strings. The last 4 statements show how all strings are combined together to 1. Enter the search terms, 2. Click Search Button, 3. Synchronize and 4. Navigate back to the Google search page.
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">strBrowser = <span style="color: #800000;">&quot;Browser(&quot;</span><span style="color: #800000;">&quot;title:=.*Google.*&quot;</span><span style="color: #800000;">&quot;)&quot;</span>      <span style="color: #008000;">'Browser(&quot;title:=Google&quot;)
</span>strPage    = <span style="color: #800000;">&quot;Page(&quot;</span><span style="color: #800000;">&quot;title:=.*Google.*&quot;</span><span style="color: #800000;">&quot;)&quot;</span>         <span style="color: #008000;">'Page(&quot;title:=Google&quot;)
</span>strText    = <span style="color: #800000;">&quot;WebEdit(&quot;</span><span style="color: #800000;">&quot;name:=q&quot;</span><span style="color: #800000;">&quot;)&quot;</span>                <span style="color: #008000;">'WebEdit(&quot;name:=q&quot;)
</span>strButton  = <span style="color: #800000;">&quot;WebButton(&quot;</span><span style="color: #800000;">&quot;value:=Google Search&quot;</span><span style="color: #800000;">&quot;)&quot;</span> <span style="color: #008000;">'WebButton(&quot;value:=Google Search&quot;)
</span>strEvent   = <span style="color: #800000;">&quot;Set &quot;</span><span style="color: #800000;">&quot;Execute Test&quot;</span><span style="color: #800000;">&quot;&quot;</span>                <span style="color: #008000;">'Set &quot;Execute Test&quot;
</span>strClick   = <span style="color: #800000;">&quot;Click&quot;</span>                               <span style="color: #008000;">'Click
</span>strSync    = <span style="color: #800000;">&quot;Sync&quot;</span>                                <span style="color: #008000;">'Sync
</span>strBack    = <span style="color: #800000;">&quot;Back&quot;</span>                                <span style="color: #008000;">'Back
</span>
Execute strBrowser &amp; <span style="color: #800000;">&quot;.&quot;</span> &amp; strPage &amp; <span style="color: #800000;">&quot;.&quot;</span> &amp; strText &amp; <span style="color: #800000;">&quot;.&quot;</span> &amp; strEvent
Execute strBrowser &amp; <span style="color: #800000;">&quot;.&quot;</span> &amp; strPage &amp; <span style="color: #800000;">&quot;.&quot;</span> &amp; strButton &amp; <span style="color: #800000;">&quot;.&quot;</span> &amp; strClick
Execute strBrowser &amp; <span style="color: #800000;">&quot;.&quot;</span> &amp; strSync
Execute strBrowser &amp; <span style="color: #800000;">&quot;.&quot;</span> &amp; strBack</pre></div></div>

<p>
The above strings are equivalent to the following QTP code:
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">Browser(<span style="color: #800000;">&quot;title:=.*Google.*&quot;</span>).Page(<span style="color: #800000;">&quot;title:=.*Google.*&quot;</span>).WebEdit(<span style="color: #800000;">&quot;name:=q&quot;</span>).<span style="color: #0000A6;">Set</span> <span style="color: #800000;">&quot;Execute Test&quot;</span>
Browser(<span style="color: #800000;">&quot;title:=.*Google.*&quot;</span>).Page(<span style="color: #800000;">&quot;title:=.*Google.*&quot;</span>).WebButton(<span style="color: #800000;">&quot;value:=Google Search&quot;</span>).Click
Browser(<span style="color: #800000;">&quot;title:=.*Google.*&quot;</span>).Sync
Browser(<span style="color: #800000;">&quot;title:=.*Google.*&quot;</span>).Back</pre></div></div>

<h2>Closing Remarks</h2>
<p>
I hope this article has covered most of the common (and uncommon) uses of Eval and Execute. I&#8217;m sure you&#8217;ll find areas in your framework where these 2 techniques will make things cleaner and more dynamic. If there is something I have omitted, please feel free to share it with us. I hope you guys find this article useful :)
</p>
<p class="alert">If you have any questions, please ask them in the comments section. If your query is confidential, please use the <a href="http://relevantcodes.com/contact/">Contact Form</a> to send me an e-mail instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/eval-function-execute-statement/feed/</wfw:commentRss>
		<slash:comments>43</slash:comments>
		</item>
		<item>
		<title>VBScript: Compare 2 Excel Files</title>
		<link>http://relevantcodes.com/vbscript-compare-2-excel-files/</link>
		<comments>http://relevantcodes.com/vbscript-compare-2-excel-files/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 18:31:29 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[Check Differences Between Excel Sheets]]></category>
		<category><![CDATA[Compare]]></category>
		<category><![CDATA[Compare 2 Excel Sheets]]></category>
		<category><![CDATA[Compare Excel]]></category>
		<category><![CDATA[Compare Excel Books]]></category>
		<category><![CDATA[Compare Two Excel Sheets]]></category>
		<category><![CDATA[Excel.Application]]></category>
		<category><![CDATA[VBScript Excel]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=3215</guid>
		<description><![CDATA[This post shows a quick and easy way to compare 2 Excel files. I know there are several code snippets on the inter-webs that show how to compare 2 Excel files, but what makes this technique different is performance. I know faster techniques are possible, but after much experiment, this was the one that continuously worked at the best speed in comparison to other methods.]]></description>
			<content:encoded><![CDATA[<p></p><p>
This post shows a quick and easy way to compare 2 Excel files. I know there are several code snippets on forums that show how to compare do this, so you may ask the following question: how is this technique any different? What makes this technique different is performance. I know there may be faster ways to achieve this, but after much experiment, this was the one that continuously worked at the best speed in comparison to other methods.
</p>
<p><h4 style="margin-left: auto; margin-right: auto; text-align: center;"><a href="http://relevantcodes.com/Articles/CompareExcelSheets/clsComparer.cls.vbs.zip">Download Class</a> | <a href="http://relevantcodes.com/Articles/CompareExcelSheets/clsComparer.cls.vbs.txt">View Class</a></h4>
<p> &nbsp;
</p>
<p>
The main concept of this technique is performing all validations through a <code>range object</code>, instead of traversing all cells of an Excel sheet (which takes a little longer). Below is the skeleton of how the class is arranged, with documentation. Please use one of the download links above to download this class. There is a usage example shown at the end of this article.
</p>
<h5>Class clsExcelComparer</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Class</span> clsComparer
	<span style="color: #008000;">'[--- Region Private Variables Start ---]
</span>
	<span style="color: #0000A6;">Private</span> oExcel		<span style="color: #008000;">'Excel.Application
</span>	<span style="color: #0000A6;">Private</span> arrRangeUno	<span style="color: #008000;">'Range.Value (array) of the Primary Excel spreadsheet
</span>	<span style="color: #0000A6;">Private</span> arrRangeDos	<span style="color: #008000;">'Range.Value (array) of the Secondary Excecl spreadsheet
</span>	<span style="color: #0000A6;">Private</span> oDict		<span style="color: #008000;">'Scripting.Dictionary containing unmatched cells
</span>
	<span style="color: #008000;">'[--- Region Private Variables End ---]
</span>
&nbsp;
	<span style="color: #008000;">'[--- Region Public Variables Start ---]
</span>
	<span style="color: #0000A6;">Public</span> Operation	<span style="color: #008000;">'0: Only Compare   1: Compare &amp; Highlight Differences
</span>
	<span style="color: #008000;">'[--- Region Public Variables End ---]
</span>
&nbsp;
	<span style="color: #008000;">'--------------------------------------------------------
</span>	<span style="color: #008000;">' Name: Function Compare [Public]
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Remarks: N/A
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Purpose: Compares differences between 2 Excel Spreadsheets
</span>	<span style="color: #008000;">'	
</span>	<span style="color: #008000;">' Arguments:
</span>	<span style="color: #008000;">'	sWorkBookUno: Primary Excel WorkBook (with complete path)
</span>	<span style="color: #008000;">'	vSheetUno: Primary Excel Spreadsheet Name
</span>	<span style="color: #008000;">'	sWorkBookDos: Secondary Excel WorkBook (with complete path)
</span>	<span style="color: #008000;">'	vSheetDos: Secondary Excel Spreadsheet Name
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Return: Boolean
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Author: Anshoo Arora, Relevant Codes
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Date: 03/17/2010
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' References: N/A
</span>	<span style="color: #008000;">'--------------------------------------------------------
</span>	<span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> <span style="color: #0000A6;">Compare</span>(sWorkBookUno, vSheetUno, sWorkBookDos, vSheetDos)
&nbsp;
	<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
	<span style="color: #008000;">'--------------------------------------------------------
</span>	<span style="color: #008000;">' Name: Function CellsFound [Private]
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Remarks: N/A
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Purpose: Finds the dissimilar cells between 2 sheets
</span>	<span style="color: #008000;">'	
</span>	<span style="color: #008000;">' Arguments: N/a
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Return: Integer
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Author: Anshoo Arora, Relevant Codes
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Date: 03/17/2010
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' References: N/A
</span>	<span style="color: #008000;">'--------------------------------------------------------
</span>	<span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Function</span> CellsFound()
&nbsp;
	<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
	<span style="color: #008000;">'--------------------------------------------------------
</span>	<span style="color: #008000;">' Name: Sub Class_Terminate [Private]
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Remarks: N/A
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Purpose: Disposes the Excel.Application object
</span>	<span style="color: #008000;">'	
</span>	<span style="color: #008000;">' Arguments: N/A
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Author: Anshoo Arora, Relevant Codes
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' Date: 03/17/2010
</span>	<span style="color: #008000;">'
</span>	<span style="color: #008000;">' References: N/A
</span>	<span style="color: #008000;">'--------------------------------------------------------
</span>	<span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Sub</span> Class_Terminate()
&nbsp;
	<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span>
&nbsp;
<span style="color: #008000;">'--------------------------------------------------------
</span><span style="color: #008000;">' Name: Function CompareExcelSheets
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' Remarks: N/A
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' Purpose: Constructor for Class clsComparer
</span><span style="color: #008000;">'	
</span><span style="color: #008000;">' Arguments:
</span><span style="color: #008000;">'	sWorkBookUno: Primary Excel WorkBook (with complete path)
</span><span style="color: #008000;">'	vSheetUno: Primary Excel Spreadsheet Name
</span><span style="color: #008000;">'	sWorkBookDos: Secondary Excel WorkBook (with complete path)
</span><span style="color: #008000;">'	vSheetDos: Secondary Excel Spreadsheet Name
</span><span style="color: #008000;">'	Operation: 0: Compare Only   1: Compare &amp; Highlight Differences
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' Return: Boolean
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' Author: Anshoo Arora, Relevant Codes
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' Date: 03/17/2010
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' References: N/A
</span><span style="color: #008000;">'--------------------------------------------------------
</span><span style="color: #0000A6;">Function</span> CompareExcelSheets(sWorkBookUno, vSheetUno, sWorkBookDos, vSheetDos, Operation)
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span></pre></div></div>

<h4 style="margin-left: auto; margin-right: auto; text-align: center;"><a class="downloadlink" href="http://relevantcodes.com/wp-content/plugins/download-monitor/download.php?id=4" title="Version1.0 downloaded 645 times" >Download clsComparer (645)</a> | <a class="downloadlink" href="http://relevantcodes.com/wp-content/plugins/download-monitor/download.php?id=5" title="Version1.0 downloaded 602 times" >View clsComparer (602)</a></h4>
<h2>Usage (Example)</h2>
<p>
Consider the following 2 Excel Sheets, with a few dissimilar cells:
</p>
<div class="wp-caption aligncenter" style="width: 585px">
	<img alt="Sheet #1 Original" src="http://relevantcodes.com/Articles/CompareExcelSheets/SheetUno.png" title="Sheet #1 Original" width="585" height="120" />
	<p class="wp-caption-text">Sheet #1 Original</p>
</div>  <div class="wp-caption aligncenter" style="width: 585px">
	<img alt="Sheet #2 Original" src="http://relevantcodes.com/Articles/CompareExcelSheets/SheetDos.png" title="Sheet #2 Original" width="585" height="120" />
	<p class="wp-caption-text">Sheet #2 Original</p>
</div>
<p>
There are 2 main operations that can be done through our code. <code>1.</code> We can simply check if both Excel Sheets are similar and <code>2.</code> we can check as well as highlight the differences between the two. To only compare the 2 sheets, without highlighting the differences the <code>Operation</code> argument will have a value of <code>0</code>. On the other hand, to compare as well as highlight the values, the <code>Operation</code> argument will have a value of <code>1</code>. Let&#8217;s first consider a case where the 2 sheets are only to be compared:
</p>
<h5>Compare Only</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #008000;">'Only Compare:
</span>MsgBox CompareExcelSheets(<span style="color: #800000;">&quot;C:\Test1.xls&quot;</span>, <span style="color: #800000;">&quot;Sheet1&quot;</span>, <span style="color: #800000;">&quot;C:\Test2.xls&quot;</span>, <span style="color: #800000;">&quot;Sheet1&quot;</span>, 0)</pre></div></div>

<p>
Because the 2 sheets are not similar, the following will be the output upon execution of the above statement:
</p>
<div class="wp-caption aligncenter" style="width: 107px">
	<img alt="Compare Only" src="http://relevantcodes.com/Articles/CompareExcelSheets/CompareOnly.png" title="Compare Only" width="107" height="109" />
	<p class="wp-caption-text">Compare Only</p>
</div>
<p>
Also, the 2 sheets will remain unchanged.
</p>
<p>
When changing the value of <code>Operation</code> to 1, we will notice that, not only does the code compare the 2 sheets, but also highlights the differences.
</p>
<h5>Compare and Highlight Differences</h5>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #008000;">'Compare and highlight differences:
</span>MsgBox CompareExcelSheets(<span style="color: #800000;">&quot;C:\Test1.xls&quot;</span>, <span style="color: #800000;">&quot;Sheet1&quot;</span>, <span style="color: #800000;">&quot;C:\Test2.xls&quot;</span>, <span style="color: #800000;">&quot;Sheet1&quot;</span>, 1)</pre></div></div>

<p>
Upon execution of the above statement, the following 2 sheets will be highlighed in areas where they differ:
</p>
<div class="wp-caption aligncenter" style="width: 584px">
	<img alt="Sheet #1 With Highlighted Differences" src="http://relevantcodes.com/Articles/CompareExcelSheets/SheetUnoHighlighted.png" title="Sheet #1 With Highlighted Differences" width="584" height="121" />
	<p class="wp-caption-text">Sheet #1 With Highlighted Differences</p>
</div>  <div class="wp-caption aligncenter" style="width: 585px">
	<img alt="Sheet #2 with Highlighted Differences" src="http://relevantcodes.com/Articles/CompareExcelSheets/SheetDosHighlighted.png" title="Sheet #2 with Highlighted Differences" width="585" height="120" />
	<p class="wp-caption-text">Sheet #2 with Highlighted Differences</p>
</div>
<h2>Limitations</h2>
<p>
1. This code will work with Excel Sheets with the same number of rows and columns in each (like ranges).
</p>
<h2>Update: RelevantCodes.Comparer COM DLL</h2>
<p>
The following .NET assembly is available as well to download: <a href="http://relevantcodes.com/Articles/RelevantCodes.Comparer/RelevantCodes.zip" rel="nofollow">here</a>.
</p>
<p>
Snapshot of the assembly can be found <a href="http://relevantcodes.com/Articles/RelevantCodes.Comparer/RelevantCodes.Comparer.png" rel="nofollow">here</a>.
</p>
<p>
To register the assembly to your PC, the following code can be used:
</p>
<pre>
RegAsm C:\RelevantCodes.dll /codebase
</pre>
<p>
or
</p>
<pre>
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe C:\RelevantCodes.dll /codebase
</pre>
<p>
Once done, the code can be used in QTP like this:
</p>
<pre>
Set oCompare = CreateObject("RelevantCodes.Comparer")
</pre>
<p>
<code>oCompare.</code> should give you the proper intellisense to use all methods. The assembly compares the following items:</p>
<ol>
<li>Arrays</li>
<li>Excel Sheets</li>
<li>Images</li>
<li>Integers</li>
<li>Strings</li>
<li>TextFiles</li>
</ol>
<p>
I hope you find this useful :)
</p>
<p class="alert">If you have any questions, please ask them in the comments section. If your query is confidential, please use the <a href="http://relevantcodes.com/contact/">Contact Form</a> to send me an e-mail instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/vbscript-compare-2-excel-files/feed/</wfw:commentRss>
		<slash:comments>73</slash:comments>
		</item>
		<item>
		<title>RelevantCodes[1]One &#8211; QTP Automation Framework</title>
		<link>http://relevantcodes.com/relevantcodes1one-qtp-automation-framework/</link>
		<comments>http://relevantcodes.com/relevantcodes1one-qtp-automation-framework/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 10:51:11 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Automation Frameworks]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[Automation Framework]]></category>
		<category><![CDATA[Automation Framework QTP]]></category>
		<category><![CDATA[Excel Automation Framework]]></category>
		<category><![CDATA[QTP Automation Framework]]></category>
		<category><![CDATA[QTP Automation Framework Demo]]></category>
		<category><![CDATA[QTP Framework]]></category>
		<category><![CDATA[QTP Framework Demo]]></category>
		<category><![CDATA[Relevant Codes Framework]]></category>
		<category><![CDATA[RelevantCodes[1]One]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=2068</guid>
		<description><![CDATA[The first release of RelevantCodes[1]One, a Keyword-Driven Automation Framework Demo Project is available and ready for download. Usage examples and Videos included.]]></description>
			<content:encoded><![CDATA[<p></p><p><img alt="" src="http://relevantcodes.com/Articles/RelevantCodesOne/ON[1]E.png" title="RelevantCodes[1]One" class="alignright" /></p>
<p>
The first release of RelevantCodes[1]One is ready, and has been successfully tested on QTP versions 9.2, 9.5 and 10.0. I would like to thank Clive Farrington, Mahesh Upadhyay and Sunitha Reddy for helping me beta-test this framework. I really appreciate your efforts, guys!
</p>
<p class="centeralign alert"><span style='font-weight:bold; font-family:Calibri; font-size:1.2em;'><a class="downloadlink" href="http://relevantcodes.com/wp-content/plugins/download-monitor/download.php?id=1" title="Version0.1b downloaded 6171 times" >Download RelevantCodes[1]One (6171)</a></span></p>
<p class="alert">Be sure to view the videos at the end of this post, which show RelevantCodes[1]One being initiated through the Excel Table, and full executions on versions 9.2 and 10.0.</p>
<p class="note">January 27, 2010: Added examples. These show how DP statements can be converted to RelevantCodes[1]One keywords.</p>
<p><h2>Features:</h2>
<ul>
<li>OO Architecture</li>
<li>Custom DOTNetFactory Form</li>
<li>Hierarchical Reporting</li>
<li>Product Version Settings</li>
<li>Single Block for Loading and Unloading Global Objects</li>
<li>Advanced Dictionary Object usage</li>
<li>Generic Structure (Currently supports Web &#038; Standard Windows)</li>
<li>Simple Validations</li>
<li>Custom CheckPoints</li>
<li>Supports execution on multiple windows/browsers (see Test Case 002)</li>
<li>Simple Reporting</li>
<li>Data Sheet with Custom Macros (macros will not available for viewing)</li>
<li>Customized Test Execution</li>
<li>Supports multiple object descriptions</li>
</ul>
<p>
I have tried to keep everything quite basic in this release, but there will be several enhancements in the next, including a <strong>CustomUserActions object</strong> which will give ability to create tests from the Table as well as perform custom actions through code simultaneously. Also in the next release will be featured an upgrade to the Implementor Class, that will enable multiple iterations of a group of statements from the Data Table.
</p>
<p><h2>Setting Up</h2>
<p>1. Download, unzip and copy the RelevantCodes folder to your &#8220;C:\&#8221;. The folder structure should look exactly like:
</p>
<pre>
C:\RelevantCodes\RelevantCodes[1]One\0.1
</pre>
<p>
2. In the folder <code>C:\RelevantCodes\RelevantCodes[1]One\0.1\Test Resources\Excel Tables</code>: change the name of the relevant table to <span class="highlight">Data.xls</span>. If you are using QTP <span class="highlight">9.2 or below</span>, rename Data92.xls to Data.xls. For versions <span class="highlight">9.5 and higher</span>, rename Data95+.xls to Data.xls.
</p>
<p><h2>Current Version</h2>
<p>0.1 Beta
</p>
<p><h2>Found a Bug. What should I do now?</h2>
<p>If any issues that you may encounter, please use the comments section to notify me. I will try to have the fix ready, and the fix will be implemented in the next version, giving full credit to you for finding the bug. If the bug requires a small fix, then, the solution will be available for everyone to view/download immediately.
</p>
<p><h2>Will it support other technologies other than Web and Standard Windows?</h2>
<p>Technically, this design is capable of working with all sorts of technologies, but because I don&#8217;t have access to other add-ins, this may not be possible in the near future. However, if someone is willing to utilize their time in this upgrade, you are most welcome to. If you get this framework to work with other technologies successfully, I would be happy to share it with the everyone giving full credit to your extraordinary work.
</p>
<p><h2>Does RelevantCodes[1]One come under any warranties?</h2>
<p>No, and Relevant Codes, Anshoo Arora or any associated parties cannot be held responsible for any damage caused by the use of RelevantCodes[1]One.
</p>
<p><h2>Copyright Notice / License Agreement</h2>
<p>RelevantCodes[1]One is being released under <span class="highlight">Creative Commons 3.0 Attribution License</span>. For more information about this license, please view <a href="http://creativecommons.org/about/licenses/">this topic</a> at <a href="http://creativecommons.org/">Creative Commons</a> website. In other words, you can <a href="http://creativecommons.org/licenses/by/3.0/legalcode">distribute, remix, tweak, and build upon</a> this framework as long as you give proper credit to the authors, and to RelevantCodes.com. You must also leave the terms in the License Agreement in the source folder unchanged. <a href="http://creativecommons.org/licenses/by/3.0/legalcode">Click here</a> to view the Creative Commons 3.0 Attribution license legal code.
</p>
<p class="centeralign alert">
<a rel="license" href="http://creativecommons.org/licenses/by/3.0/us/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by/3.0/us/88x31.png" /></a><br /><span xmlns:dc="http://purl.org/dc/elements/1.1/" href="http://purl.org/dc/dcmitype/Text" property="dc:title" rel="dc:type">RelevantCodes[1]One Automation Framework</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="http://relevantcodes.com" property="cc:attributionName" rel="cc:attributionURL">Anshoo Arora [RelevantCodes.com]</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/3.0/us/">Creative Commons Attribution 3.0 United States License</a>.<br />Based on a work at <a xmlns:dc="http://purl.org/dc/elements/1.1/" href="http://relevantcodes.com" rel="dc:source">relevantcodes.com</a>.
</p>
<p></p>
<h2>Defects</h2>
<p>
1. <code>Implementor.cls.vbs => Function CheckProperty</code>: The value parsed after using Mid(), the property value stored in sExpected the result is stored in a variable cast as string, however, when using Execute() to set the value of sValue the value is being stored in a variable cast as boolean when performing the compare on these two variable types the checkpoint is failing when it should be passing. [<a href="/Articles/RelevantCodesOne/truenottrue.jpg">Snapshot</a>] [Credits go to Brock Vietor for reporting this defect; this defect is reported as-is from Brock's e-mail. Brock: Many thanks for your great reporting style and wonderful feedback!] </p>
<p>2. <code>Engine.vbs.cls</code>: Remove/Comment line #15 (Private TestData). Instead, insert <code>Public TestData</code> on line 2. This should fix the bug as reported in <a href="http://relevantcodes.com/relevantcodes1one-qtp-automation-framework/#comment-2009">this comment</a>. [Credits go to Daim Phillips for finding and reporting this defect. Daim: Thanks for all your time and efforts that went into finding and reporting this defect!]
</p>
<h2>Converting DP Into RelevantCodes[1]One Keywords</h2>
<p><h5>Example 1: Click Image</h5>
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">Browser(<span style="color: #800000;">&quot;title:=Test&quot;</span>).Page(<span style="color: #800000;">&quot;title:=Test&quot;</span>).Image(<span style="color: #800000;">&quot;name:=ThisImage&quot;</span>).Click</pre></div></div>

<pre>
Parent       | Child | Description     | Event | Value
--------------------------------------------------------------------------
Browser Page | Image | name:=ThisImage | Click | ""
</pre>
<p><h5>Example 2: Click Image (Multiple Properties)</h5>
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">Browser(<span style="color: #800000;">&quot;title:=Test&quot;</span>).Page(<span style="color: #800000;">&quot;title:=Test&quot;</span>).Image(<span style="color: #800000;">&quot;name:=ThisImage&quot;</span>, <span style="color: #800000;">&quot;index:=0&quot;</span>).Click</pre></div></div>

<pre>
Parent       | Child | Description                 | Event | Value
--------------------------------------------------------------------------
Browser Page | Image | [name:=ThisImage][index:=0] | Click |
</pre>
<p><h5>Example 3: Enter Value in a Text Box</h5>
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">Browser(<span style="color: #800000;">&quot;title:=Test&quot;</span>).Page(<span style="color: #800000;">&quot;title:=Test&quot;</span>).WebEdit(<span style="color: #800000;">&quot;name:=ThisText&quot;</span>).<span style="color: #0000A6;">Set</span> <span style="color: #800000;">&quot;RelevantCodes[1]One&quot;</span></pre></div></div>

<pre>
Parent       | Child   | Description    | Event | Value
--------------------------------------------------------------------------
Browser Page | WebEdit | name:=ThisText | Set   | RelevantCodes[1]One
</pre>
<p><h5>Example 4: Launching a Browser and Clicking &#8220;Links&#8221;</h5>
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">SystemUtil.Run <span style="color: #800000;">&quot;iexplore.exe&quot;</span>, <span style="color: #800000;">&quot;http://relevantcodes.com&quot;</span>
Browser(<span style="color: #800000;">&quot;title:=.*Relevant Codes.*&quot;</span>).Link(<span style="color: #800000;">&quot;innertext:=Links&quot;</span>).Click</pre></div></div>

<pre>
Parent       | Child | Description      | Event  | Value
--------------------------------------------------------------------------
Browser      |       | Relevant Codes   | Launch | http://relevantcodes.com
Browser      | Link  | innertext:=Links | Click  |
</pre>
<p><h5>Example 5: Launching a Browser, Clicking &#8220;Links&#8221; and closing the Browser</h5>
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">SystemUtil.Run <span style="color: #800000;">&quot;iexplore.exe&quot;</span>, <span style="color: #800000;">&quot;http://relevantcodes.com&quot;</span>
Browser(<span style="color: #800000;">&quot;title:=.*Relevant Codes.*&quot;</span>).Link(<span style="color: #800000;">&quot;innertext:=Links&quot;</span>).Click
Browser(<span style="color: #800000;">&quot;title:=.*Relevant Codes.*&quot;</span>).<span style="color: #0000A6;">Close</span></pre></div></div>

<pre>
Parent       | Child | Description      | Event  | Value
--------------------------------------------------------------------------
Browser      |       | Relevant Codes   | Launch | http://relevantcodes.com
Browser      | Link  | innertext:=Links | Click  |
Browser      |       | Relevant Codes   | Close  |
</pre>
<p><h5>Example 6: Launching a Browser, Clicking &#8220;Links&#8221;, Synchronizing and closing the Browser</h5>
</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">SystemUtil.Run <span style="color: #800000;">&quot;iexplore.exe&quot;</span>, <span style="color: #800000;">&quot;http://relevantcodes.com&quot;</span>
Browser(<span style="color: #800000;">&quot;title:=.*Relevant Codes.*&quot;</span>).Link(<span style="color: #800000;">&quot;innertext:=Links&quot;</span>).Click
Browser(<span style="color: #800000;">&quot;title:=.*Relevant Codes.*&quot;</span>).Sync
Browser(<span style="color: #800000;">&quot;title:=.*Relevant Codes.*&quot;</span>).<span style="color: #0000A6;">Close</span></pre></div></div>

<pre>
Parent       | Child | Description      | Event  | Value
--------------------------------------------------------------------------
Browser      |       | Relevant Codes   | Launch | http://relevantcodes.com
Browser      | Link  | innertext:=Links | Click  |
Browser      |       | Relevant Codes   | Sync   |
Browser      |       | Relevant Codes   | Close  |
</pre>
<p><h5>Example 7: Adding a new Browser to the Object Class</h5>
</p>
<p class="alert"><strong>Note:</strong> The Activate method can be used to Activate a Browser, as well as add it to the clsImplementor Object Collection. This example shows how a resulting (new) browser <code>TestDomain2</code> is added to the collection by specifying its properties.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #008000;">'Open New Browser
</span>SystemUtil.Run <span style="color: #800000;">&quot;iexplore.exe&quot;</span>, <span style="color: #800000;">&quot;http://Domain.com&quot;</span>
<span style="color: #008000;">'Clicking the link opens another Browser
</span>Browser(<span style="color: #800000;">&quot;title:=testDomain_1&quot;</span>).Link(<span style="color: #800000;">&quot;innertext:=OpenNewBrowser&quot;</span>).Click
<span style="color: #008000;">'Synchronize with new browser
</span>Browser(<span style="color: #800000;">&quot;title:=testDomain_2&quot;</span>).Sync
Browser(<span style="color: #800000;">&quot;title:=testDomain_2&quot;</span>).<span style="color: #0000A6;">Close</span>
Browser(<span style="color: #800000;">&quot;title:=testDomain_1&quot;</span>).<span style="color: #0000A6;">Close</span></pre></div></div>

<pre>
Parent       | Child | Description      | Event  | Value
--------------------------------------------------------------------------
Browser      |       | TestDomain1               | Launch   | http://Domain.com
Browser      | Link  | innertext:=OpenNewBrowser | Click    |
Browser      |       | TestDomain2               | Activate | title:=testDomain_2
Browser      |       | TestDomain2               | Sync     |
Browser      |       | TestDomain2               | Close    |
Browser      |       | TestDomain1               | Activate |
Browser      |       | TestDomain1               | Close    |
</pre>
<p><h2>Demos</h2>
<p>I have created 3 simple runs. The first demonstrates RelevantCodes[1]One usage with a single browser, the second run demonstrates its usage with multiple browsers simultaneously and the last one demonstrates a simple execution with a standard windows application. The 4th test case is blank, and you can create more such blank test cases to experiment with this framework. The &#8220;Execute Y&#8221; determines which test case in the Excel Spreadsheet are going to be executed. Any test case with &#8220;Execute N&#8221; flag will not execute.
</p>
<p class="centeralign"><strong>Executing Selected Tests through Excel Spreadsheet</strong><br />
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/AINmAOOWFn0&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/AINmAOOWFn0&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
</p>
<p class="centeralign"><strong>Version 9.2</strong><br />
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/y8Lju5o_nCU&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/y8Lju5o_nCU&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
</p>
<p class="centeralign"><strong>Version 10.0</strong><br />
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/NPOmpNZU27Y&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/NPOmpNZU27Y&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
</p>
<p class="centeralign alert"><span style='font-weight:bold; font-family:Calibri; font-size:1.2em;'><a class="downloadlink" href="http://relevantcodes.com/wp-content/plugins/download-monitor/download.php?id=1" title="Version0.1b downloaded 6171 times" >Download RelevantCodes[1]One (6171)</a></span></p>
<p class="centeralign"><img alt="RelevantCodes[1]One Automation Framework" src="http://relevantcodes.com/Articles/RelevantCodesOne/RelevantCodesOne1.png" title="RelevantCodes[1]One Automation Framework" class="aligncenter" width="303" height="33" /></p>
]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/relevantcodes1one-qtp-automation-framework/feed/</wfw:commentRss>
		<slash:comments>265</slash:comments>
		</item>
		<item>
		<title>QTP: Create Test Archives</title>
		<link>http://relevantcodes.com/qtp-create-test-archives/</link>
		<comments>http://relevantcodes.com/qtp-create-test-archives/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 02:27:37 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[Archive]]></category>
		<category><![CDATA[QTP Archive]]></category>
		<category><![CDATA[QTP Create Folders]]></category>
		<category><![CDATA[QTP Test Archive]]></category>
		<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=698</guid>
		<description><![CDATA[This post explains how to create test archives elegantly and quickly and store all run-time files with a date and time stamp.]]></description>
			<content:encoded><![CDATA[<p></p><div class="photo_right"><a href="http://www.flickr.com/photos/51035707527@N01/392558/" title="Archives"><img src="http://farm1.static.flickr.com/1/392558_9192862cb1_m.jpg" alt="Archives"  /></a><br /><small><a href="http://creativecommons.org/licenses/by-sa/2.0/" title="Attribution-ShareAlike License"><img src="http://relevantcodes.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" width="16" height="16" /></a> <a href="http://www.photodropper.com/photos/">photo</a> credit: <a href="http://www.flickr.com/photos/51035707527@N01/392558/" title="Yme Bosma">Yme Bosma</a></small></div>
<p>This topic discusses archiving files created during a test session. These can be text files, XMLs, custom snapshots etc. The important bit is to keep them in one centralized test folder. At my current project, one of the core requirements of any automated test-suite is capturing snapshots wherever required by the user.  Because each suite has several test scripts, we end up collecting hundreds of snapshots in each cycle. This is where archives come in &#8211; we create archives for each script, making it easier for the users to refer to the correct folder for the test and the time the test was run.</p>
<p>Assume you have a folder &#8216;Test XYZ&#8217; is where you are storing all the runtime files and snapshots: </p>
<p class="note">Z:\Automated Scripts\QuickTest Professional\Some Application\Image Captures\Test XYZ\</p>
<p>Using the technique below, you can create quick archives for each test session. I usually do this at the end of my tests, but this procedure can be called anywhere in the script.</p>
<p>Assume (again) you have the following file structure during a run-session:</p>
<div class="wp-caption aligncenter" style="width: 491px">
	<a href="http://relevantcodes.com/Articles/CreateArchive/Before.JPG"><img alt="Before Archiving" src="http://relevantcodes.com/Articles/CreateArchive/Before.JPG" title="Before Archiving" width="491" height="366" /></a>
	<p class="wp-caption-text">Before Archiving</p>
</div>
<p>After running the following line of code:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #008000;">'C:\Demo is the parent test folder
</span><span style="color: #0000A6;">Call</span> CreateTestArchive(<span style="color: #800000;">&quot;C:\Demo&quot;</span>)</pre></div></div>

<p>You will see a neat archive folder with the correct timestamp:</p>
<div class="wp-caption aligncenter" style="width: 511px">
	<a href="http://relevantcodes.com/Articles/CreateArchive/After.JPG"><img alt="After Archiving" src="http://relevantcodes.com/Articles/CreateArchive/After.JPG" title="After Archiving" width="511" height="317" /></a>
	<p class="wp-caption-text">After Archiving</p>
</div>
<p>The contents of the newly created folder (005) will now carry all the files that were in the parent folder:</p>
<div class="wp-caption aligncenter" style="width: 412px">
	<a href="http://relevantcodes.com/Articles/CreateArchive/Contents.JPG"><img alt="Archive Folder- Contents" src="http://relevantcodes.com/Articles/CreateArchive/Contents.JPG" title="Archive Folder- Contents" width="412" height="247" /></a>
	<p class="wp-caption-text">Archive Folder- Contents</p>
</div>
<p>How it&#8217;s done:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #008000;">'-------------------------------------------
</span><span style="color: #008000;">' Name: function CreateTestArchive
</span><span style="color: #008000;">' 
</span><span style="color: #008000;">' Purpose: Creates an archive folder with a date-time stamp
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' Input(s):
</span><span style="color: #008000;">'      sPath - Path of the parent folder where all the files are located
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' Output:
</span><span style="color: #008000;">'      &quot;&quot;
</span><span style="color: #008000;">'-------------------------------------------
</span><span style="color: #0000A6;">function</span> CreateTestArchive(sPath)
<span style="color: #008000;">'-------------------------------------------
</span>	<span style="color: #0000A6;">Dim</span> oFSO, oFolder, colSubFolder, sFolder
&nbsp;
	<span style="color: #008000;">'FileSystemObject
</span>	<span style="color: #0000A6;">Set</span> oFSO = <span style="color: #0000A6;">CreateObject</span>(<span style="color: #800000;">&quot;Scripting.FileSystemObject&quot;</span>)
	<span style="color: #008000;">'Bind to the supplied path
</span>	<span style="color: #0000A6;">Set</span> oFolder = oFSO.GetFolder(sPath)
	<span style="color: #008000;">'Retrieve collection of all subfolders
</span>	<span style="color: #0000A6;">Set</span> colSubFolder = oFolder.SubFolders
&nbsp;
	<span style="color: #008000;">'Create numbering for the new folder
</span>	<span style="color: #0000A6;">Select</span> <span style="color: #0000A6;">Case</span> Len(CStr(colSubFolder.Count))
		<span style="color: #0000A6;">Case</span> 1: sFolder = <span style="color: #800000;">&quot;00&quot;</span>
		<span style="color: #0000A6;">Case</span> 2: sFolder = <span style="color: #800000;">&quot;0&quot;</span>
		<span style="color: #0000A6;">Case</span> <span style="color: #0000A6;">Else</span>: sFolder = <span style="color: #800000;">&quot;&quot;</span>
	<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Select</span>
&nbsp;
	<span style="color: #008000;">'New folder name: this part can be modified so naming convention complies
</span>	<span style="color: #008000;">' 	to your guidelines.
</span>	<span style="color: #008000;">'Example: 009 - Archive Aug, 09 2009 3.05.02 PM
</span>	sFolder = sFolder &amp; colSubFolder.Count + 1 &amp; <span style="color: #800000;">&quot; - Archive &quot;</span> &amp; _
		  ParseDate(<span style="color: #0000A6;">Date</span>, <span style="color: #800000;">&quot;MMM, dd yyyy&quot;</span>) &amp; <span style="color: #800000;">&quot; &quot;</span> &amp; Replace(Time, <span style="color: #800000;">&quot;:&quot;</span>, <span style="color: #800000;">&quot;.&quot;</span>)
&nbsp;
      <span style="color: #008000;">'Move all files to the new folder
</span>	<span style="color: #0000A6;">If</span> oFolder.Files.Count &gt; 0 <span style="color: #0000A6;">Then</span>
		<span style="color: #0000A6;">If</span> <span style="color: #0000A6;">Not</span> InStr(Len(sPath), sPath, <span style="color: #800000;">&quot;\&quot;</span>) &lt;&gt; 0 <span style="color: #0000A6;">Then</span> sPath = sPath &amp; <span style="color: #800000;">&quot;\&quot;</span>
		<span style="color: #008000;">'Create the new folder
</span>		oFSO.CreateFolder sPath &amp; sFolder
		oFSO.MoveFile sPath &amp; <span style="color: #800000;">&quot;*.*&quot;</span>, sPath &amp; sFolder
	<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
&nbsp;
	<span style="color: #0000A6;">Set</span> oFSO = <span style="color: #0000A6;">Nothing</span>
	<span style="color: #0000A6;">Set</span> oFolder = <span style="color: #0000A6;">Nothing</span>
	<span style="color: #0000A6;">Set</span> colSubFolder = <span style="color: #0000A6;">Nothing</span>
<span style="color: #0000A6;">end</span> <span style="color: #0000A6;">function</span></pre></div></div>

<p>The snippet below parses the date to a desired format. I am using <i>MM, dd yyyy</i>, but you can modify this to support any format, or multiple formats:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #008000;">'-------------------------------------------
</span><span style="color: #008000;">' Name: function ParseDate
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' Purpose: Uses DOTNetFactory to parse date in desired format
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' Input(s):
</span><span style="color: #008000;">'      dtDate: Date that is to be parsed
</span><span style="color: #008000;">'      sFormat: Format in which the date is to be parsed
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' Output:
</span><span style="color: #008000;">'      Date
</span><span style="color: #008000;">'-------------------------------------------
</span><span style="color: #0000A6;">function</span> ParseDate(dtDate, sFormat)
<span style="color: #008000;">'-------------------------------------------
</span>	<span style="color: #0000A6;">Dim</span> oInstance
&nbsp;
	<span style="color: #0000A6;">Set</span> oInstance = DotNetFactory.CreateInstance(<span style="color: #800000;">&quot;System.DateTime&quot;</span>)
	ParseDate = oInstance.Parse(CStr(dtDate)).ToString(sFormat)
&nbsp;
	<span style="color: #0000A6;">Set</span> oInstance = <span style="color: #0000A6;">Nothing</span>
<span style="color: #0000A6;">end</span> <span style="color: #0000A6;">function</span></pre></div></div>

<p>I hope you will like this simple approach.</p>
<p>Thanks for visiting Relevant Codes :)</p>
<p class="alert">If you have any questions, please ask them in the comments section. If your query is confidential, please use the <a href="http://relevantcodes.com/contact/">Contact Form</a> to send me an e-mail instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/qtp-create-test-archives/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>QTP: Using VBScript Classes as Test Modules I</title>
		<link>http://relevantcodes.com/qtp-using-classes-as-test-modules-i/</link>
		<comments>http://relevantcodes.com/qtp-using-classes-as-test-modules-i/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 02:17:23 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[OOPs]]></category>
		<category><![CDATA[QTP Classes]]></category>
		<category><![CDATA[QTP Creating Classes]]></category>
		<category><![CDATA[QTP Modular Classes]]></category>
		<category><![CDATA[QTP Modules]]></category>
		<category><![CDATA[QTP OOPs]]></category>
		<category><![CDATA[QTP VBScript]]></category>
		<category><![CDATA[QTP VBScript Classes]]></category>
		<category><![CDATA[Test Module]]></category>
		<category><![CDATA[VBScript OOPs]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=255</guid>
		<description><![CDATA[Usage of modular classes helps in keeping all methods and properties for a module intact, and if done correctly, this can simply maintenance greatly.]]></description>
			<content:encoded><![CDATA[<p></p><div class="photo_right"><a title="OOP Principles in QTP" href="http://www.flickr.com/photos/35468148654@N01/2608389400/"><img src="http://farm4.static.flickr.com/3166/2608389400_abb588ced6_m.jpg" alt="OOP Principles in QTP" /></a><br />
<small><a title="Attribution License" href="http://creativecommons.org/licenses/by/2.0/"><img src="http://relevantcodes.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" width="16" height="16" /></a> <a href="http://www.photodropper.com/photos/">photo</a> credit: <a title="jared" href="http://www.flickr.com/photos/35468148654@N01/2608389400/">jared</a></small></div>
<p>Using Classes as Test Modules in QTP (Part I).</p>
<p>In this article, techniques of creating modular classes will be demonstrated. Usage of modular classes helps in keeping all methods and properties for a module intact, and if done correctly, this can simply maintenance greatly. This article serves as an introduction to Class usage in QTP. In the coming articles, we will discover advanced concepts in building highly stable and robust class modules.</p>
<p>Each modular class can contain one or more methods, which provide some functionality to a framework. Before adding the appropriate methods, we must create a modular class. In our demonstration, &#8216;clsLogin&#8217; has been created, which as the name suggests, is a Login class.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Class</span> clsLogin
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span></pre></div></div>

<p>The goal of the class we created above is to Login to the <a href="http://newtours.demoaut.com/">Mercury Tours</a> website. Let&#8217;s name our method &#8216;Login&#8217; and include the steps that the method will perform:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Class</span> clsLogin
&nbsp;
    <span style="color: #008000;">' Login
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> Login( sUserName, sPassword )
        <span style="color: #008000;">'Page title
</span>        <span style="color: #0000A6;">Dim</span> sTitle: sTitle = <span style="color: #800000;">&quot;Welcome: Mercury Tours&quot;</span>
&nbsp;
        Login = <span style="color: #0000A6;">False</span>
&nbsp;
        <span style="color: #0000A6;">With</span> Browser( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle ).Page( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle )
            <span style="color: #008000;">'Set the userName
</span>            .WebEdit( <span style="color: #800000;">&quot;name:=userName&quot;</span> ).<span style="color: #0000A6;">Set</span> sUserName
            <span style="color: #008000;">'Set the password
</span>            .WebEdit( <span style="color: #800000;">&quot;name:=password&quot;</span> ).SetSecure sPassword
            <span style="color: #008000;">'Click SignIn
</span>            .Image( <span style="color: #800000;">&quot;name:=login&quot;</span> ).Click
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
&nbsp;
        <span style="color: #008000;">'If the correct Page appears: Step Passed
</span>        <span style="color: #0000A6;">If</span> Browser( <span style="color: #800000;">&quot;title:=Find a Flight: Mercury Tours:&quot;</span> ).Exist( 5 ) <span style="color: #0000A6;">Then</span>
            Login = <span style="color: #0000A6;">True</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span></pre></div></div>

<p>Let&#8217;s assume we have to check properties of the page banner, which is a part of our test-case. In reality, there may be multiple objects for which properties are being verified and they can be included within the same method. For our example though, we will verify property of a single object: page banner.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Class</span> clsLogin
&nbsp;
    <span style="color: #008000;">' CheckObjectProperties
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> CheckObjectProperties
        <span style="color: #008000;">'Page title
</span>        <span style="color: #0000A6;">Dim</span> sTitle: sTitle = <span style="color: #800000;">&quot;Welcome: Mercury Tours&quot;</span>
&nbsp;
        CheckObjectProperties = <span style="color: #0000A6;">False</span>
&nbsp;
        <span style="color: #0000A6;">With</span> Browser(<span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle).Page(<span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle)
            <span style="color: #008000;">'Check Page Banner
</span>            <span style="color: #0000A6;">If</span> .Image(<span style="color: #800000;">&quot;file name:=banner2.gif&quot;</span>).GetROProperty(<span style="color: #800000;">&quot;width&quot;</span>) = 488 <span style="color: #0000A6;">Then</span>
                <span style="color: #008000;">'If the width equals the expected width, step passed
</span>                CheckObjectProperties = <span style="color: #0000A6;">True</span>
                Reporter.ReportEvent micPass, <span style="color: #800000;">&quot;Banner&quot;</span>, <span style="color: #800000;">&quot;Size Correct.&quot;</span>
            <span style="color: #0000A6;">Else</span>
                Reporter.ReportEvent micFail, <span style="color: #800000;">&quot;Banner&quot;</span>, <span style="color: #800000;">&quot;Size Incorrect.&quot;</span>
            <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
    <span style="color: #008000;">' Login
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> Login( sUserName, sPassword )
        <span style="color: #0000A6;">Dim</span> sTitle: sTitle = <span style="color: #800000;">&quot;Welcome: Mercury Tours&quot;</span>
&nbsp;
        Login = <span style="color: #0000A6;">False</span>
&nbsp;
        <span style="color: #0000A6;">With</span> Browser( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle ).Page( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle )
            <span style="color: #008000;">'Set the userName
</span>            .WebEdit( <span style="color: #800000;">&quot;name:=userName&quot;</span> ).<span style="color: #0000A6;">Set</span> sUserName
            <span style="color: #008000;">'Set the password
</span>            .WebEdit( <span style="color: #800000;">&quot;name:=password&quot;</span> ).SetSecure sPassword
            <span style="color: #008000;">'Click SignIn
</span>            .Image( <span style="color: #800000;">&quot;name:=login&quot;</span> ).Click
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
&nbsp;
        Browser( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle ).Sync
&nbsp;
        <span style="color: #008000;">'If the correct Page appears: Step Passed.
</span>        <span style="color: #0000A6;">If</span> Browser( <span style="color: #800000;">&quot;title:=Find a Flight: Mercury Tours:&quot;</span> ).Exist( 5 ) <span style="color: #0000A6;">Then</span> 
            Login = <span style="color: #0000A6;">True</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span></pre></div></div>

<p>The 2 methods discussed above are quite common: we generally perform actions on objects and create checks to replace (manual) visual verifications. Now, before we can really perform the above 2 checks, we may also need to check whether we&#8217;re on the correct page. Let&#8217;s add another method for that:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Class</span> clsLogin
&nbsp;
    <span style="color: #008000;">' IsPageFound: Check if we're on the correct page
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> IsPageFound
        <span style="color: #008000;">'Page title
</span>        <span style="color: #0000A6;">Dim</span> sTitle: sTitle = <span style="color: #800000;">&quot;Welcome: Mercury Tours&quot;</span>
&nbsp;
        IsPageFound = <span style="color: #0000A6;">False</span>
&nbsp;
        <span style="color: #0000A6;">With</span> Browser( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle ).Page( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle )
            <span style="color: #0000A6;">If</span> .Exist( 10 ) <span style="color: #0000A6;">Then</span>
                IsPageFound = <span style="color: #0000A6;">True</span>
            <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
    <span style="color: #008000;">' CheckObjectProperties
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> CheckObjectProperties
        <span style="color: #0000A6;">Dim</span> sTitle: sTitle = <span style="color: #800000;">&quot;Welcome: Mercury Tours&quot;</span>
&nbsp;
        CheckObjectProperties = <span style="color: #0000A6;">False</span>
&nbsp;
        <span style="color: #0000A6;">With</span> Browser( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle ).Page( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle )
            <span style="color: #008000;">'Check Page Banner
</span>            <span style="color: #0000A6;">If</span> .Image(<span style="color: #800000;">&quot;file name:=banner2.gif&quot;</span>).GetROProperty(<span style="color: #800000;">&quot;width&quot;</span>) = 488 <span style="color: #0000A6;">Then</span>
                CheckObjectProperties = <span style="color: #0000A6;">True</span>
                Reporter.ReportEvent micPass, <span style="color: #800000;">&quot;Banner&quot;</span>, <span style="color: #800000;">&quot;Size Correct.&quot;</span>
            <span style="color: #0000A6;">Else</span>
                Reporter.ReportEvent micFail, <span style="color: #800000;">&quot;Banner&quot;</span>, <span style="color: #800000;">&quot;Size Incorrect.&quot;</span>
            <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
    <span style="color: #008000;">' Login
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> Login( sUserName, sPassword )
        <span style="color: #0000A6;">Dim</span> sTitle: sTitle = <span style="color: #800000;">&quot;Welcome: Mercury Tours&quot;</span>
&nbsp;
        Login = <span style="color: #0000A6;">False</span>
&nbsp;
        <span style="color: #0000A6;">With</span> Browser( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle ).Page( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle )
            <span style="color: #008000;">'Set the userName
</span>            .WebEdit( <span style="color: #800000;">&quot;name:=userName&quot;</span> ).<span style="color: #0000A6;">Set</span> sUserName
            <span style="color: #008000;">'Set the password
</span>            .WebEdit( <span style="color: #800000;">&quot;name:=password&quot;</span> ).SetSecure sPassword
            <span style="color: #008000;">'Click SignIn
</span>            .Image( <span style="color: #800000;">&quot;name:=login&quot;</span> ).Click
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
&nbsp;
        <span style="color: #008000;">'If the correct Page appears: Step Passed.
</span>        <span style="color: #0000A6;">If</span> Browser( <span style="color: #800000;">&quot;title:=Find a Flight: Mercury Tours:&quot;</span> ).Exist( 5 ) <span style="color: #0000A6;">Then</span> 
            Login = <span style="color: #0000A6;">True</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span></pre></div></div>

<p>If we observe the code above, the variable &#8220;sTitle&#8221; is present in each method. This is quite redundant. We can simply replace this with a readOnly property:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Class</span> clsLogin
&nbsp;
    <span style="color: #008000;">' Property sTitle
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Property</span> <span style="color: #0000A6;">Get</span> sTitle <span style="color: #008000;">'ReadOnly Property
</span>        sTitle = <span style="color: #800000;">&quot;Welcome: Mercury Tours&quot;</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Property</span>
&nbsp;
    <span style="color: #008000;">' IsPageFound
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> IsPageFound
        IsPageFound = <span style="color: #0000A6;">False</span>
&nbsp;
        <span style="color: #0000A6;">With</span> Browser( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle ).Page( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle )
            <span style="color: #0000A6;">If</span> .Exist( 10 ) <span style="color: #0000A6;">Then</span>
                IsPageFound = <span style="color: #0000A6;">True</span>
            <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
    <span style="color: #008000;">' CheckObjectProperties
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> CheckObjectProperties
        CheckObjectProperties = <span style="color: #0000A6;">False</span>
&nbsp;
        <span style="color: #0000A6;">With</span> Browser( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle ).Page( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle )
&nbsp;
            <span style="color: #008000;">'Check Page Banner
</span>            <span style="color: #0000A6;">If</span> .Image(<span style="color: #800000;">&quot;file name:=banner2.gif&quot;</span>).GetROProperty(<span style="color: #800000;">&quot;width&quot;</span>) = 488 <span style="color: #0000A6;">Then</span>
                CheckObjectProperties = <span style="color: #0000A6;">True</span>
                Reporter.ReportEvent micPass, <span style="color: #800000;">&quot;Banner&quot;</span>, <span style="color: #800000;">&quot;Size Correct.&quot;</span>
            <span style="color: #0000A6;">Else</span>
                Reporter.ReportEvent micFail, <span style="color: #800000;">&quot;Banner&quot;</span>, <span style="color: #800000;">&quot;Size Incorrect.&quot;</span>
            <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
&nbsp;
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
    <span style="color: #008000;">' Login
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> Login( sUserName, sPassword )
        Login = <span style="color: #0000A6;">False</span>
&nbsp;
        <span style="color: #0000A6;">With</span> Browser( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle ).Page( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle )
            <span style="color: #008000;">'Set the userName
</span>            .WebEdit( <span style="color: #800000;">&quot;name:=userName&quot;</span> ).<span style="color: #0000A6;">Set</span> sUserName
            <span style="color: #008000;">'Set the password
</span>            .WebEdit( <span style="color: #800000;">&quot;name:=password&quot;</span> ).SetSecure sPassword
            <span style="color: #008000;">'Click SignIn
</span>            .Image( <span style="color: #800000;">&quot;name:=login&quot;</span> ).Click
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
&nbsp;
        <span style="color: #008000;">'If the correct Page appears: Step Passed.
</span>        <span style="color: #0000A6;">If</span> Browser( <span style="color: #800000;">&quot;title:=Find a Flight: Mercury Tours:&quot;</span> ).Exist( 5 ) <span style="color: #0000A6;">Then</span> 
            Login = <span style="color: #0000A6;">True</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span></pre></div></div>

<p>Even though some methods of a class are crucial, they may only perform background operations. In our demonstration, &#8216;IsPageFound&#8217; and &#8216;CheckObjectProperties&#8217; are such methods. Both methods verify objects, and in the automation world, they can be very reasonable prerequisites to the main action, which is to Login to the application. In the demonstration below, &#8216;IsPageFound&#8217; and &#8216;CheckObjectProperties&#8217; have been marked as private. Both methods are included as a prerequisite to setting the userName and password for logging to the website. To reiterate, both &#8216;IsPageFound&#8217; and &#8216;CheckObjectProperties&#8217; can be made private methods of the class:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Class</span> clsLogin
&nbsp;
    <span style="color: #008000;">' IsPageFound: Private Function
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Function</span> IsPageFound
        IsPageFound = <span style="color: #0000A6;">False</span>
&nbsp;
        <span style="color: #0000A6;">With</span> Browser( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle ).Page( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle )
            <span style="color: #0000A6;">If</span> .Exist( 10 ) <span style="color: #0000A6;">Then</span>
                IsPageFound = <span style="color: #0000A6;">True</span>
            <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
    <span style="color: #008000;">' CheckObjectProperties: Private Function
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Function</span> CheckObjectProperties
        CheckObjectProperties = <span style="color: #0000A6;">False</span>
&nbsp;
        <span style="color: #0000A6;">With</span> Browser( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle ).Page( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle )
&nbsp;
            <span style="color: #008000;">'Check Page Banner
</span>            <span style="color: #0000A6;">If</span> .Image( <span style="color: #800000;">&quot;file name:=banner2.gif&quot;</span> ).GetROProperty( <span style="color: #800000;">&quot;width&quot;</span> ) = 488 <span style="color: #0000A6;">Then</span>
                CheckObjectProperties = <span style="color: #0000A6;">True</span>
                Reporter.ReportEvent micPass, <span style="color: #800000;">&quot;Banner&quot;</span>, <span style="color: #800000;">&quot;Size Correct.&quot;</span>
            <span style="color: #0000A6;">Else</span>
                Reporter.ReportEvent micFail, <span style="color: #800000;">&quot;Banner&quot;</span>, <span style="color: #800000;">&quot;Size Incorrect.&quot;</span>
            <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
&nbsp;
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
    <span style="color: #008000;">' Login: Public Function
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> Login( sUserName, sPassword )
        Login = <span style="color: #0000A6;">False</span>
&nbsp;
        <span style="color: #0000A6;">If</span> IsPageFound <span style="color: #0000A6;">Then</span>
            <span style="color: #0000A6;">If</span> CheckObjectProperties <span style="color: #0000A6;">Then</span>
                <span style="color: #0000A6;">With</span> Browser( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle ).Page( <span style="color: #800000;">&quot;title:=&quot;</span> &amp; sTitle )
                    <span style="color: #008000;">'Set the userName
</span>                    .WebEdit( <span style="color: #800000;">&quot;name:=userName&quot;</span> ).<span style="color: #0000A6;">Set</span> sUserName
                    <span style="color: #008000;">'Set the password
</span>                    .WebEdit( <span style="color: #800000;">&quot;name:=password&quot;</span> ).SetSecure sPassword
                    <span style="color: #008000;">'Click SignIn
</span>                    .Image( <span style="color: #800000;">&quot;name:=login&quot;</span> ).Click
                <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
            <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
&nbsp;
        <span style="color: #008000;">'If the Page with the title 'Find a Flight: Mercury Tours:' appears: Step Passed.
</span>        <span style="color: #0000A6;">If</span> Browser( <span style="color: #800000;">&quot;title:=Find a Flight: Mercury Tours:&quot;</span> ).Exist( 5 ) <span style="color: #0000A6;">Then</span> 
            Login = <span style="color: #0000A6;">True</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
    <span style="color: #008000;">' Property sTitle: Private Property
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Property</span> <span style="color: #0000A6;">Get</span> sTitle
        sTitle = <span style="color: #800000;">&quot;Welcome: Mercury Tours&quot;</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Property</span>
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span></pre></div></div>

<p>If you notice the code above, you will see that both private methods have become a part of the Login method. They&#8217;re both prerequisites to logging in to the application. Only once both are True, will the code perform the code-action of the class. We can implement the class above using the following snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> Login( sUserName, sPassword )
    <span style="color: #0000A6;">Dim</span> oAction: <span style="color: #0000A6;">Set</span> oAction = <span style="color: #0000A6;">New</span> clsLogin
    Login = oAction.Login( sUserName, sPassword )
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span></pre></div></div>

<p>If you would like to keep &#8216;IsPageFound&#8217; and &#8216;CheckObjectProperties&#8217; as public methods of the class, then you can even do this:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> Login( sUserName, sPassword )
    <span style="color: #0000A6;">Dim</span> oAction: <span style="color: #0000A6;">Set</span> oAction = <span style="color: #0000A6;">New</span> clsLogin  
&nbsp;
    Login = <span style="color: #0000A6;">False</span>
&nbsp;
    <span style="color: #0000A6;">With</span> oAction
        <span style="color: #0000A6;">If</span> .IsPageFound <span style="color: #0000A6;">Then</span>
            <span style="color: #0000A6;">If</span> .CheckObjectProperties <span style="color: #0000A6;">Then</span>
                Login = .Login( sUserName, sPassword )
            <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span></pre></div></div>

<p>It is possible for a single function to do everything, but don&#8217;t you think its much easier to understand what each method of the class does with little effort? &#8216;IsPageFound&#8217; simply checks if we&#8217;re on the correct page, &#8216;CheckObjectProperties&#8217; checks if the object was loaded correctly, and ultimately, &#8216;Login&#8217; performs to core-action by logging the user to the application. Moreover, all methods are grouped together in a class.</p>
<p>In practice, the entire test suite can be created on this principle with each module representing a core-functionality of the overall test. There are several ways to bind these modules together, and we must find an elegant solution for that as well. This is because, because of some major changes, we may need to completely remove a module, or even add one. Therefore, if done correctly, even such a big change can be implemented without much trouble. We will discuss such a technique in the next article in this series, which will be an advanced implementation of class usage in QTP.</p>
<p>I hope you guys find this article useful.</p>
<p>
<span class="postheading">References</span></p>
<ol>
<li><a href="http://www.advancedqtp.com/knowledge-base/articles/qtp-tricks4/descriptive-programming/implementing-a-gui-layer-with-classes/">Implementing a GUI Layer of Classes</a> by Meir-Bar Tal (AdvancedQTP, Solmar)</li>
</ol>
<p class="alert">If you have any questions, please ask them in the comments section. If your query is confidential, please use the <a href="http://relevantcodes.com/contact/">Contact Form</a> to send me an e-mail instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/qtp-using-classes-as-test-modules-i/feed/</wfw:commentRss>
		<slash:comments>51</slash:comments>
		</item>
		<item>
		<title>CDO: Send Email from Yahoo, Hotmail, Live, AOL or GMail</title>
		<link>http://relevantcodes.com/cdo-send-email-from-yahoo-hotmail-live-aol-or-gmail/</link>
		<comments>http://relevantcodes.com/cdo-send-email-from-yahoo-hotmail-live-aol-or-gmail/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 21:48:09 +0000</pubDate>
		<dc:creator>Anshoo Arora</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[CDO]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[Email QTP]]></category>
		<category><![CDATA[QTP AOL]]></category>
		<category><![CDATA[QTP Check Email]]></category>
		<category><![CDATA[QTP Email]]></category>
		<category><![CDATA[QTP GMail]]></category>
		<category><![CDATA[QTP Hotmail]]></category>
		<category><![CDATA[QTP Live]]></category>
		<category><![CDATA[QTP Yahoo]]></category>
		<category><![CDATA[Send Email]]></category>

		<guid isPermaLink="false">http://relevantcodes.com/?p=97</guid>
		<description><![CDATA[This article discusses 2 ways of sending e-mail from your Yahoo, Hotmail, Gmail, Live or AOL e-mail accounts using the ‘clsSendMail’ class:
]]></description>
			<content:encoded><![CDATA[<p></p><div class="photo_right"><a href="http://www.flickr.com/photos/9395368@N02/3550395288/" title="" ><img src="http://farm4.static.flickr.com/3052/3550395288_8b08487cbe_m.jpg" alt=""  /></a><br /><small><a href="http://creativecommons.org/licenses/by-sa/2.0/" title="Attribution-ShareAlike License" ><img src="http://relevantcodes.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" width="16" height="16" /></a> <a href="http://www.photodropper.com/photos/" >photo</a> credit: <a href="http://www.flickr.com/photos/9395368@N02/3550395288/" title="Philippe sergent">Philippe sergent</a></small></div>
<p>This article discusses 2 ways of sending e-mail from your Yahoo, Hotmail, Gmail, Live or AOL e-mail accounts using the &#8216;clsSendMail&#8217; class:</p>
<ol>
<li>Calling the Method with the Body Message</li>
<li>Retrieve body-messages from text files</li>
</ol>
<p>This can be quite handy during regression cycles &#8211; you can use this code to send yourself an e-mail whenever a step in the test process fails. You can even keep a count of the number of times this procedure executes in your script and even <a href="http://abouttesting.blogspot.com/2009/03/test-framework-stop-if-too-many-errors.html">stop your test if too many steps fail</a>.</p>
<p><span class="postheading">Calling the Method with the Body Message</span><br />
The code snippets below shows the syntax to send an HTML and plain text e-mails:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #008000;">'TEXT
</span>
<span style="color: #008000;">'Call Email( Sender's_ID, Sender's_Password, Send_To, CC_To, Subject, Message )
</span>Email <span style="color: #800000;">&quot;XYZ@AOL.com&quot;</span>, <span style="color: #800000;">&quot;myPassword&quot;</span>, <span style="color: #800000;">&quot;ABC@Gmail.com&quot;</span>, _
        <span style="color: #800000;">&quot;&quot;</span>, <span style="color: #800000;">&quot;Subject&quot;</span>, <span style="color: #800000;">&quot;Hello, this is a test mail.&quot;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #008000;">'HTML
</span>
<span style="color: #008000;">'Call Email( Sender's_ID, Sender's_Password, Send_To, CC_To, Subject, Message )
</span>Email <span style="color: #800000;">&quot;XYZ@AOL.com&quot;</span>, <span style="color: #800000;">&quot;myPassword&quot;</span>, <span style="color: #800000;">&quot;ABC@Gmail.com&quot;</span>, <span style="color: #800000;">&quot;&quot;</span>, _
        <span style="color: #800000;">&quot;Subject&quot;</span>, <span style="color: #800000;">&quot;&lt;h1&gt;Hello&lt;/h1&gt;&lt;p&gt;Test Mail&lt;/p&gt;&quot;</span></pre></div></div>

<p>
<span class="postheading">Retrieve body-messages from text files</span><br />
You can create a text file with the body message, and use the file path in the method to send the e-mail. Below is an example of an HTML body inserted into a text file:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #008000;">'Call Email( Sender's_ID, Sender's_Password, Send_To, CC_To, Subject, Message )
</span>Email <span style="color: #800000;">&quot;XYZ@AOL.com&quot;</span>, <span style="color: #800000;">&quot;myPassword&quot;</span>, <span style="color: #800000;">&quot;ABC@Gmail.com&quot;</span>, <span style="color: #800000;">&quot;&quot;</span>, _
        <span style="color: #800000;">&quot;Subject&quot;</span>, <span style="color: #800000;">&quot;C:\MyTestMail.txt&quot;</span></pre></div></div>

<div class="wp-caption aligncenter" style="width: 533px">
	<a href="http://www.relevantcodes.com/Articles/CDO/MyTestMail.jpg"><img alt="Sample Text File" src="http://www.relevantcodes.com/Articles/CDO/MyTestMail.jpg" title="Sample Text File" width="533" height="120" /></a>
	<p class="wp-caption-text">Sample Text File</p>
</div>
<p>Similarly, mails can be sent to multiple recipients using &#8220;;&#8221; between Recipient&#8217;s emails. Example:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;">Email <span style="color: #800000;">&quot;Me@MeEmail.Com&quot;</span>, <span style="color: #800000;">&quot;mePassword&quot;</span>, <span style="color: #800000;">&quot;You@YouEmail.Com;Them@TheirEmail.Com&quot;</span>, _
    <span style="color: #800000;">&quot;CC@YouCC.Com;CC@MeCC.Com&quot;</span>, <span style="color: #800000;">&quot;Test Subject&quot;</span>, <span style="color: #800000;">&quot;Test Message&quot;</span></pre></div></div>

<p>You can find the download links to this class towards the end of this post.</p>
<p><br/><b>How its done:</b></p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:consolas, monospace;"><span style="color: #008000;">'Public Class clsSendMail
</span><span style="color: #0000A6;">Class</span> clsSendMail
&nbsp;
    <span style="color: #0000A6;">Private</span> oMessage    <span style="color: #008000;">'CDO.Message Object
</span>    <span style="color: #0000A6;">Private</span> strFrom     <span style="color: #008000;">'Sender's Email ID: XX@YY.COM
</span>    <span style="color: #0000A6;">Public</span> Body         <span style="color: #008000;">'Body Text from Text File
</span>
    <span style="color: #008000;">'—————————————————————————————————————————————  
</span>    <span style="color: #008000;">' Name: Sub Email (Public)
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Purpose: Send Email Using CDO
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Parameters:
</span>    <span style="color: #008000;">'    sEmailID: Sender's Mail ID String
</span>    <span style="color: #008000;">'    sPassword: Sender's Password String
</span>    <span style="color: #008000;">'    sTo: Recipient's Mail ID String (Primary)
</span>    <span style="color: #008000;">'    sCC: Recipient's Mail ID String (CC)
</span>    <span style="color: #008000;">'    sSubject: Subject String
</span>    <span style="color: #008000;">'    sBody: Body Message String
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Return: -
</span>    <span style="color: #008000;">'————————————————————————————————————————————— 
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Sub</span> Send( sEMailID, sPassword, sTo, sCC, sSubject, sBody )
    <span style="color: #008000;">'————————————————————————————————————————————— 
</span>        <span style="color: #0000A6;">Dim</span> oRegExp     <span style="color: #008000;">'RegEx Object
</span>        <span style="color: #0000A6;">Dim</span> sDetails    <span style="color: #008000;">'Report Details
</span>        <span style="color: #0000A6;">Dim</span> intStatus   <span style="color: #008000;">'Report Status
</span>        <span style="color: #0000A6;">Dim</span> sStepName   <span style="color: #008000;">'Report Step
</span>        
        <span style="color: #008000;">'Sender ID has Class scope
</span>        Me.From = sEmailID
        <span style="color: #008000;">'Message Body
</span>        <span style="color: #0000A6;">If</span> sBody &lt;&gt; <span style="color: #800000;">&quot;&quot;</span> <span style="color: #0000A6;">Then</span> Me.Body = sBody
&nbsp;
        intStatus = micPass
        sStepName = <span style="color: #800000;">&quot; Sent&quot;</span>
&nbsp;
        <span style="color: #0000A6;">Set</span> oRegExp = <span style="color: #0000A6;">New</span> RegExp
        oRegExp.Global = <span style="color: #0000A6;">True</span>
        oRegExp.Pattern = <span style="color: #800000;">&quot;&lt;\w&gt;|&lt;\w\w&gt;|&lt;\w\d&gt;&quot;</span>
        <span style="color: #0000A6;">Set</span> oMatches = oRegExp.Execute( Me.Body )
&nbsp;
        <span style="color: #008000;">'Build Message
</span>        <span style="color: #0000A6;">With</span> oMessage
            .Subject = sSubject
            .From = sEmailID
            .<span style="color: #0000A6;">To</span> = sTo
            .CC = sCC
            <span style="color: #008000;">'BCC Property can be added as well:
</span>            <span style="color: #008000;">'.BCC = sBCC
</span>            <span style="color: #008000;">'If HTML Tags found, use .HTMLBody
</span>            <span style="color: #0000A6;">If</span> oMatches.Count &gt; 0 <span style="color: #0000A6;">Then</span>
                .HTMLBody = Me.Body
            <span style="color: #0000A6;">Else</span>
                .TextBody = Me.Body
            <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
&nbsp;
        <span style="color: #0000A6;">Set</span> oMatches = <span style="color: #0000A6;">Nothing</span>
        <span style="color: #0000A6;">Set</span> oRegExp = <span style="color: #0000A6;">Nothing</span>
&nbsp;
        <span style="color: #0000A6;">With</span> oMessage.Configuration.Fields
            <span style="color: #008000;">'Sender's Mail ID
</span>            .Item(<span style="color: #800000;">&quot;http://schemas.microsoft.com/cdo/configuration/&quot;</span> &amp;_
            <span style="color: #800000;">&quot;sendusername&quot;</span>) = sEmailID
            <span style="color: #008000;">'Sender's Password
</span>            .Item(<span style="color: #800000;">&quot;http://schemas.microsoft.com/cdo/configuration/&quot;</span> &amp;_
            <span style="color: #800000;">&quot;sendpassword&quot;</span>) = sPassword
            <span style="color: #008000;">'Name/IP of SMTP Server
</span>            .Item(<span style="color: #800000;">&quot;http://schemas.microsoft.com/cdo/configuration/&quot;</span> &amp;_
            <span style="color: #800000;">&quot;smtpserver&quot;</span>) = cdoSMTPServer
            <span style="color: #008000;">'Server Port
</span>            .Item(<span style="color: #800000;">&quot;http://schemas.microsoft.com/cdo/configuration/&quot;</span> &amp;_
            <span style="color: #800000;">&quot;smtpserverport&quot;</span>) = cdoOutgoingMailSMTP
            <span style="color: #008000;">'Send Using: (1) Local SMTP Pickup Service (2) Use SMTP Over Network
</span>            .Item(<span style="color: #800000;">&quot;http://schemas.microsoft.com/cdo/configuration/&quot;</span> &amp;_
            <span style="color: #800000;">&quot;sendusing&quot;</span>) = cdoSendUsing
            <span style="color: #008000;">'Authentication Used: (1) None (2) Basic (3) NTLM
</span>            .Item(<span style="color: #800000;">&quot;http://schemas.microsoft.com/cdo/configuration/&quot;</span> &amp;_
            <span style="color: #800000;">&quot;smtpauthenticate&quot;</span>) = cdoAuthenticationType
            <span style="color: #008000;">'SMTP Server Requires SSL/STARTTLS: Boolean
</span>            .Item(<span style="color: #800000;">&quot;http://schemas.microsoft.com/cdo/configuration/&quot;</span> &amp;_
            <span style="color: #800000;">&quot;smtpusessl&quot;</span>) = cdoUseSSL
            <span style="color: #008000;">'Maximum Time in Seconds CDO will try to Establish Connection
</span>            .Item(<span style="color: #800000;">&quot;http://schemas.microsoft.com/cdo/configuration/&quot;</span> &amp;_
            <span style="color: #800000;">&quot;smtpconnectiontimeout&quot;</span>) = cdoTimeout
            <span style="color: #008000;">'Update Configuration Entries
</span>            .Update
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">With</span>
&nbsp;
        <span style="color: #008000;">'Report Details
</span>        sDetails = <span style="color: #800000;">&quot;SMTP Server: &quot;</span> &amp; cdoSMTPServer &amp; vbLf
        sDetails = sDetails &amp; <span style="color: #800000;">&quot;Sender: &quot;</span> &amp; sEMailID &amp; vbLf
        sDetails = sDetails &amp; <span style="color: #800000;">&quot;Recipient: &quot;</span> &amp; sTo &amp; vbLf
        sDetails = sDetails &amp; <span style="color: #800000;">&quot;Server Port: &quot;</span> &amp; cdoOutgoingMailSMTP &amp; vbLf
        sDetails = sDetails &amp; <span style="color: #800000;">&quot;SSL Used: &quot;</span> &amp; cdoUseSSL &amp; vbLf
        sDetails = sDetails &amp; <span style="color: #800000;">&quot;Authentication Type: &quot;</span> &amp; cdoAuthenticationType &amp; vbLf
        sDetails = sDetails &amp; <span style="color: #800000;">&quot;SMTP Service Type: &quot;</span> &amp; cdoSendUsing &amp; vbLf &amp; vbLf
        sDetails = sDetails &amp; <span style="color: #800000;">&quot;Subject: &quot;</span> &amp; sSubject &amp; vbLf &amp; vbLf
        sDetails = sDetails &amp; <span style="color: #800000;">&quot;Body: &quot;</span> &amp; sBody
&nbsp;
        <span style="color: #0000A6;">On</span> <span style="color: #0000A6;">Error</span> <span style="color: #0000A6;">Resume</span> <span style="color: #0000A6;">Next</span>
            <span style="color: #008000;">'Send Message
</span>            oMessage.Send
            <span style="color: #0000A6;">If</span> Err.Number &lt;&gt; 0 <span style="color: #0000A6;">Then</span>
                intStatus = micWarning
                sStepName = <span style="color: #800000;">&quot; Not Sent&quot;</span>
                sDetails = sDetails &amp; vbLf &amp; <span style="color: #800000;">&quot;Error Description: &quot;</span> &amp; Err.Description
            <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
        <span style="color: #0000A6;">On</span> <span style="color: #0000A6;">Error</span> <span style="color: #0000A6;">Goto</span> 0
&nbsp;
        <span style="color: #008000;">'If you're not using QTP, please disable the statement below:
</span>        Reporter.ReportEvent intStatus, <span style="color: #800000;">&quot;EMail&quot;</span> &amp; sStepName, sDetails
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
    <span style="color: #008000;">'—————————————————————————————————————————————  
</span>    <span style="color: #008000;">' Name: Sub LoadBodyMessage (Public)
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Purpose: Loads BodyText from a Text File
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Parameters:
</span>    <span style="color: #008000;">'    sCompleteFilePath: Complete Path to the Text File (Eg: &quot;C:\MyDocs\MyMail.txt&quot;)
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Return: -
</span>    <span style="color: #008000;">'—————————————————————————————————————————————  
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Sub</span> LoadBodyMessage( sCompleteFilePath )
    <span style="color: #008000;">'————————————————————————————————————————————— 
</span>        CONST ForReading = 1 
        <span style="color: #0000A6;">Dim</span> oFSO, oFile
&nbsp;
        <span style="color: #0000A6;">Set</span> oFSO = <span style="color: #0000A6;">CreateObject</span>( <span style="color: #800000;">&quot;Scripting.FileSystemObject&quot;</span> )
        <span style="color: #0000A6;">Set</span> oFile = oFSO.OpenTextFile( sCompleteFilePath, ForReading )
        Me.Body = oFile.ReadAll
        oFile.<span style="color: #0000A6;">Close</span>: <span style="color: #0000A6;">Set</span> oFile = <span style="color: #0000A6;">Nothing</span>
&nbsp;
        <span style="color: #0000A6;">Set</span> oFSO = <span style="color: #0000A6;">Nothing</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
    <span style="color: #008000;">'—————————————————————————————————————————————  
</span>    <span style="color: #008000;">' Name: Class_Initialize (Private)
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Purpose: Binds to the CDO Object
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Parameters: -
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Return: -
</span>    <span style="color: #008000;">'—————————————————————————————————————————————  
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Sub</span> Class_Initialize
    <span style="color: #008000;">'—————————————————————————————————————————————  
</span>        <span style="color: #0000A6;">Set</span> oMessage = <span style="color: #0000A6;">CreateObject</span>( <span style="color: #800000;">&quot;CDO.Message&quot;</span> )
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
    <span style="color: #008000;">'—————————————————————————————————————————————  
</span>    <span style="color: #008000;">' Name: Class_Initialize (Private)
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Purpose: Release the CDO Object
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Parameters: -
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Return: -
</span>    <span style="color: #008000;">'—————————————————————————————————————————————     
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Sub</span> Class_Terminate
    <span style="color: #008000;">'————————————————————————————————————————————— 
</span>        <span style="color: #0000A6;">Set</span> oMessage = <span style="color: #0000A6;">Nothing</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Sub</span>
&nbsp;
    <span style="color: #008000;">'—————————————————————————————————————
</span>    <span style="color: #008000;">' Name: Property cdoSendUsing (Private)
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Purpose: Readonly property configuration for SMTP Service
</span>    <span style="color: #008000;">'—————————————————————————————————————
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Property</span> <span style="color: #0000A6;">Get</span> cdoSendUsing  <span style="color: #008000;">'As Integer
</span>    <span style="color: #008000;">'—————————————————————————————————————    
</span>        cdoSendUsing = 2    <span style="color: #008000;">'Use SMTP Over The Network
</span>        <span style="color: #008000;">'cdoSendUsing = 1    'Use Local SMTP Service Pickup Directory
</span>    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Property</span>
&nbsp;
    <span style="color: #008000;">'—————————————————————————————————————
</span>    <span style="color: #008000;">' Name: Property cdoTimeout (Private)
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Purpose: Maximum time in seconds CDO will try to establish a connection
</span>    <span style="color: #008000;">'—————————————————————————————————————
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Property</span> <span style="color: #0000A6;">Get</span> cdoTimeout  <span style="color: #008000;">'As Integer
</span>    <span style="color: #008000;">'—————————————————————————————————————    
</span>        <span style="color: #008000;">'cdoTimeout = 15    'Seconds
</span>        cdoTimeout = 45    <span style="color: #008000;">'Seconds
</span>        <span style="color: #008000;">'cdoTimeout = 75    'Seconds
</span>    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Property</span>
&nbsp;
    <span style="color: #008000;">'—————————————————————————————————————
</span>    <span style="color: #008000;">' Name: Property cdoAuthenticationType (Private)
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Purpose: Type of Authentication to be used
</span>    <span style="color: #008000;">'—————————————————————————————————————    
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Property</span> <span style="color: #0000A6;">Get</span> cdoAuthenticationType  <span style="color: #008000;">'As Integer
</span>    <span style="color: #008000;">'—————————————————————————————————————    
</span>        <span style="color: #008000;">'cdoAuthenticationType = 0    'No Authentication
</span>        cdoAuthenticationType = 1    <span style="color: #008000;">'Basic Authentication
</span>        <span style="color: #008000;">'cdoAuthenticationType = 2    'NTML Authentication
</span>    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Property</span>
&nbsp;
    <span style="color: #008000;">'—————————————————————————————————————
</span>    <span style="color: #008000;">' Name: Property cdoOutgoingMailSMTP (Private)
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Purpose: Server Port
</span>    <span style="color: #008000;">'—————————————————————————————————————    
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Property</span> <span style="color: #0000A6;">Get</span> cdoOutgoingMailSMTP  <span style="color: #008000;">'As Integer
</span>    <span style="color: #008000;">'—————————————————————————————————————    
</span>        <span style="color: #0000A6;">If</span> InStr(1, Lcase(Me.From), <span style="color: #800000;">&quot;@gmail&quot;</span>) &lt;&gt; 0 <span style="color: #0000A6;">Then</span>
            cdoOutgoingMailSMTP = 465
        <span style="color: #0000A6;">ElseIf</span> InStr(1, LCase(Me.From), <span style="color: #800000;">&quot;@aol&quot;</span>) &lt;&gt; 0 <span style="color: #0000A6;">Then</span>
            cdoOutgoingMailSMTP = 587
        <span style="color: #0000A6;">Else</span>
            cdoOutgoingMailSMTP = 25
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Property</span>
&nbsp;
    <span style="color: #008000;">'—————————————————————————————————————
</span>    <span style="color: #008000;">' Name: Property cdoSMTPServer (Private)
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Purpose: Name/IP of SMTP Server
</span>    <span style="color: #008000;">'—————————————————————————————————————    
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Property</span> <span style="color: #0000A6;">Get</span> cdoSMTPServer  <span style="color: #008000;">'As String
</span>    <span style="color: #008000;">'—————————————————————————————————————    
</span>        <span style="color: #0000A6;">If</span> InStr(1, LCase(Me.From), <span style="color: #800000;">&quot;@yahoo&quot;</span>) &lt;&gt; 0 <span style="color: #0000A6;">Then</span>
            cdoSMTPServer = <span style="color: #800000;">&quot;smtp.mail.yahoo.com&quot;</span>
        <span style="color: #0000A6;">ElseIf</span> InStr(1, LCase(Me.From), <span style="color: #800000;">&quot;@gmail&quot;</span>) &lt;&gt; 0 <span style="color: #0000A6;">Then</span>
            cdoSMTPServer = <span style="color: #800000;">&quot;smtp.gmail.com&quot;</span>
        <span style="color: #0000A6;">ElseIf</span> InStr(1, LCase(Me.From), <span style="color: #800000;">&quot;@hotmail&quot;</span>) &lt;&gt; 0 <span style="color: #0000A6;">Or</span> _
               InStr(1, LCase(Me.From), <span style="color: #800000;">&quot;@live&quot;</span>) &lt;&gt; 0 <span style="color: #0000A6;">Then</span>
            cdoSMTPServer = <span style="color: #800000;">&quot;smtp.live.com&quot;</span>
        <span style="color: #0000A6;">ElseIf</span> InStr(1, LCase(Me.From), <span style="color: #800000;">&quot;@aol&quot;</span>) &lt;&gt; 0 <span style="color: #0000A6;">Then</span>
            cdoSMTPServer = <span style="color: #800000;">&quot;smtp.aol.com&quot;</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>    
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Property</span>
&nbsp;
    <span style="color: #008000;">'—————————————————————————————————————
</span>    <span style="color: #008000;">' Name: Property cdoUseSSL (Private)
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Purpose: Setting for SMTP Server's use of SSL (Boolean)
</span>    <span style="color: #008000;">'—————————————————————————————————————        
</span>    <span style="color: #0000A6;">Private</span> <span style="color: #0000A6;">Property</span> <span style="color: #0000A6;">Get</span> cdoUseSSL  <span style="color: #008000;">'As Boolean
</span>    <span style="color: #008000;">'—————————————————————————————————————    
</span>        cdoUseSSL = <span style="color: #0000A6;">True</span>
        <span style="color: #0000A6;">If</span> InStr(1, LCase(Me.From), <span style="color: #800000;">&quot;@aol&quot;</span>) &lt;&gt; 0 <span style="color: #0000A6;">Then</span>
            cdoUseSSL = <span style="color: #0000A6;">False</span>
        <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">If</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Property</span>
&nbsp;
    <span style="color: #008000;">'—————————————————————————————————————
</span>    <span style="color: #008000;">' Name: Property From (Public)
</span>    <span style="color: #008000;">' 
</span>    <span style="color: #008000;">' Purpose: Sender's Email ID
</span>    <span style="color: #008000;">'—————————————————————————————————————
</span>    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Property</span> <span style="color: #0000A6;">Let</span> From( <span style="color: #0000A6;">ByVal</span> Val )
           strFrom = Val
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Property</span>
    <span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Property</span> <span style="color: #0000A6;">Get</span> From <span style="color: #008000;">'As String
</span>           From = strFrom
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Property</span>
&nbsp;
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Class</span>
&nbsp;
<span style="color: #008000;">'—————————————————————————————————————————————  
</span><span style="color: #008000;">' Name: Sub Email (Public)
</span><span style="color: #008000;">' 
</span><span style="color: #008000;">' Purpose: Sends an Email Using CDO to a recipient
</span><span style="color: #008000;">' 
</span><span style="color: #008000;">' Parameters:
</span><span style="color: #008000;">'    sEmailID: Sender's Mail ID String
</span><span style="color: #008000;">'    sPassword: Sender's Password String
</span><span style="color: #008000;">'    sTo: Recipient's Mail ID String
</span><span style="color: #008000;">'    sSubject: Subject String
</span><span style="color: #008000;">'    sBody: Body Message String
</span><span style="color: #008000;">' 
</span><span style="color: #008000;">' Return: -
</span><span style="color: #008000;">'————————————————————————————————————————————— 
</span><span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> Email( EmailID, Password, Recipient, CC, Subject, Body )
<span style="color: #008000;">'————————————————————————————————————————————— 
</span>    <span style="color: #0000A6;">Set</span> Email = <span style="color: #0000A6;">New</span> clsSendMail
    <span style="color: #0000A6;">With</span> Email
        .Send EmailID, Password, Recipient, CC, Subject, Body
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">with</span> 
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span>
&nbsp;
<span style="color: #008000;">'—————————————————————————————————————————————  
</span><span style="color: #008000;">' Name: Sub EmailFromFile (Public)
</span><span style="color: #008000;">' 
</span><span style="color: #008000;">' Purpose: Sends an Email Using CDO to a recipient
</span><span style="color: #008000;">' 
</span><span style="color: #008000;">' Parameters:
</span><span style="color: #008000;">'    sEmailID: Sender's Mail ID String
</span><span style="color: #008000;">'    sPassword: Sender's Password String
</span><span style="color: #008000;">'    sTo: Recipient's Mail ID String
</span><span style="color: #008000;">'    sSubject: Subject String
</span><span style="color: #008000;">'    sCompleteFilePath: Text File containing the Body Message
</span><span style="color: #008000;">' 
</span><span style="color: #008000;">' Return: -
</span><span style="color: #008000;">'————————————————————————————————————————————— 
</span><span style="color: #0000A6;">Public</span> <span style="color: #0000A6;">Function</span> EmailFromFile( EmailID, Password, Recipient, CC, Subject, sCompleteFilePath )
<span style="color: #008000;">'————————————————————————————————————————————— 
</span>    <span style="color: #0000A6;">Set</span> EmailFromFile = <span style="color: #0000A6;">New</span> clsSendMail
    <span style="color: #0000A6;">With</span> EmailFromFile
        .LoadBodyMessage sCompleteFilePath
        .Send EmailID, Password, Recipient, CC, Subject, <span style="color: #800000;">&quot;&quot;</span>
    <span style="color: #0000A6;">End</span> <span style="color: #0000A6;">with</span> 
<span style="color: #0000A6;">End</span> <span style="color: #0000A6;">Function</span></pre></div></div>

<p>You can use this code as a .VBS file, but, please remember to remove/comment the Reporter.ReportEvent statement on Line 113.</p>
<p><a href="http://relevantcodes.com/Articles/CDO/CDOQTP.txt">Download for QTP</a><br />
   <a href="http://relevantcodes.com/Articles/CDO/CDOVBS.txt" >Download for executing as .VBS</a></p>
<p class="alert">If you have any questions, please ask them in the comments section. If your query is confidential, please use the <a href="http://relevantcodes.com/contact/">Contact Form</a> to send me an e-mail instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://relevantcodes.com/cdo-send-email-from-yahoo-hotmail-live-aol-or-gmail/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
	</channel>
</rss>

