Environment Variables

By xerocube | December 8, 2006

Today, I faced a seemingly simple task in QuickTest Pro. I have been toying with creating my own XMLDom object in VBScript to generate custom data report of my test results. After creating the proof of concept I needed, I started implementing my code into my existing script.

My script is broken into 5 actions, each of which perform a certain task. My validation action loops through a data table with hundreds of rows. The way my XMLDom obj is written though, I have to set my variables to a new XMLDom object. Combine this with the fact that variables in QTP Actions are limited in scope to the action they are declared – this posed an issue.

Here’s our initial sample code:

Dim objXMLDoc

Set objXMLDoc = CreateObject(“Microsoft.XMLDOM”)
objXMLDoc.async = False

‘ Write out the new file
objXMLDoc.save(“c:\P1_” & Year(Now) & Month(Now) & Day(Now) & “_” & Hour(Now) & Minute(Now) & “.xml”)

‘ Destroy the objects
set objXMLDoc = Nothing

The simple thought of using a global variable came to mind. That’s when I realized that there are no global variables. So, as the post heading gave away, we started investigating Environment Variables with success.

Here’s our new code:

Environment(“objXMLDoc”) = CreateObject(“Microsoft.XMLDOM”)

Environment(“objXMLDoc”).async = False

‘ Write out the new file
Environment(“objXMLDoc”).save(“c:\P1_” & Year(Now) & Month(Now) & Day(Now) & “_” & Hour(Now) & Minute(Now) & “.xml”)

‘ Destroy the objects
Environment(“objXMLDoc”) = Nothing

What we’ve done is create a makeshift “global” variable that all actions within my script can use. A couple of notes to make though, everywhere that you reference your environment variable must use the Environment(“MyVar”) notation. Also, you cannot use the SET keyword either, otherwise it will result in an error.

Now, we’ve created a variable that can be used across multiple actions and is in scope throughout the course of this test. As always, be sure to clean up your trash afterwards.

Good Luck and Happy coding!

Topics: Code, QuickTest Pro | No Comments »