EmbedScriptFromFile & RunScriptFromFile - QTP/UFT

ぃ、小莉子 提交于 2019-11-30 23:09:08

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!