Tuesday, 13 November 2012

QTP/vbs - Dynamically loading executable code - pt I.

There is a cool function in vbs which allows you to execute a string as regular vbs code.
This function is Execute.
The downside is that the Execute command is not persistent; i.e. the variables declared in memory, and functions 'registered', will not be available globally[0]. I didn't realize this in 3 years of using QTP until I came to reimplement it on another project.

[0] Example
strMyVbs = "strSomeText = ""Hello World"" "


f_loadMyVariable
msgbox (strSomeText) 'This does not print Hello World to the screen, because the executed code was/is local to the function



Function f_loadMyVariable()
  Execute strMyVbs
  msgbox (strSomeText) 'This will print Hello World to the screen
End Function

Hopefully, for the sake of my productivity today, there will be a post following showing how to make this a global thing. -- solved, QTP/vbs - Dynamically loading executable code - pt II. --

Implementation

A quick elaboration on implementation
Dim uri
uri = PathFinder.Locate("[QualityCenter\Resources] Resources\SomeOtherFolder\myFile.qfl")

Dim oFileSystem, oFile
Set oFileSystem = CreateObject("Scripting.fileSystemObject")
Set oFile = oFileSystem.OpenTextFile(uri)
Dim strInputText
strInputText = oFile.ReadAll
Execute strInputText
oFile.Close


No comments:

Post a Comment

Robot Framework, Basic Setup

Plug: Robot Framework is quick to setup, easy to write tests for, and super fast to triage failures in. The last point really sets it apart...