Forum Discussion

nastester's avatar
nastester
Regular Contributor
3 years ago

Batch file closing before completing all steps

I have a batch file in TestedApps which I am executing via a script. 

It is running/opening but seemingly only running the first command I have and then closing and the test is marked as successful. If I run the bat through Windows and not TC, it runs the whole thing fine. 

How can I get TC to run this whole script without stopping/closing? 

  • I was able to solve this issue with this script: 

     

    function Setup() {
      
    //Run bat file from TestedApps
      var app = TestedApps.Items(0);
      app.Run();
      
      Delay(5000);
      Sys.Refresh();
      //Map console object
      var p = NameMapping.Sys.cmd.wndConsoleWindowClass;
      
      while (p.Exists) {
    //while bat is open, keep test running. Looks for text within the console window
        var IsOpen = p.WaitProperty("wText", "*Deleting dashboard records*", 1000);
        if (IsOpen == false)
        break;
        Aliases.cmd.RefreshMappingInfo();
      }
    }

     

  • Here's an answer from an archived script that can be helpful

    https://community.smartbear.com/t5/TestComplete-Questions/How-do-i-run-a-Batch-file-from-testComplete/td-p/119065

     

    from m_essaid 

     

    I use cmd alot now, I create environments for my apps.

     

    for example :

     

    first, I kill all cmd processes

     

    then :

     

    TestedApps.Add('cmd.exe', 'mylocation\mybatchfile.bat', 1, true, '');

    TestedApps.cmd.Run;

     

    Then I wait till the end of the batch execution with this wait procedure

     

    p := Sys.FindChild('ProcessName', 'cmd');
    while p.Exists do
    begin
    IsClosed:= p.WaitProperty('Exists', False);
    if not IsClosed then
    Delay(3000, 'I wait for the end of the files copy');
    p:= Sys.FindChild('ProcessName', 'cmd');
    end;




  • nastester's avatar
    nastester
    Regular Contributor

    I was able to solve this issue with this script: 

     

    function Setup() {
      
    //Run bat file from TestedApps
      var app = TestedApps.Items(0);
      app.Run();
      
      Delay(5000);
      Sys.Refresh();
      //Map console object
      var p = NameMapping.Sys.cmd.wndConsoleWindowClass;
      
      while (p.Exists) {
    //while bat is open, keep test running. Looks for text within the console window
        var IsOpen = p.WaitProperty("wText", "*Deleting dashboard records*", 1000);
        if (IsOpen == false)
        break;
        Aliases.cmd.RefreshMappingInfo();
      }
    }

     

  • It would be good if there is a way to detect if a .cmd file was started by doubleclick in the explorer or if it was started by a already open command line. In the later case (and especially if executed by another program) you dont want the pause

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Lindaaq :

       

      Hi,

       

      if there is a way to detect if a .cmd file was started [...]

      Check if this work for you:

       

      REM Find out if this file was started interactively
      REM %interactive%==0 if started not from cmd window (i.e., e.g. by double-clicking in Explorer)
      set interactive=1
      echo %cmdcmdline% | find /i "%~0" > nul
      if not errorlevel 1 set interactive=0

       

      rem Execute required commands here

       

      :End
      if _%interactive%_==_0_ pause
      exit /b %errorlevel%