EmbedScriptFromFile & RunScriptFromFile - QTP / UFT

Please help me to use EmbedScriptFromFile

and RunScriptFromFile

execute JS file in QTP / UFT.

I am trying to get N number of values ​​using a JS file and I get the same thing in QTP / UFT in an array. For which I found out about EmbedScriptFromFile

and RunScriptFromFile

in the QTP / UFT help section. But when I tried to use the sample code, I could not do it as expected. Please help me in this release

Java Script code I'm using:

function cloneArray(arr) {
          var ret = [];
          for (var i = 0; i < arr.length; ++i)
          ret.push(arr[i]);
       return ret;
 }

      

VB Script, I am using:

Browser("Home").Page("Home").EmbedScriptFromFile "C:\Users\Gopi\Desktop\CloneArray.js" 'Call the function and run the script that returns the array'
Set cloned = Browser("Home").Page("Home").RunScriptFromFile("cloneArray(C:\Users\Gopi\Desktop)")

      

Getting some errors while executing these two lines - for the first line I get an error because "Object does not support this property or method." And for the second line, I get the error "Parameter is invalid". Please, help


15-Dec-2014:

I tried the suggestions below and it worked! But apart from that, I am also trying to get array values ​​from JavaScript function.

Code to create array:

function makeArray() {
    var myArray = new Array(4);
    for (var i = 0; i < myArray.length; i++){
        myArray[i] = i+1;
    }
    return myArray;
}

      

So exactly what I'm trying to achieve, execute a function makeArray()

to create an array and create a supporting QTP / UFT array with a method cloneArray

, passing the return value / array makeArray()

as a parameter to ConeArray(arr)

. But when I try to achieve it with the following code, I couldn't do it.

Browser("Home").EmbedScriptFromFile "C:\Users\Gopi\Desktop\cloneArray.js"
'Set arr1 = Browser("Home").Page("Home").RunScriptFromFile "C:\Users\Gopi\Desktop\makeArray.js"
Set arr = Browser("Home").Page("Home").RunScript("cloneArray[C:\Users\Gopi\Desktop\makeArray.js]")
For i = 0 To arr.length - 1
    msgbox i & ": " & arr.item(i)
Next

      

EmbedScript

and RunScript

work fine when I try to stand alone but can't use when trying to pass another function as a parameter.

I tried to use both functions in one JS file and call functions, and tried several other possibilities. But nothing helped, so please help.

+3


source to share


1 answer


First of all, we must understand the functions of RunScript

and EmbedScript

(and their variants FromFile

).

  • RunScript

    is a method Page

    and Frame

    , and it takes JavaScript and executes it, returning the result of the script (usually a run of the last expression).
  • EmbedScript

    - a method Browser

    , which means: "make sure that the script is running at all Page

    , and Frame

    that Browser

    from that moment on." This function does not return a value, since its main purpose is to be run in the future (although it also runs immediately on Page

    and Frame

    currently in Browser

    ). EmbedScript

    can be used to make a JavaScript function available for future use RunScript

    .

Simple versions of these functions take some JavaScript script, while the variation FromFile

takes a filename (either in the filesystem or ALM) and reads that file.

As for your question - on the second line, you are creating RunScriptFromFile

but not passing in the filename, you seem to be passing in a script (you should be using for that RunScript

). Also, the parameter you pass to cloneArray

is not a valid JavaScript value.

If you want it to be a string you have to put it in quotes, it looks like you are expecting an array anyway, so perhaps you did this:

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

      

It's problematic to pass JavaScript arrays to VBScript anyway, the property .length

works fine, but indexing into an array is a problem (possibly because JavaScript uses square brackets, while VBScript uses parentheses).



A workaround for the array problem could be something like this

// wrapArray.js
function wrapArray(array) {
    return { 
        length: array.length,
        item: function(index) {
            return array[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 = 0 To arr.length - 1
    Print i & ": " & arr.item(i)
Next

      

Output:

0: answer
1: 42

+4


source







All Articles