private void StartStep(StepInstance stepInstance)
        {
            var stepResult = new StepResult
            {
                name = $"{stepInstance.Keyword} {stepInstance.Text}"
            };


            // parse MultilineTextArgument
            if (stepInstance.MultilineTextArgument != null)
            {
                allure.AddAttachment(
                    "multiline argument",
                    "text/plain",
                    Encoding.ASCII.GetBytes(stepInstance.MultilineTextArgument),
                    ".txt");
            }

            var table            = stepInstance.TableArgument;
            var isTableProcessed = table == null;

            // parse table as step params
            if (table != null)
            {
                var header = table.Header.ToArray();
                if (pluginConfiguration.stepArguments.convertToParameters)
                {
                    var parameters = new List <Parameter>();

                    // convert 2 column table into param-value
                    if (table.Header.Count == 2)
                    {
                        var paramNameMatch  = Regex.IsMatch(header[0], pluginConfiguration.stepArguments.paramNameRegex);
                        var paramValueMatch =
                            Regex.IsMatch(header[1], pluginConfiguration.stepArguments.paramValueRegex);
                        if (paramNameMatch && paramValueMatch)
                        {
                            for (var i = 0; i < table.RowCount; i++)
                            {
                                parameters.Add(new Parameter {
                                    name = table.Rows[i][0], value = table.Rows[i][1]
                                });
                            }

                            isTableProcessed = true;
                        }
                    }
                    // add step params for 1 row table
                    else if (table.RowCount == 1)
                    {
                        for (var i = 0; i < table.Header.Count; i++)
                        {
                            parameters.Add(new Parameter {
                                name = header[i], value = table.Rows[0][i]
                            });
                        }
                        isTableProcessed = true;
                    }

                    stepResult.parameters = parameters;
                }
            }

            allure.StartStep(PluginHelper.NewId(), stepResult);

            // add csv table for multi-row table if was not processed as params already
            if (isTableProcessed)
            {
                return;
            }
            using var sw  = new StringWriter();
            using var csv = new CsvWriter(sw, CultureInfo.InvariantCulture);
            foreach (var item in table.Header)
            {
                csv.WriteField(item);
            }
            csv.NextRecord();
            foreach (var row in table.Rows)
            {
                foreach (var item in row.Values)
                {
                    csv.WriteField(item);
                }
                csv.NextRecord();
            }

            allure.AddAttachment("table", "text/csv",
                                 Encoding.ASCII.GetBytes(sw.ToString()), ".csv");
        }
Пример #2
0
        public override object InvokeBinding(IBinding binding, IContextManager contextManager, object[] arguments,
                                             ITestTracer testTracer, out TimeSpan duration)
        {
            // process hook
            if (binding is HookBinding hook)
            {
                var featureContainerId = PluginHelper.GetFeatureContainerId(contextManager.FeatureContext?.FeatureInfo);

                switch (hook.HookType)
                {
                case HookType.BeforeFeature:
                    if (hook.HookOrder == int.MinValue)
                    {
                        // starting point
                        var featureContainer = new TestResultContainer
                        {
                            uuid = PluginHelper.GetFeatureContainerId(contextManager.FeatureContext?.FeatureInfo)
                        };
                        allure.StartTestContainer(featureContainer);

                        contextManager.FeatureContext.Set(new HashSet <TestResultContainer>());
                        contextManager.FeatureContext.Set(new HashSet <TestResult>());

                        return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
                    }
                    else
                    {
                        try
                        {
                            StartFixture(hook, featureContainerId);
                            var result = base.InvokeBinding(binding, contextManager, arguments, testTracer,
                                                            out duration);
                            allure.StopFixture(x => x.status = Status.passed);
                            return(result);
                        }
                        catch (Exception ex)
                        {
                            allure.StopFixture(x => x.status = Status.broken);

                            // if BeforeFeature is failed, execution is already stopped. We need to create, update, stop and write everything here.

                            // create fake scenario container
                            var scenarioContainer =
                                PluginHelper.StartTestContainer(contextManager.FeatureContext, null);

                            // start fake scenario
                            var scenario = PluginHelper.StartTestCase(scenarioContainer.uuid,
                                                                      contextManager.FeatureContext, null);

                            // update, stop and write
                            allure
                            .StopTestCase(x =>
                            {
                                x.status        = Status.broken;
                                x.statusDetails = PluginHelper.GetStatusDetails(ex);
                            })
                            .WriteTestCase(scenario.uuid)
                            .StopTestContainer(scenarioContainer.uuid)
                            .WriteTestContainer(scenarioContainer.uuid)
                            .StopTestContainer(featureContainerId)
                            .WriteTestContainer(featureContainerId);

                            throw;
                        }
                    }

                case HookType.BeforeStep:
                case HookType.AfterStep:
                {
                    var scenario = PluginHelper.GetCurrentTestCase(contextManager.ScenarioContext);

                    try
                    {
                        return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
                    }
                    catch (Exception ex)
                    {
                        allure
                        .UpdateTestCase(scenario.uuid,
                                        x =>
                            {
                                x.status        = Status.broken;
                                x.statusDetails = PluginHelper.GetStatusDetails(ex);
                            });
                        throw;
                    }
                }

                case HookType.BeforeScenario:
                case HookType.AfterScenario:
                    if (hook.HookOrder == int.MinValue || hook.HookOrder == int.MaxValue)
                    {
                        return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
                    }
                    else
                    {
                        var scenarioContainer = PluginHelper.GetCurrentTestConainer(contextManager.ScenarioContext);

                        try
                        {
                            StartFixture(hook, scenarioContainer.uuid);
                            var result = base.InvokeBinding(binding, contextManager, arguments, testTracer,
                                                            out duration);
                            allure.StopFixture(x => x.status = Status.passed);
                            return(result);
                        }
                        catch (Exception ex)
                        {
                            var status = ex.GetType().Name.Contains(PluginHelper.IGNORE_EXCEPTION)
                                    ? Status.skipped
                                    : Status.broken;

                            allure.StopFixture(x => x.status = status);

                            // get or add new scenario
                            var scenario = PluginHelper.GetCurrentTestCase(contextManager.ScenarioContext) ??
                                           PluginHelper.StartTestCase(scenarioContainer.uuid,
                                                                      contextManager.FeatureContext, contextManager.ScenarioContext);

                            allure.UpdateTestCase(scenario.uuid,
                                                  x =>
                            {
                                x.status        = status;
                                x.statusDetails = PluginHelper.GetStatusDetails(ex);
                            });
                            throw;
                        }
                    }

                case HookType.AfterFeature:
                    if (hook.HookOrder == int.MaxValue)
                    // finish point
                    {
                        WriteScenarios(contextManager);
                        allure
                        .StopTestContainer(featureContainerId)
                        .WriteTestContainer(featureContainerId);

                        return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
                    }
                    else
                    {
                        try
                        {
                            StartFixture(hook, featureContainerId);
                            var result = base.InvokeBinding(binding, contextManager, arguments, testTracer,
                                                            out duration);
                            allure.StopFixture(x => x.status = Status.passed);
                            return(result);
                        }
                        catch (Exception ex)
                        {
                            var scenario = contextManager.FeatureContext.Get <HashSet <TestResult> >().Last();
                            allure
                            .StopFixture(x => x.status = Status.broken)
                            .UpdateTestCase(scenario.uuid,
                                            x =>
                            {
                                x.status        = Status.broken;
                                x.statusDetails = PluginHelper.GetStatusDetails(ex);
                            });

                            WriteScenarios(contextManager);

                            allure
                            .StopTestContainer(featureContainerId)
                            .WriteTestContainer(featureContainerId);

                            throw;
                        }
                    }

                case HookType.BeforeScenarioBlock:
                case HookType.AfterScenarioBlock:
                case HookType.BeforeTestRun:
                case HookType.AfterTestRun:
                default:
                    return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
                }
            }

            return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
        }