private static void StartStep(StepInfo stepInfo, string containerId)
        {
            var stepResult = new StepResult
            {
                name = $"{stepInfo.StepDefinitionType} {stepInfo.Text}"
            };

            allure.StartStep(containerId, AllureHelper.NewId(), stepResult);

            if (stepInfo.Table != null)
            {
                var csvFile = $"{Guid.NewGuid().ToString()}.csv";
                using (var csv = new CsvWriter(File.CreateText(csvFile)))
                {
                    foreach (var item in stepInfo.Table.Header)
                    {
                        csv.WriteField(item);
                    }
                    csv.NextRecord();
                    foreach (var row in stepInfo.Table.Rows)
                    {
                        foreach (var item in row.Values)
                        {
                            csv.WriteField(item);
                        }
                        csv.NextRecord();
                    }
                }
                allure.AddAttachment("table", "text/csv", csvFile);
            }
        }
Esempio n. 2
0
        public void LastBeforeScenario()
        {
            // start scenario after last fixture and before the first step to have valid current step context in allure storage
            var scenarioContainer = AllureHelper.GetCurrentTestConainer(scenarioContext);

            AllureHelper.StartTestCase(scenarioContainer.uuid, featureContext, scenarioContext);
        }
 private static void FailScenario(Exception ex)
 {
     allure.UpdateTestCase(
         x =>
     {
         x.status        = (x.status != Status.none) ? x.status : Status.failed;
         x.statusDetails = AllureHelper.GetStatusDetails(ex);
     });
 }
Esempio n. 4
0
        public void FirstAfterScenario()
        {
            var scenarioId = AllureHelper.GetCurrentTestCase(scenarioContext).uuid;

            // update status to passed if there were no step of binding failures
            allure
            .UpdateTestCase(scenarioId,
                            x => x.status = (x.status != Status.none) ? x.status : Status.passed)
            .StopTestCase(scenarioId);
        }
 private void StartFixture(HookBinding hook, string containerId)
 {
     if (hook.HookType.ToString().StartsWith("Before"))
     {
         allure.StartBeforeFixture(containerId, AllureHelper.NewId(), AllureHelper.GetFixtureResult(hook));
     }
     else
     {
         allure.StartAfterFixture(containerId, AllureHelper.NewId(), AllureHelper.GetFixtureResult(hook));
     }
 }
Esempio n. 6
0
        private static void StartStep(StepInstance stepInstance)
        {
            var stepResult = new StepResult()
            {
                name = $"{stepInstance.Keyword} {stepInstance.Text}"
            };

            var table = stepInstance.TableArgument;

            // add step params for 1 row table
            if (table != null && table.RowCount == 1)
            {
                var paramNames = table.Header.ToArray();
                var parameters = new List <Parameter>();
                for (int i = 0; i < table.Header.Count; i++)
                {
                    parameters.Add(new Parameter()
                    {
                        name = paramNames[i], value = table.Rows[0][i]
                    });
                }
                stepResult.parameters = parameters;
            }

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

            // add csv table for multi-row table
            if (table != null && table.RowCount != 1)
            {
                var csvFile = $"{Guid.NewGuid().ToString()}.csv";
                using (var csv = new CsvWriter(File.CreateText(csvFile)))
                {
                    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", csvFile);
            }
        }
        public override object InvokeBinding(IBinding binding, IContextManager contextManager, object[] arguments,
                                             ITestTracer testTracer, out TimeSpan duration)
        {
            var hook = binding as HookBinding;

            // process hook
            if (hook != null)
            {
                var featureContainerId = AllureHelper.GetFeatureContainerId(contextManager.FeatureContext?.FeatureInfo);

                switch (hook.HookType)
                {
                case HookType.BeforeFeature:
                    if (hook.HookOrder == int.MinValue)
                    {
                        // starting point
                        var featureContainer = new TestResultContainer
                        {
                            uuid = AllureHelper.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
                        {
                            this.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 stopped. We need to create, update, stop and write everything here.

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

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

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

                            throw;
                        }
                    }

                case HookType.BeforeStep:
                case HookType.AfterStep:
                {
                    var scenario = AllureHelper.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 = AllureHelper.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 = AllureHelper.GetCurrentTestConainer(contextManager.ScenarioContext);

                        try
                        {
                            this.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)
                        {
                            allure.StopFixture(x => x.status = Status.broken);

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

                            allure.UpdateTestCase(scenario.uuid,
                                                  x =>
                            {
                                x.status        = Status.broken;
                                x.statusDetails = AllureHelper.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 = AllureHelper.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));
                }
            }
            else
            {
                return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
            }
        }
Esempio n. 8
0
 public void FirstBeforeScenario()
 {
     AllureHelper.StartTestContainer(featureContext, scenarioContext);
     //AllureHelper.StartTestCase(scenarioContainer.uuid, featureContext, scenarioContext);
 }
        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;
            bool isTableProcessed = (table == null);

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

                    // convert 2 column table into param-value
                    if (table.Header.Count == 2)
                    {
                        var paramNameMatch  = pluginConfiguration.ParamNameRegex?.IsMatch(header[0]);
                        var paramValueMatch = pluginConfiguration.ParamValueRegex?.IsMatch(header[1]);
                        if (paramNameMatch.HasValue && paramValueMatch.HasValue && paramNameMatch.Value && paramValueMatch.Value)
                        {
                            for (int 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 (int 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(AllureHelper.NewId(), stepResult);

            // add csv table for multi-row table if was not processed as params already
            if (!isTableProcessed)
            {
                using (var sw = new StringWriter())
                    using (var csv = new CsvWriter(sw))
                    {
                        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");
                    }
            }
        }