chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.ComponentModel;
 
namespace OpenTap.Plugins.BasicSteps
{
    [Display("If Verdict", Group: "OpenTap", Description: "Runs its child steps only when the verdict of another step has a specific value.", Order:1)]
    [AllowAnyChild]
    public class IfStep : TestStep
    {
        public enum IfStepAction
        {
            [Display("Run Children")]
            RunChildren,
            [Display("Break Loop", "Break out of the current loop")]
            BreakLoop,
            [Display("Continue Loop", "Skip steps until the parent loop step regains control.")]
            ContinueLoop,
            [Display("Abort Test Plan")]
            AbortTestPlan,
            [Display("Wait For User")]
            WaitForUser
        }
 
        #region Settings
        [Display("If", Order: 1)]
        public Input<Verdict> InputVerdict { get; set; }
        [Display("Equals", Order: 2)]
        public Verdict TargetVerdict { get; set; }
        [Display("Then", Order: 3)]
        public IfStepAction Action { get; set; }
        #endregion
 
        public IfStep()
        {
            InputVerdict = new Input<Verdict>();
            Rules.Add(() => InputVerdict.Step != null, "Input property must be set.", nameof(InputVerdict));
            Rules.Add(() => Action != IfStepAction.ContinueLoop || GetParent<LoopTestStep>() != null, "Continue Loop only works when this step is a child of a loop-type step, e.g Repeat or Sweep Steps.", nameof(Action));
            Rules.Add(() => Action != IfStepAction.BreakLoop || GetParent<LoopTestStep>() != null, "Break Loop only works when this step is a child of a loop-type step, e.g Repeat or Sweep Steps.", nameof(Action));
        }
 
        
        class Request
        {
            public string Name => "Waiting for user input";
            [Browsable(true)]
            [Layout(LayoutMode.FullRow)]
            public string Message { get; private set; } = "Continue?";
            [Submit]
            [Layout(LayoutMode.FloatBottom | LayoutMode.FullRow)]
 
            public WaitForInputResult1 Response { get; set; } = WaitForInputResult1.Yes;
        }
 
        public override void Run()
        {
            // Get the targetStep
            if (InputVerdict == null)
                throw new ArgumentException("Could not locate target test step");
            
            if (InputVerdict.Value == TargetVerdict)
            {
                switch (Action)
                {
                    case IfStepAction.RunChildren:
                        Log.Info("Condition is true, running childSteps");
                        RunChildSteps();
                        break;
                    case IfStepAction.AbortTestPlan:
                        Log.Info("Condition is true, aborting TestPlan run.");
                        string msg = String.Format("TestPlan aborted by \"If\" Step ({2} of {0} was {1})", InputVerdict.Step.Name, InputVerdict.Value, InputVerdict.PropertyName);
                        PlanRun.MainThread.Abort();
                        break;
                    case IfStepAction.ContinueLoop:
                        StepRun.SuggestedNextStep = GetParent<LoopTestStep>()?.Id;
                        if (StepRun.SuggestedNextStep != null)
                            Log.Info("Condition is true, jumping to next loop iteration.");
                        else
                            Log.Error("Condition is true, but no loop parent step was found.");
                        break;
                    case IfStepAction.WaitForUser:
                        Log.Info("Condition is true, waiting for user input.");
                        var req = new Request();
                        UserInput.Request(req, false);
                        if (req.Response == WaitForInputResult1.No)
                        {
                            Log.Debug("User requested to end test plan execution. Aborting test plan run.");
                            PlanRun.MainThread.Abort();
                        }
                        break;
                    case IfStepAction.BreakLoop:
                        
                        var loopStep = GetParent<LoopTestStep>();
                        if(loopStep != null)
                        {
                            Log.Info("Condition is true, breaking loop.");
                            loopStep.BreakLoop();
                        }else{
                            Log.Error("Condition is true, but no loop parent step was found.");
                        }
                        break;
                    default:
                        throw new NotImplementedException();
                }
            }
            else
            {
                Log.Info("Condition is false.");
            }
        }
 
    }
}