javascript - EmbedScriptFromFile & RunScriptFromFile - QTPUFT - Stack Overflow

Please help me in using EmbedScriptFromFile & RunScriptFromFile for executing JS file in QTPUFT.I&

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 QTP/UFT in an array. For which, I have got to know about EmbedScriptFromFile & RunScriptFromFile in QTP/UFT help section. But when I've tried to use the sample code, I couldn't make it as expected. Please help me in this issue

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'm 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 line - for first line, I'm getting error as 'Object doesn't support this property or method'. And for the second line, I'm getting error as 'The parameter is incorrect'. Please help


15-Dec-2014: I have tried the suggestions below and it worked! But on top of that, I'm trying to get the array values too from JavaScript function.

Code to create an 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 is, execute makeArray() function to create an array and create the QTP/UFT supporting array using cloneArray method by passing the makeArray() returning value/array as parameter to ConeArray(arr). But when I try to achieve this with following code, I couldn't make 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 & RunScript are working fine when I try separately, but not able to use while trying to pass another function as parameter.

I have tried to have both the functions in same JS file and call the functions, and tried with a few other possibilities too. But nothing helped, so please help.

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 QTP/UFT in an array. For which, I have got to know about EmbedScriptFromFile & RunScriptFromFile in QTP/UFT help section. But when I've tried to use the sample code, I couldn't make it as expected. Please help me in this issue

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'm 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 line - for first line, I'm getting error as 'Object doesn't support this property or method'. And for the second line, I'm getting error as 'The parameter is incorrect'. Please help


15-Dec-2014: I have tried the suggestions below and it worked! But on top of that, I'm trying to get the array values too from JavaScript function.

Code to create an 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 is, execute makeArray() function to create an array and create the QTP/UFT supporting array using cloneArray method by passing the makeArray() returning value/array as parameter to ConeArray(arr). But when I try to achieve this with following code, I couldn't make 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 & RunScript are working fine when I try separately, but not able to use while trying to pass another function as parameter.

I have tried to have both the functions in same JS file and call the functions, and tried with a few other possibilities too. But nothing helped, so please help.

Share Improve this question edited Dec 15, 2014 at 11:44 zeal asked Dec 12, 2014 at 6:36 zealzeal 4754 gold badges12 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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:

Set cloned = 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.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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745525504a4631451.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信