Forum Discussion

mfoster711's avatar
mfoster711
Regular Contributor
2 months ago

Script to Stop and close TestComplete or TestExecute

In my code, I want to check for a certain condition and if it is true then stop execution of the script and close TestComplete. How can this be done?

I know that the /exit option is available if I started the script from command line. But, I was looking for an option that would work when manually running scripts or running from command line.

I am also aware of aqEnvironment.RebootAndContinue but I don't need to reboot. I just want to exit TestComplete.

18 Replies

  • scot1967's avatar
    scot1967
    Icon for Champion Level 3 rankChampion Level 3

    Nice, I am glad that worked for you.  Have a great day!  🙂

  • mfoster711's avatar
    mfoster711
    Regular Contributor

    I ended up going with the option to user a PowerShell script. My normal script will run the PowerShell script and stop. This leaves TestComplete open but stopped. The PowerShell script pauses for 20 seconds (to ensure TestComplete has stopped) and then forces TestComplete to close. Not the most elegant, but it works.

    • scot1967's avatar
      scot1967
      Icon for Champion Level 3 rankChampion Level 3

      Apologies, my last two posts are at the wrong nested level and were replies to rraghvani.  I think this leveled nesting of comments is confusing.  I like a flat structure using mentions with @....

    • scot1967's avatar
      scot1967
      Icon for Champion Level 3 rankChampion Level 3

      ...

      I forgot about the .close and .terminate methods.  If you can snag the TC process you may be able to use one of these methods.  The terminate method is the more risky of the two but may be the only way.  It just kills the process instead of asking politely to close properly.

    • scot1967's avatar
      scot1967
      Icon for Champion Level 3 rankChampion Level 3

      I added my concern about terminating TC in one of the first post here.  These would be my biggest concerns.  I am not sure if the log file is being lost in the project or not, or if a licensing issue will be seen these are valid points to consider as well as the corruption the project file. I am sure this user has considered the risk.

  • Great thread — lots of solid tips already! 

    Just to sum it up for anyone checking this out later: you can stop a test run with Runner.Stop() or Runner.Halt(), optionally close TestComplete (or TestExecute) using Sys.Process("TestComplete").Terminate() — or call a PowerShell script if you want a cleaner shutdown.

    Here’s a quick example in JavaScript:

    function checkConditionAndExit() {
      if (Project.Variables.MyFlag == true) {
        Log.Message("Condition met — stopping and closing TestComplete...");
        Runner.Stop(true);              // stop execution
      }
    }
    

    I’d still be careful with Terminate() — it’s basically the “pull the plug” option and can skip saving logs or logging out from license. If you’re running from the command line, the /exit flag is still the smoothest way to finish up. 

    🤖 AI-assisted response
    👍 Found it helpful? Click Like
    ✅ Issue resolved? Click Mark as Solution

    • rraghvani's avatar
      rraghvani
      Icon for Champion Level 3 rankChampion Level 3

      I have no idea why your ChatGPT has suggested to terminate the process of TestComplete, and then suggesting to be careful!? If you are running automation, don't terminate the TC process! Also, the remaining lines after Runner.Stop(true) will not get called!

      • Hassan_Ballan's avatar
        Hassan_Ballan
        Icon for Champion Level 3 rankChampion Level 3

        You’re right — suggesting Sys.Process("TestComplete").Terminate() was a mistake. That’s unsafe and any code after Runner.Stop(true) won’t run.

        The correct way is just to use Runner.Stop() or Runner.Halt(), and use the /exit flag from the command line if you want TestComplete/TestExecute to close automatically.

        Thanks for pointing this out!

  • rraghvani's avatar
    rraghvani
    Icon for Champion Level 3 rankChampion Level 3

    Use Runner Object Methods, together with your /exit command-line paremeter. For example,

    function loop()
    {
        for (var i = 0; i < 100; i++) {
            if (i = 50) {
                Log.Message(`Stopping loop at ${i}`);
                Runner.Stop();
            }
        }
        Log.Message("This line is not called!");
    }

     

  • scot1967's avatar
    scot1967
    Icon for Champion Level 3 rankChampion Level 3

    I forgot about the .close and .terminate methods.  If you can snag the TC process you may be able to use one of these methods.  The terminate method is the more risky of the two but may be the only way.  It just kills the process instead of asking politely to close properly.

    Close Action (Process and Browser Objects)
    https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/window-and-process/close-action-process-object.html?sbsearch=process.terminate

     ✌️

  • scot1967's avatar
    scot1967
    Icon for Champion Level 3 rankChampion Level 3

    Stopping the script is easy but closing TC requires a bit more thought.  Have you tried calling a PowerShell script in code your code, say from an error handler like try/catch or from an event?

    Running PowerShell Scripts From TestComplete
    https://support.smartbear.com/testcomplete/docs/testing-with/advanced/using-external-functions/running-powershell-scripts.html?sbsearch=Run%20a%20powershell

    I'll post back it I find something else.

    ... If you find my posts helpful drop me a like! 👍 Be sure to mark or post the solution to help others out and/or to credit the one who helped you. 😎