EmbedScriptFromFile& RunScriptFromFile - QTP / UFT [英] EmbedScriptFromFile & RunScriptFromFile - QTP/UFT
问题描述
请帮我使用 EmbedScriptFromFile
&用于在QTP / UFT中执行JS文件的 RunScriptFromFile
。
Please help me in using EmbedScriptFromFile
& RunScriptFromFile
for executing JS file in QTP/UFT.
我正在尝试使用JS获取N个值文件,并在数组中的QTP / UFT中接收相同的文件。为此,我必须了解 EmbedScriptFromFile
& QTP / UFT帮助部分中的 RunScriptFromFile
。但是当我尝试使用示例代码时,我无法按预期进行。请帮助我解决这个问题
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脚本代码:
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脚本,我正在使用:
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
2014年12月15日:
我试过以下建议,它的工作原理!但最重要的是,我试图从JavaScript函数中获取数组值。
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.
创建数组的代码:
function makeArray() {
var myArray = new Array(4);
for (var i = 0; i < myArray.length; i++){
myArray[i] = i+1;
}
return myArray;
}
所以我想要实现的是,执行 makeArray()
通过传递创建数组并使用
将值/数组作为参数返回到 cloneArray
方法创建QTP / UFT支持数组的函数makeArray() ConeArray(arr)
。但是,当我尝试使用以下代码实现此目的时,我无法实现。
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
正常工作,但在尝试将另一个函数作为参数传递时无法使用。
EmbedScript
& RunScript
are working fine when I try separately, but not able to use while trying to pass another function as parameter.
我试图将这两个函数放在同一个JS文件中并调用函数,并尝试了其他一些可能性。但没有任何帮助,所以请帮助。
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.
推荐答案
首先我们应该了解 RunScript
和 EmbedScript
函数(及其 FromFile
变体)。
First of all we should understand the RunScript
and EmbedScript
functions (and their FromFile
variants).
-
RunScript
是一种Page
和Frame
,它接受一个JavaScript并执行它,返回脚本的结果(通常是最后一个表达式运行)。 -
EmbedScript
是一种浏览器
的方法,它意味着确保此脚本在所有页面上运行从现在起
code> s和此
。此函数不返回任何值,因为它的主要目的是将来运行(尽管它也会立即在浏览器
的框架Page
和现有的Frame $ c上运行$ c> s当前位于
浏览器
中。可以使用EmbedScript
来使JavaScript函数可用于将来的RunScript
使用。
RunScript
is a method ofPage
andFrame
and it accepts a JavaScript and executes it, returning the result of the script (typically the last expression run).EmbedScript
is a method ofBrowser
and it means "make sure that this script is run on allPage
s andFrame
s of thisBrowser
from now on". This function returns no value since its main purpose is to run in the future (although it also runs immediately on thePage
and existingFrame
s currently in theBrowser
).EmbedScript
can be used in order to make a JavaScript function available for futureRunScript
use.
这些函数的普通版本接受一些JavaScript脚本,而 FromFile
变体采用文件名(或者是文件系统或在ALM中)并读取该文件。
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.
关于你的问题 - 在你的第二行你正在预先形成一个 RunScriptFromFile
但是没有传递文件名,你似乎在传递一个脚本(为此你应该使用 RunScript
)。另外,您传递给 cloneArray
的参数不是有效的JavaScript值。
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'])")
无论如何将JavaScript数组传递给VBScript是有问题的, .length
属性工作正常但索引编制进入数组是一个问题(可能是因为JavaScript使用方括号,而VBScript使用括号)。
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];
}
};
}
然后你可以在UFT / QTP中使用以下内容。
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
输出:
0:回答
1:42
0: answer
1: 42
这篇关于EmbedScriptFromFile& RunScriptFromFile - QTP / UFT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!