/// <summary> /// Waits until the Question's answer value meets the condition. /// If the expected condition is not met within the time limit, then an exception is thrown. /// Returns the actual awaited value. /// </summary> /// <param name="actor">The Screenplay Actor.</param> /// <returns></returns> protected void WaitForValue(IActor actor) { // Set variables bool satisfied = false; ActualTimeout = CalculateTimeout(actor); // Adjust log level if necessary (to avoid too many messages) LogSeverity original = actor.Logger.LowestSeverity; if (SuppressLogs && actor.Logger.LowestSeverity < LogSeverity.Warning) { actor.Logger.LowestSeverity = LogSeverity.Warning; } // Start the timer var timer = new Stopwatch(); timer.Start(); try { // Repeatedly check if the condition is satisfied until the timeout is reached do { satisfied = EvaluateCondition(actor); }while (!satisfied && timer.Elapsed.TotalSeconds < ActualTimeout); } finally { // Return the log level to normal, no matter what goes wrong if (SuppressLogs) { actor.Logger.LowestSeverity = original; } // Stop the timer timer.Stop(); } // Verify successful waiting if (!satisfied) { if (ConditionEvaluators.Count > 1) { throw new WaitingException(this, ConditionEvaluators.Select(c => c.Answer).ToList()); } else { throw ConditionEvaluators[0].WaitingException(this); } } }
/// <summary> /// Add a Question and condition pair to the list of conditions to be evaluated with an Or operator. /// </summary> /// <typeparam name="TAnswer"></typeparam> /// <param name="question"></param> /// <param name="condition"></param> /// <returns></returns> public Wait Or <TAnswer>(IQuestion <TAnswer> question, ICondition <TAnswer> condition) { ConditionEvaluators.Add(new ConditionEvaluator <TAnswer>(question, condition, ConditionOperators.Or)); return(this); }