Skip to content Skip to sidebar Skip to footer

Embedscriptfromfile & Runscriptfromfile - Qtp/uft

Please help me in using EmbedScriptFromFile & RunScriptFromFile for executing JS file in QTP/UFT. I'm trying to fetch N number of values using JS file, and receive the same in

Solution 1:

First of all we should understand the RunScript and EmbedScript functions (and their FromFile variants).

  • RunScript is a method of Page and Frame and it accepts a JavaScript and executes it, returning the result of the script (typically the last expression run).
  • EmbedScript is a method of Browser and it means "make sure that this script is run on all Pages and Frames of this Browser from now on". This function returns no value since its main purpose is to run in the future (although it also runs immediately on the Page and existing Frames currently in the Browser). EmbedScript can be used in order to make a JavaScript function available for future RunScript use.

The plain versions of these functions accept some JavaScript script while the FromFile variation takes a file name (either on the file system or in ALM) and reads that file.

Regarding your question - on your second line you're preforming a RunScriptFromFile but aren't passing a file name, you seem to be passing a script (for this you should be using RunScript). Additionaly the parameter you're passing to cloneArray is not an a valid JavaScript value.

If you want it to be a string you should put it in quotes, in any case it looks like you're expecting an array so perhaps you meant to do this:

Setcloned= Browser("Home").Page("Home").RunScript("cloneArray(['Users', 'Gopi'])")

In any case it's problematic to pass JavaScript arrays into VBScript, the .length property works fine but indexing into the array is a problem (perhaps due to the fact that JavaScript uses square brackets while VBScript uses parentheses).

A workaround for the array problem could be something like this

// wrapArray.jsfunctionwrapArray(array) {
    return { 
        length: array.length,
        item: function(index) {
            returnarray[index];
        }
    };
}

Then you can use the following in UFT/QTP.

Browser("B").EmbedScriptFromFile "C:\wrapArray.js"Set arr = Browser("B").Page("P").RunScript("wrapArray(['answer', 42])")
For i = 0To arr.length - 1
    Print i & ": " & arr.item(i)
Next

Output:

0: answer 1: 42

Post a Comment for "Embedscriptfromfile & Runscriptfromfile - Qtp/uft"