Execute Javascript from QuickTest Pro
Despite persistent the rumors to the contrary, you really can execute Javascript from QTP, and not just using Web Extensibility. Here is a simple function that will run some javascript in IE and return the result.
-
Public Function evalJS(oBrowser, sJavaScript)
-
Set JSEntry = oBrowser.object.document.documentelement.parentnode.parentwindow
-
On Error Resume Next
-
evalJS = JSEntry.eval(sJavaScript)
-
On Error Goto 0
-
End Function
-
-
Set oBrowser = Browser("version:=inter.*")
-
evalJS oBrowser, "alert('Hello, world');"
*** See the update below ***
That should make IE pop up a "Hello, world" alert. That won't do much for you, but if the page you are testing has a variable set in Javascript, this is a great way to get at it. For instance, an application I work with creates a javascript variable called openPopups that contains the ids of all DivPopups that are currently displayed. To get the first displayed DivPopup, I can run sPopupId = evalJS(oBrowser, "openPopups[0];").
Defining Functions
If you have a Javascript function you want to run, you can define that with evalJS as well. This should get you the same useless output as before and serve the purpose of demonstrating the loading of a javascript function through QTP:
-
sJSFunction = "function alertThis(sThis){ alert(sThis) }"
-
evalJS oBrowser, sJSFunction
-
evalJS oBrowser, "alertThis('Hello, world');"
Of course this requires you to squeeze the function into one line. An alternative is to keep your Javascript library in a text file and load it using loadJsLib.
Put this in a text file called c:\jsdemo.js.
-
function alertThis (sAlertText) {
-
alert(sAlertText);
-
}
-
-
function isItOne (iNumber) {
-
var bReturn = false;
-
if (iNumber == 1)
-
{
-
bReturn = true;
-
}
-
return bReturn
-
}
Now run this in QTP. Make sure evalJS is available too.
-
Function loadJsLib ( oBrowser, sLibPath )
-
Set fso = CreateObject("Scripting.FileSystemObject")
-
Set f = fso.OpenTextFile(sLibPath, 1)
-
sLibText = f.ReadAll
-
loadJsLib = evalJS( oBrowser, sLibText)
-
End Function
-
-
LoadJsLib oBrowser, "c:\jsdemo.js"
-
print evalJS(oBrowser, "isItOne(1)")
UPDATE: MJP points out in the comments that evalJS may not work as written for everybody. If you have trouble with it, try changing the first line to:
-
Set JSEntry = oBrowser.Page("micClass:=Page").object.documentelement.parentnode.parentwindow
UPDATE 2: I realized I didn't give credit where credit is due. I got the idea from this article at Ajaxian and applied a few hours of hacking to get it working in vbscript on IE. The ironic part is that it was the code they said would only work on Firefox that gave me the idea to try eval in IE. The window.execScript method they write about for IE was useless to me because I needed a return value.
Tags: Automated-Testing, Functions, HP-Functional-Testing, Javascript, qtp, QTP-9.2, QTP-9.5, QuickTest Pro, QuickTest-Pro-9.2, QuickTest-Pro-9.5


May 28th, 2008 at 3:42 pm
Hi, Thanks for an interesting article. I long suspected it was doable. BTW - I was able to get the example to run in the QTP’s VbScript editor but needed to change one line.
Changed this:
Set JSEntry = oBrowser.object.document.documentelement.parentnode.parentwindow
To this:
Set JSEntry = oBrowser.Page(”micClass:=Page”).object.documentelement.parentnode.parentwindow
Regards,
M.
Reply to mjp
May 28th, 2008 at 4:11 pm
I’m glad this is useful for you, and I am also interested to hear that you needed to add the Page element to get this working.
I have rarely had to use a Page object in QTP, but when I look at other people’s code, I see them using Page all over the place. I have meant to find out what that is all about but have never gotten around to researching it.
Reply to Will Roden
June 3rd, 2008 at 4:54 am This is great!! No idea how it works though. I have looked at ‘eval’ before, but it is a javascript function, not related to vbscript/DOM. How did you come about this?
Reply to Vid
June 3rd, 2008 at 7:58 am
I got the idea from this article at Ajaxian. I saw they were calling eval on a DOM object, and knowing that DOM methods aren’t language dependent, I knew I would be able to do the same thing in vbscript.
What took some effort was figuring out exactly what class had the eval method. I couldn’t get to it using their scripts since they are javascript, so I had to hack around in QTP debug mode for a while. After a couple of hours of trying various DOM paths, I finally hit pay dirt.
Reply to Will Roden
June 3rd, 2008 at 5:06 am
Actually, I was thinking of ‘evaluate’. Still, be interested to hear how you found this.
Reply to Vid
June 3rd, 2008 at 7:29 am I wonder if this has any advantage over : oBrowser.navigate “javascript:void(alert(’Hello, world’));”
Reply to Vid
June 3rd, 2008 at 7:44 am
The main advantage this has over the browser.navigate method is that it can return a value.
I guess with browser.navigate you could get a value by doing an alert then looking for the value in a dialog box, but I haven’t tried that.
Reply to Will Roden
June 3rd, 2008 at 11:04 am A bit confused by this!! QTP only supports I.E DOM? but EVAL does not seem to be supported by I.E (at least not according to MSDN), but it is by Mozilla (http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Functions:eval) However, when I try to use the EVALUATE function (again only mozilla), I get it is not recognised as a method !!
Reply to Vid
June 3rd, 2008 at 11:47 am
Indeed it is confusing. I haven’t found any reference to window.eval on MSDN. It appears to be entirely undocumented.
Are you asking about this because you can’t get it to work in IE or because you are curious how it works?
Reply to Will Roden
June 3rd, 2008 at 1:13 pm It work’s fine for me (I.E 7). In fact, you can shorten the statement to : oBrowser.oPage.object.parentwindow Yeah, just curious. I would really be interested in getting the evaluate method working, as this would lead to xpath support.
Reply to Vid
June 3rd, 2008 at 1:25 pm
I want to be the first hear about it if you get evaluate and xpath working. That will be huge.
Reply to Will Roden
June 4th, 2008 at 8:08 am
I have a way to do this. I need to spend some time testing, then will put a document together.
Reply to Vid
June 5th, 2008 at 5:57 am
Well I haven’t been able to get evaluate to work, but used another method.
My site is still under construction, and need to add some more examples to article, but take a look :
http://www.vidbob.com/qtp_xpath.html
Reply to Vid