Forum Discussion

easy-soft's avatar
easy-soft
Occasional Contributor
2 years ago

Errorhandling in JScript KDTOperation

I have a custom KDTOperation written in JScript installed as script extension. The operation is called at the start of my keyword test. The aim of the operation is to terminate a running process of the tested application. 

 

The simplified code looks as follows:

 

function KillProcess()
{
try
{
Sys.Process('my application.exe').Terminate();
}
catch (e)
{
Log.Warning(e.message);
}
}

 

This works fine if the process is running but the test fails when the process is not running: the catch-block is never called even though an exception is thrown in the try part. What am I doing wrong? How can I handle an exception in my own KDTOperations?

6 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If you want to ensure that the application is fully closed i.e. doesn't appear in processes. Then you can implement this for example,

    function Test()
    {
        if (Sys.WaitProcess("notepad", 30000).Exists) {
            Log.Message("Close application here");
            
            // Wait until process no longer exists
            while (Sys.WaitProcess("notepad", 1000).Exists) {
                Log.Message("Waiting...");
                aqUtils.Delay(1000);
            }
        }
    }

    This will wait until the process no longer exists. You can tweak the delays

  • easy-soft's avatar
    easy-soft
    Occasional Contributor

    Thanks for your answer. I have simplified the code a lot. The productive code includes WaitProcess(). But during test execution it happens randomly, that the process is terminated between WaitProcess() and Terminate(). I just want to log a warning in this case and continue with the execution of the test case. 

     

    Is it not possible to call a script function and ignore any errors it throws?

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I'm not sure what you mean by "randomly". The code basically waits for the process, if it exists, then it closes the application.

     

    The Process method returns either process object or null.

     

  • easy-soft's avatar
    easy-soft
    Occasional Contributor

    This seems to get of the path from my question but I'll try to explain. The test suite is run embedded into our build system. Each test case tries to close the application at the end, but that sometimes fails. Thus the next test case has to make sure, that the application is closed before running. Sometimes it happens, that the application is still in the process of closing when the next test case is started. In this case WaitProcess() returns true but Terminate() fails if the final closing of the process falls in-between the two function calls. This doesn't happen too often but every few times the test suite is run. Thus, the test suite fails randomly, depending on whether the server manages to close the application fast enough or not. I could include delay operations at the end of each test but this would significantly increase the overall time to run the test suite. There is already enough waiting for things to happen. But if this is the workaround for not being able to catch an exception then so be it ğŸ˜ž

  • easy-soft's avatar
    easy-soft
    Occasional Contributor

    Thanks I will try to modify my code in the suggested way and let that run for some days. Let's hope that that reduces falsely failed tests.