{"id":531,"date":"2011-10-09T17:27:04","date_gmt":"2011-10-09T11:57:04","guid":{"rendered":"http:\/\/www.automationrepository.com\/?p=531"},"modified":"2011-10-09T17:27:29","modified_gmt":"2011-10-09T11:57:29","slug":"global-variables-in-qtp","status":"publish","type":"post","link":"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/global-variables-in-qtp\/","title":{"rendered":"2 Different Ways to Create Global Variables in QTP"},"content":{"rendered":"<div style=\"font-family: Verdana;\">\n<p style=\"text-align: justify;\">Did you ever face any situation where you needed to create global variables in QTP so that these global values can be used across multiple actions\/functions within your test script? How did you tackle the problem then? If you would have noticed, QTP doesn&#8217;t provide any direct method to create global variables. But you can<!--more--> use any of the below 2 methods to create variables with Global Scope in QTP.<\/p>\n<h3 style=\"text-align: justify;\"><span style=\"text-decoration: underline;\">By using Environment Variables<\/span><\/h3>\n<p style=\"text-align: justify;\">This is the most commonly used method to create variables in QTP with global scope. <span style=\"text-decoration: underline;\">All you have to do is just create a User-Defined Environment variable anywhere in your script (be it at the beginning of your action, or even inside any of your functions). That&#8217;s all. Once your environment variable is created, it would be available throughout your test script (inside all your actions &amp; functions within the test script)<\/span>. Example:<code><\/code><\/p>\n<p><code><span style=\"color: #008000;\">'Declare Environment Variable (Global Variable)<\/span><br \/>\nEnvironment.Value(<span style=\"color: #993300;\">\"gloVarBlogName\"<\/span>) = <span style=\"color: #993300;\">\"Automation Repository\"<\/span><\/code><br \/>\n<code><br \/>\n<span style=\"color: #008000;\"> 'Use Environment Variable (Global Variable)<\/span><br \/>\nmsgbox Environment.Value(<span style=\"color: #993300;\">\"gloVarBlogName\"<\/span>)<code><\/code><br \/>\n<\/code><\/p>\n<h3><span style=\"text-decoration: underline;\">By using Function Library<\/span><\/h3>\n<p>You can also use a function library to create and use variables with global scope in your QTP script. This is how it can be done.<\/p>\n<p><strong>1.<\/strong> Create a function library(.vbs file) and associate it to your test script.<\/p>\n<p><strong>2.<\/strong> In your function library, declare a variable &amp; assign required value to it. Example:<br \/>\n<code><br \/>\n<span style=\"color: #008000;\">'Declare variable in Function Library<\/span><br \/>\nDim gloVarBlogName<br \/>\ngloVarBlogName = <span style=\"color: #993300;\">\"Automation Repository\"<\/span><\/code><\/p>\n<p style=\"text-align: justify;\">Once you have defined the variable in the function library &amp; associated the function library with your test script, you can then use this variable anywhere in your script. For Example, writing <code>msgbox gloVarBlogName<\/code> in your action would display a message box with text &#8211; Automation Repository<\/p>\n<p style=\"text-align: justify;\"><em>Other than the above two methods you can also store your variables in some external file (such as excel sheet, text file,\u00a0 xml file etc) and then you can write code to access these variable values from the external file. Usually this method is not used much because anyways you can get the job done with environment variables &amp; function library with much less code.<\/em><\/p>\n<p>&nbsp;<\/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, you can join our blog to get free email updates directly in your inbox.<\/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\" \/><\/form>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Did you ever face any situation where you needed to create global variables in QTP so that these global values can be used across multiple actions\/functions within your test script? How did you tackle the problem then? If you would have noticed, QTP doesn&#8217;t provide any direct method to create global variables. But you can<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,9],"tags":[42,41],"class_list":["post-531","post","type-post","status-publish","format-standard","hentry","category-qtp-basic-stuff","category-qtp-concepts","tag-qtp-global-variables","tag-qtp-variables"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>2 Different Ways to Create Global Variables in QTP - XX<\/title>\n<meta name=\"description\" content=\"How to declare global variables in QTP. you can use user-defined environment variables or function library to create variables with global scope in 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\/2011\/10\/global-variables-in-qtp\/\" \/>\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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/global-variables-in-qtp\/\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/global-variables-in-qtp\/\",\"name\":\"2 Different Ways to Create Global Variables in QTP - XX\",\"isPartOf\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#website\"},\"datePublished\":\"2011-10-09T11:57:04+00:00\",\"dateModified\":\"2011-10-09T11:57:29+00:00\",\"author\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20\"},\"description\":\"How to declare global variables in QTP. you can use user-defined environment variables or function library to create variables with global scope in QTP.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/global-variables-in-qtp\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/global-variables-in-qtp\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/global-variables-in-qtp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.automationrepository.com\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"2 Different Ways to Create Global Variables in QTP\"}]},{\"@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":"2 Different Ways to Create Global Variables in QTP - XX","description":"How to declare global variables in QTP. you can use user-defined environment variables or function library to create variables with global scope in 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\/2011\/10\/global-variables-in-qtp\/","twitter_misc":{"Written by":"Anish Pillai","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/global-variables-in-qtp\/","url":"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/global-variables-in-qtp\/","name":"2 Different Ways to Create Global Variables in QTP - XX","isPartOf":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/#website"},"datePublished":"2011-10-09T11:57:04+00:00","dateModified":"2011-10-09T11:57:29+00:00","author":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20"},"description":"How to declare global variables in QTP. you can use user-defined environment variables or function library to create variables with global scope in QTP.","breadcrumb":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/global-variables-in-qtp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/global-variables-in-qtp\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/global-variables-in-qtp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.automationrepository.com\/wordpress\/"},{"@type":"ListItem","position":2,"name":"2 Different Ways to Create Global Variables in QTP"}]},{"@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\/531","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=531"}],"version-history":[{"count":22,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/531\/revisions"}],"predecessor-version":[{"id":553,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/531\/revisions\/553"}],"wp:attachment":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}