End all the running process and threads and stop the test from going to next step

Hi , I am using Open tap in my application and when I am giving “OpenTap.TapThread.ThrowIfAborted();” in C# in one of my plugins, it stop that particular plugin but then moves on to next step. How to implement if we want all the running process and threads to stop if it reaches the loop where iam using “OpenTap.TapThread.ThrowIfAborted();”.
Please advise

1 Like

Hi @sam836,

This kind of thing is generally handled by Break Conditions. Probably you just need to set break conditions to Break on Error for the parent step or for the test plan in general.

There are 3 different places you can set this:

  • Engine Settings
  • Test Plan Settings (inherits Engine setting, overrides Engine setting if set)
  • Test Step Settings (inherits Test Plan, Engine or parent step setting, overrides if set)

How to do engine Settings? I have developed my own GUI in C# using OpenTap. Now in that i want to implement that whenever stop button is pressed it should kill all threads and processes . Even after setting break condition to “break on Error” it moves on to next step.
Can you please advise

Hi @sam836 ,

You can register a cancellation token to abort the test plan run.

Before running test plan create a CancellationTokenSource and register it.

CancellationTokenSource _abortTokenSource = new CancellationTokenSource();
var cancellationToken = _abortTokenSource.Token;
cancellationToken.Register(TapThread.Current.Abort);
// Execute plan....

To abort, call this method in your button click event or command:

_abortTokenSource.Cancel();
2 Likes

I used this.PlanRun.MainThread.Abort(); to abort the tap plan.

1 Like

Hi justin,

I tried this in my code and getting an exception "Object reference not set to an instance of an object.’ Any other suggestion please.

I have used this code inside Run function of a test step. Have you used inside a Test step or some place else?

Using it Inside test step. Is there any way to catch opentap exception generated due to this? with normal catch(Exception e) exception message is not very clear

Hi Sam,

I understand like now your are able to Abort the tap plan using this.PlanRun.MainThread.Abort(); ? Is that correct? Another way is just throw an exception from Test Step.

  this.Verdict = Verdict.Aborted;
  throw new Exception("Abort");

If you are using an External GUI and in the stop button, you need to abort the tap plan

you can try

For running the tap pan

 cancellationToken = new CancellationTokenSource();
 myTestPlan.TestPlan.ExecuteAsync(resultListeners, null, null, cancellationToken.Token);

For stopping the tap plan
cancellationToken.Cancel();

Note: This steps are not Open TAP suggested ways, this is all work around methods that we use in our plugins.

In my curerent implementation I am using cancellation token for stop button. but when i press stop button it only abort that particular test and continues to next step.
However, adding this.PlanRun.MainThread.Abort is actually stopping my test but throws an exception and cause GUI to close.

Hi Sam,

In the Stop Button How did you call this.PlanRun.MainThread.Abort() ? Or are are your trying

        public void AbortTapPlan()
        {
            try
            {

                myTestPlan.TestPlan.Steps.FirstOrDefault().PlanRun.MainThread.Abort();
            }
            catch
            {
                
            }
        }

Hi justin,

I am calling “this.PlanRun.MainThread.Abort()” from my teststep plugin which i want to abort. So I have multiple plugins and if it works i will add this.PlanRun.MainThread.Abort() in all plugins. I have added this in my Run function -

try
{
//test steps here
if (StopButtonStatus)
{
Log.Info(“Test Aborted”);
this.PlanRun.MainThread.Abort();
this.Verdict = Verdict.Aborted;
}
}
catch()
{
// catch exception
}

It is always going to catch block.

Now in my stop button function Iam only setting the status of stop button to true when it is clicked.

private void Button_Stop_Click(object sender, RoutedEventArgs e)
{
try
{
//Stop the stop watch
sw.Stop();
SetTestStatus(“ABORTED”);

            m.StopTestPlan();              
        }

In StopTestPlan - setting StopButtonStatus to true.

and after catch block your external GUI get exited now?

No, it throws this exception -
“TestPlan ; Error ; Error running “Wait 30 seconds”: Object reference not set to an instance of an object…
00:00:44.534738 ; TestPlan ; Debug ; NullReferenceException: Object reference not set to an instance of an object.
00:00:44.557947 ; TestPlan ; Debug ; at Mavenir.Plugins.Misc.Functions.Wait.Run()
00:00:44.557947 ; TestPlan ; Debug ; at OpenTap.TestStepExtensions.DoRun(ITestStep Step, TestPlanRun planRun, TestRun parentRun, IEnumerable1 attachedParameters) 00:00:44.560925 ; TestPlan ; Debug ; Exception caught at: 00:00:44.568762 ; TestPlan ; Debug ; at failState execTestPlan(OpenTap.TestPlanRun, System.Collections.Generic.IList1[OpenTap.ITestStep])
00:00:44.568762 ; TestPlan ; Debug ; at OpenTap.TestPlanRun DoExecute(System.Collections.Generic.IEnumerable1[OpenTap.IResultListener], System.Collections.Generic.IEnumerable1[OpenTap.ResultParameter], System.Collections.Generic.HashSet1[OpenTap.ITestStep]) 00:00:44.568762 ; TestPlan ; Debug ; at OpenTap.TestPlanRun executeInContext(System.Collections.Generic.IEnumerable1[OpenTap.IResultListener], System.Collections.Generic.IEnumerable1[OpenTap.ResultParameter], System.Collections.Generic.HashSet1[OpenTap.ITestStep])
00:00:44.568762 ; TestPlan ; Debug ; at Void b__0()
00:00:44.568762 ; TestPlan ; Debug ; …
00:00:44.568762 ; TestPlan ; Information ; “Wait 30 seconds” completed with verdict ‘Error’. [30.0 s]
00:00:44.569762 ; TestStep ; Debug ; Break issued from ‘Wait 30 seconds’ due to verdict Error. See Break Conditions settings.”

Strange error… I have not seen this error, any result Listeners added? if yes just check events of the result listeners too. I was not able to find the root cause for this error.

Recommendation: Cancellation Token will be the best option to Abort the tap plan from external GUI.

@rolf_madsen : I have attempted this question because , I have done Stop button in our sample UI application, also in multiple test steps ( If ,For loop conditions) we have aborted the tap plan., I could not solve this issue. Please help.

The exception comes from here:

I don’t know what this plugin does, but it seems like it does not handle being aborted correctly.

Hi rolf,

Yes, I have added the abort in this wait function to verify. but since I am having difficulty in adding abort correctly that’s why the exception.
As soon as my program reach at “this.PlanRun.MainThread.Abort();” it goes to catch block and print error exception. How can i handle the abort event correctly?

This is a bit hard to say since I don’t have an overview of the code.

Is there a risk that the MainThread is the application main thread and you are aborting it from a child thread?

In that case you need to create a ‘virtual’ thread, otherwise you will be aborting your application thread.

e.g:

TapThread.WithNewContext(() =>
            {
                plan.Execute();
            });

Otherwise maybe you can create and share a small sample application which demonstrates your problem?