Forum Discussion

AnneTheAgile's avatar
AnneTheAgile
Contributor
10 years ago
Solved

cannot capture return code from command line in powershell OR batch

I have two trivial tests. zTdd1_Good provides green, passing. zTdd2_Fail provides a very simple red, failure. They both appear to work great, with the small exception that the script at the end of 2_Fail doesn't run due to failing earlier.



I want to capture the return code from the test, ie zero for good and non-zero for failure.



The docs say this should work and in fact point to a list of supported return codes. How can I get this to work?



I want it to work the most in powershell, which is more robust. Since it doesn't work in batch either, it seems like something is grossly wrong?

thank you!



code; in powershell ISE as administrator;



PS C:\windows\system32> & "c:\Program Files (x86)\SmartBear\TestComplete 10\Bin\TestComplete.exe" "c:\Users\MYNAME\Documents\TestComplete 10 Projects\hig4TestProject1\hig4TestProject1.pjs" /run /project:hig4TestProject1 /test:"KeywordTests|zTdd1_Good" /exit



PS C:\windows\system32> echo $LASTEXITCODE

0



PS C:\windows\system32> & "c:\Program Files (x86)\SmartBear\TestComplete 10\Bin\TestComplete.exe" "c:\Users\MYNAME\Documents\TestComplete 10 Projects\hig4TestProject1\hig4TestProject1.pjs" /run /project:hig4TestProject1 /test:"KeywordTests|zTdd2_Fail" /exit



PS C:\windows\system32> echo $LASTEXITCODE

0





code, in Batch VS2013 prompt;



C:\Program Files (x86)\Microsoft Visual Studio 12.0>"c:\Program Files (x86)\Smar

tBear\TestComplete 10\Bin\TestComplete.exe" "c:\Users\MYNAME\Documents\TestCompl

ete 10 Projects\hig4TestProject1\hig4TestProject1.pjs" /run /project:hig4TestPro

ject1 /test:"KeywordTests|zTdd1_Good" /exit



C:\Program Files (x86)\Microsoft Visual Studio 12.0>echo %ERRORLEVEL%

0



C:\Program Files (x86)\Microsoft Visual Studio 12.0>"c:\Program Files (x86)\Smar

tBear\TestComplete 10\Bin\TestComplete.exe" "c:\Users\MYNAME\Documents\TestCompl

ete 10 Projects\hig4TestProject1\hig4TestProject1.pjs" /run /project:hig4TestPro

ject1 /test:"KeywordTests|zTdd2_Fail" /exit



C:\Program Files (x86)\Microsoft Visual Studio 12.0>echo %ERRORLEVEL%

0
  • It should work when using Start-Process ... -PassThru -Wait:



    PS C:\Users\helen> $tc = Start-Process 'C:\Program Files (x86)\SmartBear\TestComplete 10\Bin\TestComplete.exe' '"C:\Users\Public\Documents\TestComplete 10 Samples\Common\Test Log\TestLog.pjs" /run /exit' -PassThru -Wait



    PS C:\Users\helen> $tc.ExitCode

    2



    Sorry I can't be more specific, I don't know PowerShell well.

5 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Anne,



    To get the exit code when running GUI-based apps like TestComplete from the command prompt (not from a batch file/script), you need to add start "" /wait at the beginning of the command, for example:

    start "" /wait "c:\...\TestComplete.exe" "c:\...\Project.pjs" /run /exit


    Without this, the shell doesn't wait for the application to exit and returns the control to the command prompt immediately. In this case, the exit code of 0 means the process was created successfully.



    This is how the Windows command prompt works, and it applies to all GUI-based applications, not just TestComplete.



    Note that start /wait is only needed when using the command prompt; it's not needed when running TestComplete from batch files /scripts.
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    It should work when using Start-Process ... -PassThru -Wait:



    PS C:\Users\helen> $tc = Start-Process 'C:\Program Files (x86)\SmartBear\TestComplete 10\Bin\TestComplete.exe' '"C:\Users\Public\Documents\TestComplete 10 Samples\Common\Test Log\TestLog.pjs" /run /exit' -PassThru -Wait



    PS C:\Users\helen> $tc.ExitCode

    2



    Sorry I can't be more specific, I don't know PowerShell well.
  • fyi, a powershell alternative version [1] that also works;



    PS C:\windows\system32> (Start-Process -FilePath "c:\Program Files (x86)\SmartBear\TestComplete 10\Bin\TestComplete.exe" -ArgumentList '"c:\Users\MYNAME\Documents\TestComplete 10 Projects\hig4TestProject1\hig4TestProject1.pjs" /run /project:hig4TestProject1 /test:"KeywordTests|zTdd1_Good" /exit' -PassThru -Wait).ExitCode

    0



    PS C:\windows\system32> (Start-Process -FilePath "c:\Program Files (x86)\SmartBear\TestComplete 10\Bin\TestComplete.exe" -ArgumentList '"c:\Users\MYNAME\Documents\TestComplete 10 Projects\hig4TestProject1\hig4TestProject1.pjs" /run /project:hig4TestProject1 /test:"KeywordTests|zTdd2_Fail" /exit' -PassThru -Wait).ExitCode

    2



    PS C:\windows\system32> $PSVersionTable.PSVersion



    Major  Minor  Build  Revision

    -----  -----  -----  --------

    4      0      -1     -1   



    1.[] ; ; ; ; ; X.Run MsiExec from PowerShell and get Return Code - Stack Overflow ; ; http://stackoverflow.com/questions/4124409/run-msiexec-from-powershell-and-get-return-code

  • That works great for batch environment! I got the error 2 as desired [1].



    For Powershell I still cannot get it to work? I found some help online [2] but it didn't give any return code - I got a boolean, eg;



    PS C:\windows\system32> $exitCode = [Diagnostics.Process]::Start( "c:\Program Files (x86)\SmartBear\TestComplete 10\Bin\TestComplete.exe" ,'"c:\Users\MYNAME\Documents\TestComplete 10 Projects\hig4TestProject1\hig4TestProject1.pjs" /run /project:hig4TestProject1 /test:"KeywordTests|zTdd1_Good" /exit' ).WaitForExit(60)



    PS C:\windows\system32> echo $exitCode

    False





    1.[] ; ; ;2 => The last test results include errors. ; ; X.TestExecute Exit Codes ; ; http://support.smartbear.com/viewarticle/54696/?utm_source=site-search&utm_medium=search-results&utm_campaign=site-search-c&utm_term=errorlevel



    2.[] ; ; ; ; ; X.How to tell PowerShell to wait for each command to end before starting the next? - Stack Overflow ; ; http://stackoverflow.com/questions/1741490/how-to-tell-powershell-to-wait-for-each-command-to-end-before-starting-the-next/22653584?noredirect=1#comment44269788_22653584