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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//            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.Collections.Generic;
using System.Linq;
 
namespace OpenTap.Plugins.BasicSteps
{
    [Display("Repeat", Group: "OpenTap",Order:1, Description: "Repeats its child steps a fixed number of times or until the verdict of a child step changes to a specified state.")]
    [AllowAnyChild]
    public class RepeatStep : LoopTestStep
    {
        public enum RepeatStepAction
        {
            [Display("Fixed Count", "Repeat iteration a fixed number of times.")]
            Fixed_Count,
            [Display("While", "Repeat while the specified condition is met. This guarantees a minimum of one time.")]
            While,
            [Display("Until", "Repeat until the specified condition is met. This guarantees a minimum of one time.")]
            Until
        }
 
        #region Settings
        [Display("Repeat", Order: 0, Description: "Select if you want to repeat for a fixed number of times or you want to repeat while or until a certain step has a certain verdict.")]
        public RepeatStepAction Action { get; set; }
 
        [StepSelector(StepSelectorAttribute.FilterTypes.All)]
        [EnabledIf(nameof(Action), RepeatStepAction.While, RepeatStepAction.Until, HideIfDisabled = true)]
        [Display("Verdict Of", Order: 1, Description: "Select the step that you want to validate the verdict of.")]
        public ITestStep TargetStep { get; set; }
        
        [EnabledIf("Action", RepeatStepAction.While, RepeatStepAction.Until, HideIfDisabled = true)]
        [Display("Equals", Order: 2, Description: "Set the verdict you want to validate against.")]
        public Verdict TargetVerdict { get; set; }
 
        [EnabledIf("Action",RepeatStepAction.Fixed_Count, HideIfDisabled = true)]
        [Display("Count", Order: 1, Description: "Set the fixed number of times to repeat.")]
        public uint Count { get; set; }
        
        [Display("Retry", Order:3, Description: "Clear the verdict and try again if break conditions were reached for child steps.")]
        public bool Retry { get; set; }
        
        [EnabledIf("Action", RepeatStepAction.While, RepeatStepAction.Until, HideIfDisabled = true)]
        [Display("Clear Verdict", Order:3, Description: "Clear the verdict before each loop iteration. This can be useful for 'repeat until pass' scenarios.")]
        public bool ClearVerdict { get; set; }
 
        public Enabled<uint> maxCount = new Enabled<uint> { Value = 3, IsEnabled = false };
 
        [Display("Max Count", Order: 3, Description: "When enabled the children will only be repeated a maximum number of times.")]
        [EnabledIf("Action", RepeatStepAction.While, RepeatStepAction.Until, HideIfDisabled = true)]
        public Enabled<uint> MaxCount
        {
            get => maxCount;
            set => maxCount = value ?? throw new ArgumentNullException(nameof(value), "Cannot assign Max Count a null value.");
        }
        
        [Output(OutputAvailability.BeforeRun)]
        [Display("Iteration", "The current iteration.", Order: 4)]
        public string IterationInfo
        {
            get
            {
                
                uint _iteration = iteration;
                if(GetParent<TestPlan>().IsRunning == false)
                    _iteration = 0;
 
                if(MaxCount.IsEnabled && Action != RepeatStepAction.Fixed_Count)
                    return $"{_iteration} of {MaxCount.Value}";
                if(Action == RepeatStepAction.Fixed_Count)
                     return $"{_iteration} of {Count}";
                return $"{_iteration}";
            }
        }
 
        [Output(OutputAvailability.BeforeRun)]
        [Display("Iteration", "The current iteration.", Group: "Outputs", Order: 5)]
        public uint Iteration => iteration;
 
        #endregion
 
        public RepeatStep()
        {
            TargetVerdict = Verdict.Fail;
            Action = RepeatStepAction.Fixed_Count;
            Count = 3;
            Rules.Add(() => TargetStep != null || Action == RepeatStepAction.Fixed_Count, "No step selected", "TargetStep");
        }
        uint iteration;
 
        Verdict getCurrentVerdict() => TargetStep?.Verdict ?? Verdict.NotSet;
        private bool retried;
 
        Verdict iterate()
        {
            TapThread.Current.AbortToken.ThrowIfCancellationRequested();
            if (ClearVerdict)
                Verdict = Verdict.NotSet;
            if (retried)
            {
                Verdict = Verdict.NotSet;
                retried = false;
            }
            this.iteration += 1;
            OnPropertyChanged(nameof(IterationInfo));
            var additionalParams = new List<ResultParameter> { new ResultParameter("", "Iteration", this.iteration) };
            var runs = RunChildSteps(additionalParams, BreakLoopRequested, throwOnBreak: false);
            foreach (var r in runs)
            {
                r.WaitForCompletion();
            }
 
            if (runs.LastOrDefault()?.BreakConditionsSatisfied() == true)
            {
                if(!Retry) BreakLoop();
                retried = true;
            }
 
            return getCurrentVerdict();
        }
 
        static bool IsChildOfOrSelf(ITestStep step, ITestStep parent)
        {
            if (step == parent) return true;
            step = step.Parent as ITestStep;
            while(step != null && step != parent)
                step = step.Parent as ITestStep;
            return step != null;
        }
 
        public override void Run()
        {
            base.Run();
            iteration = 0;
            
            if (Action != RepeatStepAction.Fixed_Count && TargetStep == null)
                throw new ArgumentException("Could not locate target test step");
 
            uint loopCount;
            if (Action == RepeatStepAction.Fixed_Count)
                loopCount = Count;
            else if (MaxCount.IsEnabled)
                loopCount = MaxCount.Value;
            else
                loopCount = uint.MaxValue;
 
            {   // Special case:
                // when TargetStep is not a child of this, we need to evaluate the verdict _before_ 
                // running the first iteration.
 
                if (Action != RepeatStepAction.Fixed_Count && !IsChildOfOrSelf(TargetStep, this))
                {
                    var currentVerdict = getCurrentVerdict();
                    if (currentVerdict != TargetVerdict && Action == RepeatStepAction.While)
                        return;
                    if (currentVerdict == TargetVerdict && Action == RepeatStepAction.Until)
                        return;
                }
            }
            
 
            switch (Action)
            {
                case RepeatStepAction.While:
                {
                    while (iterate() == TargetVerdict && iteration < loopCount && !BreakLoopRequested.IsCancellationRequested) { }
 
                    break;
                }
                case RepeatStepAction.Until:
                {
                    while (iterate() != TargetVerdict && iteration < loopCount && !BreakLoopRequested.IsCancellationRequested) { }
 
                    break;
                }
                case RepeatStepAction.Fixed_Count:
                {
                    while (iteration < loopCount && !BreakLoopRequested.IsCancellationRequested) 
                        iterate();
                    break;
                }
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }
}