Forum Discussion

m1013864's avatar
m1013864
Occasional Contributor
14 years ago

Problem with WSH ActiveX Objects 'Exec' statement on Test Complete 7.52

Hi,



I am having problen in getting the values/object returned by 'Exec' command for a WSH object while using JScript in Test Complete 7.52. The JScript piece of the code is as following;

function test()



    try

    {

        var oWS = new ActiveXObject('WScript.Shell');

        var cmds = oWS.ExpandEnvironmentStrings("%COMSPEC%"); 

        cmds="<Commands>"; //Here a API is called which return a list of available resources of a server//

        var data1 = oWS.Exec(cmds);  //PROBLEM HERE//****//A dos prompt appears on the screen untill the execution is completed.//



        do

        {

            var strResult = data1.StdOut.ReadLine;

            Log.Message(strResult);

        }while(!data1.StdOut.AtEndOfStream)

}

    catch(exception)

    {

        Log.Message(exception.description)

    }

}



The problem here is in the line highlited by ****. The script control is going to the next line that is the begening of 'do' statement before the execution of the command is completed successfully as a result rest of the script generates exception: 'Object does not support this property or method.'



The same function when written in VB script directly on the windows machine not using Test Complete gives the expected result as it waits untill the execution gets completed. A dos prompt appears on the screen untill the execution is in progress and control goes to nxt statement only when the prompt disappears.



The corrosponding VB script code:


dim serverUserName, serverUserPasswd, serverIP


Set oWS = WScript.CreateObject("WScript.Shell")

sDOS_CMD = oWS.ExpandEnvironmentStrings("%COMSPEC%")

sDOS_CMD = "<Command>"

set op= oWS.Exec(sDOS_CMD)  //The script control waits untill the Dos prompt disappears//


Do While Not(op.StdOut.AtEndOfStream)

    strResult = op.StdOut.ReadLine

     WScript.echo strResult

Loop


 

Could anyone please help?

2 Replies

  • Hi Pawan,



    The problem is in this line:



    ReadLine is a method, so you need parenthesis to call it in JScript:





    Also, you probably need to change the post-condition do..while loop to a pre-condition while loop to properly handle situations when the StdOut stream is empty:

    while (!data1.StdOut.AtEndOfStream)

    {

      var strResult = data1.StdOut.ReadLine();

      Log.Message(strResult);

    }