Esempio n. 1
0
        //--------------------------------------------------------------------------------------------------------------------------------------

        private ScenarioResult RunAllteps <TScenario>(
            TScenario scenario,
            IDictionary <int, StepAndAttributeBundle> steps)
        {
            ScenarioResult scenarioResult = new EmptyScenarioResult();

            object[] previousParameters = null;

            var orderedMethods = steps.OrderBy(x => x.Key).ToList();

            for (var index = 0; index < orderedMethods.Count;)
            {
                var method  = orderedMethods[index].Value.MethodInfo;
                var timeout = orderedMethods[index].Value.Timeout;

                ScenarioResult scenarioResultCurrent;
                var            resultBundle = RunScenarioStep(scenario, method, timeout, previousParameters, orderedMethods[index].Value.BusinessStepScenario);


                _logger.Information("-------------------------------------------------------------------------------------------------------------------------------------");
                _logger.Information("-------------------------------------------------------------------------------------------------------------------------------------");
                _logger.Information("-------------------------------------------------------------------------------------------------------------------------------------");


                scenarioResult += resultBundle.ScenarioResult;

                if (resultBundle.ScenarioResult.IsSuccessful == false)
                {
                    if (orderedMethods[index].Value.ExceptionIgnoreAttribute == null)
                    {
                        return(scenarioResult);
                    }

                    scenarioResult |= new EmptyScenarioResult(isSuccessful: true);
                }

                previousParameters = resultBundle.ScenarioStepReturn.Parameters;

                var scenarioStepJumpToStep = resultBundle.ScenarioStepReturn as ScenarioStepJumpToStep;

                if (scenarioStepJumpToStep != null)
                {
                    for (int i = index + 1; i < orderedMethods.Count; i++)
                    {
                        if (orderedMethods[i].Key == scenarioStepJumpToStep.IndexToJumpToStep)
                        {
                            index = i;
                            break;
                        }
                    }
                    //TODO elsewhere  throw exceptions.
                }
                else
                {
                    index++;
                }
            }

            return(scenarioResult);
        }
        //--------------------------------------------------------------------------------------------------------------------------------------

        private ScenarioResult RunScenario <TScenario>(TScenario scenario)
        {
            var            setupStepsResult    = RunAllteps(scenario, _scenarioSetups);
            ScenarioResult businessStepsResult = new EmptyScenarioResult();

            if (setupStepsResult.IsSuccessful)
            {
                businessStepsResult = RunAllteps(scenario, _scenarioSteps);
            }

            var cleanupStepsResult = RunAllteps(scenario, _scenarioCleanups);

            return(setupStepsResult + businessStepsResult + cleanupStepsResult);
        }
        //--------------------------------------------------------------------------------------------------------------------------------------

        private ScenarioResult RunAllteps <TScenario>(
            TScenario scenario,
            IDictionary <int, StepAndAttributeBundle> steps)
        {
            ScenarioResult scenarioResult = new EmptyScenarioResult();

            object[] previousParameters = null;
            var      tasks = new List <Tuple <int, Task <ScenarioStepReturnResultBundle> > >();

            var orderedMethods = steps.OrderBy(x => x.Key).ToList();

            for (var index = 0; index < orderedMethods.Count;)
            {
                var method     = orderedMethods[index].Value.MethodInfo;
                var timeout    = orderedMethods[index].Value.Timeout;
                var isParallel = orderedMethods[index].Value.ScenarioStepParallelAttribute != null;

                ScenarioResult scenarioResultCurrent;
                var            taskScenarioStepBundle = RunScenarioStep(scenario, method, timeout, previousParameters, orderedMethods[index].Value.BusinessStepScenario);

                if (isParallel == true)
                {
                    tasks.Add(new Tuple <int, Task <ScenarioStepReturnResultBundle> >(index++, taskScenarioStepBundle));
                    continue;
                }

                var resultBundle = taskScenarioStepBundle.Result;


                _logger.Information("-------------------------------------------------------------------------------------------------------------------------------------");
                _logger.Information("-------------------------------------------------------------------------------------------------------------------------------------");
                _logger.Information("-------------------------------------------------------------------------------------------------------------------------------------");


                scenarioResult += resultBundle.ScenarioResult;

                if (resultBundle.ScenarioResult.IsSuccessful == false)
                {
                    if (orderedMethods[index].Value.ExceptionIgnoreAttribute == null)
                    {
                        return(scenarioResult);
                    }

                    scenarioResult |= new EmptyScenarioResult(isSuccessful: true);
                }

                previousParameters = resultBundle.ScenarioSStepReturn.Parameters;

                var scenarioStepJumpToStep = resultBundle.ScenarioSStepReturn as ScenarioStepJumpToStep;

                if (scenarioStepJumpToStep != null)
                {
                    for (int i = index + 1; i < orderedMethods.Count; i++)
                    {
                        if (orderedMethods[i].Key == scenarioStepJumpToStep.IndexToJumpToStep)
                        {
                            index = i;
                            break;
                        }
                    }
                    //TODO elsewhere  throw exceptions.
                }
                else
                {
                    index++;
                }
            }

            var pureTasks = tasks.Select(x => x.Item2).ToArray();

            Task.WaitAll(pureTasks);
            foreach (var task in tasks)
            {
                scenarioResult += task.Item2.Result.ScenarioResult;

                if (task.Item2.Result.ScenarioResult.IsSuccessful == false)
                {
                    if (orderedMethods[task.Item1].Value.ExceptionIgnoreAttribute == null)
                    {
                        //return scenarioResult;
                        continue;
                    }

                    scenarioResult |= new EmptyScenarioResult(isSuccessful: true);
                }
            }

            return(scenarioResult);
        }