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.

Visual Basic:
  1. Public Function evalJS(oBrowser, sJavaScript)
  2.     Set JSEntry = oBrowser.object.document.documentelement.parentnode.parentwindow
  3.     On Error Resume Next
  4.     evalJS = JSEntry.eval(sJavaScript)
  5.     On Error Goto 0
  6. End Function
  7.  
  8. Set oBrowser = Browser("version:=inter.*")
  9. 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:

Visual Basic:
  1. sJSFunction = "function alertThis(sThis){ alert(sThis) }"
  2. evalJS oBrowser, sJSFunction
  3. 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.

JavaScript:
  1. function alertThis (sAlertText) {
  2.     alert(sAlertText);
  3. }
  4.  
  5. function isItOne (iNumber) {
  6.     var bReturn = false;
  7.     if (iNumber == 1)
  8.     {
  9.         bReturn = true;
  10.     }
  11.     return bReturn
  12. }

Now run this in QTP. Make sure evalJS is available too.

Visual Basic:
  1. Function loadJsLib ( oBrowser, sLibPath )
  2.    Set fso = CreateObject("Scripting.FileSystemObject")
  3.    Set f = fso.OpenTextFile(sLibPath, 1)
  4.    sLibText = f.ReadAll
  5.    loadJsLib = evalJS( oBrowser, sLibText)
  6. End Function
  7.  
  8. LoadJsLib oBrowser, "c:\jsdemo.js"
  9. 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:

Visual Basic:
  1. 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: , , , , , , , , ,

13 Comments for “Execute Javascript from QuickTest Pro”

  1. mjp Says:

    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

  2. Will Roden Says:

    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

  3. Vid Says:
    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

    1. Will Roden Says:

      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

  4. Vid Says:

    Actually, I was thinking of ‘evaluate’. Still, be interested to hear how you found this.

    Reply to Vid

  5. Vid Says:
    I wonder if this has any advantage over : oBrowser.navigate “javascript:void(alert(’Hello, world’));”

    Reply to Vid

    1. Will Roden Says:

      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

  6. Vid Says:
    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

    1. Will Roden Says:

      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

  7. Vid Says:
    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

    1. Will Roden Says:

      I want to be the first hear about it if you get evaluate and xpath working. That will be huge.

      Reply to Will Roden

  8. Vid Says:

    I have a way to do this. I need to spend some time testing, then will put a document together.

    Reply to Vid

  9. Vid Says:

    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

Leave a Reply