{"id":1063,"date":"2012-01-29T00:45:00","date_gmt":"2012-01-28T19:15:00","guid":{"rendered":"http:\/\/www.automationrepository.com\/?p=1063"},"modified":"2012-01-29T00:47:26","modified_gmt":"2012-01-28T19:17:26","slug":"qtp-and-vbscript-tutorials-introduction-to-vbscript","status":"publish","type":"post","link":"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/","title":{"rendered":"Part 1: QTP and VBScript | Introduction to VBScript"},"content":{"rendered":"<div style=\"text-align: justify; font-family: Verdana;\">\n<p><em>With this post, we are starting off a series of articles on the basic concepts of VBScript that you would be using extensively while creating automation test scripts in QTP. These articles would not be providing a comprehensive reference of VBScript. <strong>Instead, this would act as a quick refresher course on VBScript<!--more--> that would help you get started with QTP.<\/strong><\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" title=\"QTP and VBScript\" src=\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-and-vbscript.png\" alt=\"QTP and VBScript\" width=\"291\" height=\"95\" \/><\/p>\n<h3>VBScript &#8211; Quick Introduction<\/h3>\n<p>VBScript, modeled on Visual Basic, is an active scripting language developed by Microsoft. VBScript is a light programming language that uses the Component Object Model to access elements of the environment within which it is running. The first version of VBScript, version 1.0, was released by Microsoft in 1996. The current stable version of VBScript that is available is version 5.8. QTP uses VBScript to specify a test procedure, and to manipulate the objects and controls of the application under test.<br \/>\n<span><br \/>\n<\/span><\/p>\n<h3>Data Types in VBScript<\/h3>\n<p>VBScript has only one data type known as a <strong>Variant<\/strong>. A Variant is a special kind of data type that can contain different types of values, based on how it is used. The Variant data type in VBScript has different subtypes that represent different types of values stored in a Variant data type. <strong>Some of the common Variant subtypes that are generally used in QTP are listed in the below table.<\/strong><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"102\"><strong>Subtype<\/strong><\/td>\n<td valign=\"top\" width=\"309\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"102\">Integer<\/td>\n<td valign=\"top\" width=\"309\">Contains an integer value<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"102\">String<\/td>\n<td valign=\"top\" width=\"309\">Contains a text<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"102\">Boolean<\/td>\n<td valign=\"top\" width=\"309\">Contains either True or False<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"102\">Date<\/td>\n<td valign=\"top\" width=\"309\">Contains a value that represents a date.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"102\">Object<\/td>\n<td valign=\"top\" width=\"309\">Contains an object<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span><br \/>\n<\/span><\/p>\n<h3>Variables in VBScript<\/h3>\n<p><strong>Variable Declaration and Assignment<\/strong><br \/>\nYou can explicitly declare variables in QTP using <strong>Dim<\/strong>, <strong>Public<\/strong> or <strong>Private<\/strong> statement (Dim is the most commonly used option). Assigning a value to a variable follows the standard procedure where the variable is in the left hand side followed by equal to operator with the value on the right hand side.<br \/>\n<span style=\"text-decoration: underline; color: #0000ff;\">Note:<\/span><br \/>\n<span style=\"color: #0000ff;\"> 1) It is not necessary to declare a variable to use it.<\/span><br \/>\n<span style=\"color: #ff0000;\">2) You cannot declare a variable and assign value to it in the same statement.<\/span><br \/>\n<span style=\"color: #0000ff;\">3) You can use the same variable for storing multiple types of values such as a string, integer etc (although this is not considered a good programming practice)<\/span><br \/>\n<span style=\"color: #0000ff;\">4) You can write multiple statements in the same line in QTP by separating them using a colon (:)<\/span><br \/>\n<span style=\"color: #0000ff;\">5) Comments can be specified in QTP using <strong>single quote (&#8216;)<\/strong> or <strong>Rem<\/strong> statement followed by the text.<\/span><br \/>\n<em>Let&#8217;s see some examples for variable declaration declaration &amp; assignment.<\/em><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Declaring variables\r\nDim a\r\nDim b\r\nDim c, d, e 'Multiple Declarations in a single statement\r\n\r\n'Assigning values\r\na = 1\r\nb = &quot;some text&quot;\r\nc = 2 : d = &quot;abc&quot; : e = true 'Multiple statements in single line\r\n\r\n'Assigning value to undeclared variable\r\nstartDate = #06\/18\/2008#\r\nstartTime = #3:36:00 PM#\r\n\r\n'Dim f = 12 -- This statement would give error..\r\n\r\n'Assigning different types of data to same variable\r\na = 1\r\na = &quot;text&quot;\r\na = false\r\n\r\n'This is a comment\r\nRem This is also a comment\r\n<\/pre>\n<p><em> <\/em><br \/>\n<strong>Using Option Explicit in QTP. <\/strong>Using Option Explicit statement in QTP forces you to declare every variable that you want to use in your script. Using any undeclared variable will result in an error.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nOption Explicit\r\n\r\nDim a, b\r\n\r\na = 10\r\nb = 20\r\n'c = 30 -- This statement would throw an error\r\n<\/pre>\n<p><em> <\/em><br \/>\n<strong>Scope of variables in VBScript. <\/strong> A variable&#8217;s scope is determined by where it is declared. If you declare a variable within a procedure, only code within that procedure can access or change the value of that variable. It has local scope and is a procedure-level variable. If you declare a variable outside a procedure, you make it recognizable to all the procedures in your script. This is a script-level variable, and it has script-level scope.<\/p>\n<p>The lifetime of a variable depends on how long it exists. The lifetime of a script-level variable extends from the time it is declared until the time the script is finished running. At procedure level, a variable exists only as long as you are in the procedure. When the procedure exits, the variable is destroyed.<br \/>\n<span><br \/>\n<\/span><\/p>\n<h3>Arrays in VBScript<\/h3>\n<p><span style=\"color: #0000ff;\">1) Array variables are declared the same way as normal variables except that the variable name should be followed by parentheses.<\/span><br \/>\n<span style=\"color: #0000ff;\"> 2) In a two-dimensional array, the first number represents the number of rows and the second number represents the number of columns.<\/span><br \/>\n<span style=\"color: #0000ff;\"> 3) The size of an array can be changed at run-time using <strong>ReDim<\/strong> statement.<\/span><br \/>\n<span style=\"color: #0000ff;\"> 4) You can also use the <strong>Preserve<\/strong> keyword to preserve the contents of the array during its re-size.<\/span><br \/>\n<em> Let&#8217;s see some examples of how to use arrays in QTP.<\/em><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Declare an array of size 10\r\nDim arr(9)\r\n\r\narr(0) = &quot;0&quot;\r\narr(1) = &quot;1&quot;\r\n'.. and so on..\r\n\r\n'Declare Multi-Dimension array\r\nDim arrM(1, 1)\r\narrM(0,0) = &quot;00&quot;\r\narrM(0,1) = &quot;01&quot;\r\narrM(1,0) = &quot;10&quot;\r\narrM(1,1) = &quot;11&quot;\r\n\r\n'Using Dynamic Array\r\nDim arr_New()\r\n'.......\r\n'.......\r\n\r\nReDim Preserve arr_New(1)\r\narr_New(0) = &quot;0&quot;\r\narr_New(1) = &quot;1&quot;\r\n\r\nReDim Preserve arr_New(3)\r\narr_New(2) = &quot;2&quot;\r\narr_New(3) = &quot;3&quot;\r\n<\/pre>\n<p><em> <\/em><br \/>\nThis was all about how you can use variables in QTP. In the upcoming articles, we&#8217;ll be discuss more about various other VBScript concepts. <em>Please use the comments section or the <a title=\"Contact Me\" href=\"http:\/\/www.automationrepository.com\/contact\/\" target=\"_blank\">contact page<\/a> to connect with us in case you have any queries about the concepts mentioned in this article. Thanks for visiting.. :-&gt;<\/em><br \/>\n<span><br \/>\n<\/span><\/p>\n<\/div>\n<div style=\"font-family: Verdana,sans-serif; border: 1px solid #C8B560; padding: 3mm; background: #FFF8C6; text-align: center;\">\n<p>If you enjoyed this article, <strong>you can join our blog to get free email updates directly in your inbox.<\/strong><\/p>\n<form style=\"text-align: center;\" action=\"http:\/\/feedburner.google.com\/fb\/a\/mailverify\" method=\"post\" onsubmit=\"window.open('http:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=automationrepository\/feeds', 'popupwindow', 'scrollbars=yes,width=550,height=520');return true\" target=\"popupwindow\"><input style=\"width: 160px; height: 20px;\" onfocus=\"if (this.value == 'Enter Your Email Address') {this.value = '';}\" onblur=\"if (this.value == '') {this.value = 'Enter Your Email Address';}\" type=\"text\" value=\"Enter Your Email Address\" \/> <input type=\"hidden\" name=\"uri\" value=\"automationrepository\/feeds\" \/> <input type=\"hidden\" name=\"loc\" value=\"en_US\" \/> <input type=\"submit\" value=\"Join Us\" \/><\/p>\n<p style=\"text-align: justify;\">\n<\/form>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>With this post, we are starting off a series of articles on the basic concepts of VBScript that you would be using extensively while creating automation test scripts in QTP. These articles would not be providing a comprehensive reference of VBScript. Instead, this would act as a quick refresher course on VBScript<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64,37],"tags":[68,67,66,65],"class_list":["post-1063","post","type-post","status-publish","format-standard","hentry","category-qtp-and-vbscript-tutorials","category-qtp-tutorials-for-beginners","tag-preserve","tag-redim","tag-vbscript-arrays","tag-vbscript-variables"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Part 1: Working with Variables and Arrays in VBScript<\/title>\n<meta name=\"description\" content=\"Learn about variable declaration and value assignment in QTP. See why Option Explicit statement is used in VBScript. Also read about array declaration in VBScript\/QTP.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anish Pillai\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/\",\"name\":\"Part 1: Working with Variables and Arrays in VBScript\",\"isPartOf\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-and-vbscript.png\",\"datePublished\":\"2012-01-28T19:15:00+00:00\",\"dateModified\":\"2012-01-28T19:17:26+00:00\",\"author\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20\"},\"description\":\"Learn about variable declaration and value assignment in QTP. See why Option Explicit statement is used in VBScript. Also read about array declaration in VBScript\/QTP.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/#primaryimage\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-and-vbscript.png\",\"contentUrl\":\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-and-vbscript.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.automationrepository.com\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Part 1: QTP and VBScript | Introduction to VBScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#website\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/\",\"name\":\"XX\",\"description\":\"\\r\\nasas\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.automationrepository.com\/wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20\",\"name\":\"Anish Pillai\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g\",\"caption\":\"Anish Pillai\"},\"description\":\"Find more about Anish Pillai on Google+\",\"sameAs\":[\"http:\/\/www.automationrepository.com\"],\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/author\/anish\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Part 1: Working with Variables and Arrays in VBScript","description":"Learn about variable declaration and value assignment in QTP. See why Option Explicit statement is used in VBScript. Also read about array declaration in VBScript\/QTP.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/","twitter_misc":{"Written by":"Anish Pillai","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/","url":"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/","name":"Part 1: Working with Variables and Arrays in VBScript","isPartOf":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/#primaryimage"},"image":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-and-vbscript.png","datePublished":"2012-01-28T19:15:00+00:00","dateModified":"2012-01-28T19:17:26+00:00","author":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20"},"description":"Learn about variable declaration and value assignment in QTP. See why Option Explicit statement is used in VBScript. Also read about array declaration in VBScript\/QTP.","breadcrumb":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/#primaryimage","url":"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-and-vbscript.png","contentUrl":"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-and-vbscript.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/01\/qtp-and-vbscript-tutorials-introduction-to-vbscript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.automationrepository.com\/wordpress\/"},{"@type":"ListItem","position":2,"name":"Part 1: QTP and VBScript | Introduction to VBScript"}]},{"@type":"WebSite","@id":"https:\/\/www.automationrepository.com\/wordpress\/#website","url":"https:\/\/www.automationrepository.com\/wordpress\/","name":"XX","description":"\r\nasas","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.automationrepository.com\/wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20","name":"Anish Pillai","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g","caption":"Anish Pillai"},"description":"Find more about Anish Pillai on Google+","sameAs":["http:\/\/www.automationrepository.com"],"url":"https:\/\/www.automationrepository.com\/wordpress\/author\/anish\/"}]}},"_links":{"self":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/1063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1063"}],"version-history":[{"count":21,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/1063\/revisions"}],"predecessor-version":[{"id":1084,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/1063\/revisions\/1084"}],"wp:attachment":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}