Embedscriptfromfile & Runscriptfromfile - Qtp/uft
Solution 1:
First of all we should understand the RunScript
and EmbedScript
functions (and their FromFile
variants).
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.
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"