private string GetMethodName(StepInstance stepInstance, AnalyzedStepText analyzedStepText, StepDefinitionSkeletonStyle style, ProgrammingLanguage language) { var keyword = LanguageHelper.GetDefaultKeyword(stepInstance.StepContext.Language, stepInstance.StepDefinitionType); switch (style) { case StepDefinitionSkeletonStyle.RegexAttribute: return(keyword.ToIdentifier() + string.Concat(analyzedStepText.TextParts.ToArray()).ToIdentifier()); case StepDefinitionSkeletonStyle.MethodNameUnderscores: return(GetMatchingMethodName(keyword, analyzedStepText, stepInstance.StepContext.Language, AppendWordsUnderscored, "_{0}")); case StepDefinitionSkeletonStyle.MethodNamePascalCase: return(GetMatchingMethodName(keyword, analyzedStepText, stepInstance.StepContext.Language, AppendWordsPascalCase, "_{0}_")); case StepDefinitionSkeletonStyle.MethodNameRegex: if (language != ProgrammingLanguage.FSharp) { goto case StepDefinitionSkeletonStyle.MethodNameUnderscores; } return("``" + GetRegex(analyzedStepText) + "``"); default: throw new NotSupportedException(); } }
public void TraceNoMatchingStepDefinition(StepInstance stepInstance, ProgrammingLanguage targetLanguage, System.Globalization.CultureInfo bindingCulture, List <BindingMatch> matchesWithoutScopeCheck) { if (CurrentStepId != null) { var request = new FinishTestItemRequest { Status = Status.Failed, EndTime = DateTime.UtcNow, Issue = new Issue { Type = IssueType.AutomationBug, Comment = "No matching step definition." } }; if (BeforeStepFinished != null) { BeforeStepFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request)); } var message = Bridge.Service.FinishTestItem(CurrentStepId, request).Info; if (AfterStepFinished != null) { AfterStepFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request, message)); } CurrentStepId = null; Bridge.Context.TestId = null; } }
public string GetStepText(StepInstance stepInstance) { StringBuilder result = new StringBuilder(); if (stepInstance.Keyword != null) { result.Append(stepInstance.Keyword); } else { result.Append(LanguageHelper.GetDefaultKeyword(stepInstance.StepContext.Language, stepInstance.StepDefinitionKeyword)); result.Append(" "); } result.AppendLine(stepInstance.Text); if (stepInstance.MultilineTextArgument != null) { result.AppendLine("--- multiline step argument ---".Indent(INDENT)); result.AppendLine(stepInstance.MultilineTextArgument.Indent(INDENT)); } if (stepInstance.TableArgument != null) { result.AppendLine("--- table step argument ---".Indent(INDENT)); result.AppendLine(stepInstance.TableArgument.ToString().Indent(INDENT)); } return(result.ToString()); }
protected virtual BindingMatch GetStepMatch(StepInstance stepInstance) { var match = _stepDefinitionMatchService.GetBestMatch(stepInstance, FeatureContext.BindingCulture, out var ambiguityReason, out var candidatingMatches); if (match.Success) { return(match); } if (candidatingMatches.Any()) { if (ambiguityReason == StepDefinitionAmbiguityReason.AmbiguousSteps) { throw _errorProvider.GetAmbiguousMatchError(candidatingMatches, stepInstance); } if (ambiguityReason == StepDefinitionAmbiguityReason.ParameterErrors) // ambiguouity, because of param error { throw _errorProvider.GetAmbiguousBecauseParamCheckMatchError(candidatingMatches, stepInstance); } } _testTracer.TraceNoMatchingStepDefinition(stepInstance, FeatureContext.FeatureInfo.GenerationTargetLanguage, FeatureContext.BindingCulture, candidatingMatches); _contextManager.ScenarioContext.MissingSteps.Add(stepInstance); throw _errorProvider.GetMissingStepDefinitionError(); }
private StepInstance CreateSimpleThen(string text = "I have something") { var result = new StepInstance(StepDefinitionType.Then, StepDefinitionKeyword.Then, "Then ", text, null, null, new StepContext("MyFeature", "MyScenario", null, new CultureInfo("en-US"))); analizeResult.TextParts.Add(text); return(result); }
public override string GetStepDefinitionSkeleton(StepInstance stepInstance) { List <string> extraArgs = new List <string>(); if (stepInstance.MultilineTextArgument != null) { extraArgs.Add("string multilineText"); } if (stepInstance.TableArgument != null) { extraArgs.Add("Table table"); } StringBuilder result = new StringBuilder(); result.AppendFormat(@"[{0}(@""{1}"")] public void {2}({3}) {{ ScenarioContext.Current.Pending(); }}", stepInstance.BindingType, EscapeRegex(stepInstance.Text), GetStepText(stepInstance).ToIdentifier(), string.Join(", ", extraArgs.ToArray()) ); result.AppendLine(); return(result.ToString()); }
private StepInstance CreateSimpleWhen(string text = "I do something") { var result = new StepInstance(StepDefinitionType.When, StepDefinitionKeyword.When, "When ", text, null, null, new StepContext("MyFeature", "MyScenario", new string[0], new CultureInfo("en-US"))); return(result); }
protected IEnumerable <BindingMatch> GetCandidatingBindings(StepInstance stepInstance, CultureInfo bindingCulture, bool useRegexMatching = true, bool useParamMatching = true, bool useScopeMatching = true) { var matches = bindingRegistry.GetConsideredStepDefinitions(stepInstance.StepDefinitionType, stepInstance.Text).Select(b => Match(b, stepInstance, bindingCulture, useRegexMatching, useParamMatching, useScopeMatching)).Where(b => b.Success); // we remove duplicate maches for the same method (take the highest scope matches from each) matches = matches.GroupBy(m => m.StepBinding.Method, (methodInfo, methodMatches) => methodMatches.OrderByDescending(m => m.ScopeMatches).First(), BindingMethodComparer.Instance); return(matches); }
void ITestTracer.TraceStep(StepInstance stepInstance, bool showAdditionalArguments) { base.TraceStep(stepInstance, showAdditionalArguments); var title = $"{stepInstance.Keyword} {stepInstance.Text}"; AllureAdapter.Instance.StartStep(title); }
public static string GetLabel(this StepInstance stepInstance) { string inFilePositionText = stepInstance.StepContext.ScenarioTitle == null ? "'Background' section" : String.Format("scenario \"{0}\"", stepInstance.StepContext.ScenarioTitle); return(String.Format("\"{0}{1}\" in {2}", stepInstance.Keyword, stepInstance.Text, inFilePositionText)); }
public Exception GetAmbiguousMatchError(List <BindingMatch> matches, StepInstance stepInstance) { string stepDescription = stepFormatter.GetStepDescription(stepInstance); return(new BindingException( string.Format("Ambiguous step definitions found for step '{0}': {1}", stepDescription, string.Join(", ", matches.Select(m => GetMethodText(m.StepBinding.Method)).ToArray())))); }
public void Push(StepInstance stepInstance) { var client = Clients.Client(clientInfo); if (client != null) { client.broadcastMessage(stepInstance); } }
void ITestTracer.TraceNoMatchingStepDefinition(StepInstance stepInstance, ProgrammingLanguage targetLanguage, CultureInfo bindingCulture, List <BindingMatch> matchesWithoutScopeCheck) { TraceNoMatchingStepDefinition(stepInstance, targetLanguage, bindingCulture, matchesWithoutScopeCheck); allure.StopStep(x => x.status = Status.broken); allure.UpdateTestCase(x => { x.status = Status.broken; x.statusDetails = new StatusDetails { message = noMatchingStepMessage }; }); }
public static string GetFileScopedLabel(this StepInstance stepInstance) { var position = stepInstance as ISourceFilePosition; var simpleLabel = stepInstance.GetLabel(); if (position == null) { return(simpleLabel); } return(String.Format("{0} ({1}, line {2})", simpleLabel, position.SourceFile, position.FilePosition.Line)); }
public AnalyzedStepText Analyze(StepInstance stepInstance, CultureInfo bindingCulture) { var result = stepTextAnalyzer.Analyze(stepInstance.Text, bindingCulture); if (stepInstance.MultilineTextArgument != null) { result.Parameters.Add(new AnalyzedStepParameter("String", "multilineText")); } if (stepInstance.TableArgument != null) { result.Parameters.Add(new AnalyzedStepParameter("Table", "table")); } return(result); }
public BindingMatch Match(IStepDefinitionBinding stepDefinitionBinding, StepInstance stepInstance, CultureInfo bindingCulture, bool useRegexMatching = true, bool useParamMatching = true, bool useScopeMatching = true) { if (useParamMatching) { useRegexMatching = true; } //if (stepDefinitionBinding.StepDefinitionType != stepInstance.StepDefinitionType) // return BindingMatch.NonMatching; Match match = null; if (useRegexMatching && stepDefinitionBinding.Regex != null && !(match = stepDefinitionBinding.Regex.Match(stepInstance.Text)).Success) { throw new Exception(String.Format("Error 1: Keyword: {0} Step Def type {1} TEXT: {2}", stepInstance.Keyword, stepInstance.StepDefinitionType, stepInstance.Text)); return(BindingMatch.NonMatching); } int scopeMatches = 0; if (useScopeMatching && stepDefinitionBinding.IsScoped && stepInstance.StepContext != null && !stepDefinitionBinding.BindingScope.Match(stepInstance.StepContext, out scopeMatches)) { throw new Exception(String.Format("Error 2: Keyword: {0} Step Def type {1} TEXT: {2}", stepInstance.Keyword, stepInstance.StepDefinitionType, stepInstance.Text)); return(BindingMatch.NonMatching); } var arguments = match == null ? new object[0] : CalculateArguments(match, stepInstance); if (useParamMatching) { Debug.Assert(match != null); // useParamMatching -> useRegexMatching var bindingParameters = stepDefinitionBinding.Method.Parameters.ToArray(); // check if the regex + extra arguments match to the binding method parameters if (arguments.Length != bindingParameters.Length) { return(BindingMatch.NonMatching); } // Check if regex & extra arguments can be converted to the method parameters //if (arguments.Zip(bindingParameters, (arg, parameter) => CanConvertArg(arg, parameter.Type)).Any(canConvert => !canConvert)) if (arguments.Where((arg, argIndex) => !CanConvertArg(arg, bindingParameters[argIndex].Type, bindingCulture)).Any()) { throw new Exception(String.Format("Error 3: Keyword: {0} Step Def type {1} TEXT: {2}", stepInstance.Keyword, stepInstance.StepDefinitionType, stepInstance.Text)); return(BindingMatch.NonMatching); } } return(new BindingMatch(stepDefinitionBinding, scopeMatches, arguments, stepInstance.StepContext)); }
public void TraceStep(StepInstance stepInstance, bool showAdditionalArguments) { if (CurrentScenario != null) { CurrentScenarioDescription += Environment.NewLine + stepInstance.Keyword + " " + stepInstance.Text; if (stepInstance.MultilineTextArgument != null) { CurrentScenarioDescription += Environment.NewLine + stepInstance.MultilineTextArgument; } var tableDescription = string.Empty; if (stepInstance.TableArgument != null) { tableDescription = string.Empty; foreach (var header in stepInstance.TableArgument.Header) { tableDescription += "| " + header + "\t"; } tableDescription += "|\n"; foreach (var row in stepInstance.TableArgument.Rows) { foreach (var value in row.Values) { tableDescription += "| " + value + "\t"; } tableDescription += "|\n"; } } if (!string.IsNullOrEmpty(tableDescription)) { CurrentScenarioDescription += Environment.NewLine + tableDescription; } var updateScenarioRequest = new UpdateTestItemRequest { Description = CurrentScenarioDescription }; CurrentScenario.Update(updateScenarioRequest); var stepInfoRequest = new AddLogItemRequest { Level = LogLevel.Info, //TODO log time should be greater than test time Time = DateTime.UtcNow.AddMilliseconds(1), Text = string.Format("{0}\r{1}", stepInstance.Keyword + " " + stepInstance.Text, tableDescription) }; CurrentScenario.Log(stepInfoRequest); } }
public void TraceNoMatchingStepDefinition(StepInstance stepInstance, ProgrammingLanguage targetLanguage, CultureInfo bindingCulture, List <BindingMatch> matchesWithoutScopeCheck) { log.Trace(MethodBase.GetCurrentMethod().Name); if (ReportFileInstance.CurrentStep != null) { ReportFileInstance.CurrentStep.Status = TestResultStatus.Failed; ReportFileInstance.CurrentStep.ErrorMessageString = stepInstance.ToString(); // ReportFileInstance.WriteToXml(TestResultsDirectory); } }
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); } }
private object[] CalculateArguments(Match match, StepInstance stepInstance) { var regexArgs = match.Groups.Cast <Group>().Skip(1).Select(g => g.Value); var arguments = regexArgs.Cast <object>().ToList(); if (stepInstance.MultilineTextArgument != null) { arguments.Add(stepInstance.MultilineTextArgument); } if (stepInstance.TableArgument != null) { arguments.Add(stepInstance.TableArgument); } return(arguments.ToArray()); }
public void TraceStep(StepInstance stepInstance, bool showAdditionalArguments) { if (CurrentScenarioId != null) { var description = stepInstance.MultilineTextArgument; if (stepInstance.TableArgument != null) { description = string.Empty; foreach (var header in stepInstance.TableArgument.Header) { description += "| " + header + "	"; } description += "|\n"; foreach (var row in stepInstance.TableArgument.Rows) { foreach (var value in row.Values) { description += "| " + value + "	"; } description += "|\n"; } } var request = new StartTestItemRequest { LaunchId = Bridge.Context.LaunchId, Name = stepInstance.Keyword + " " + stepInstance.Text, Description = description, StartTime = DateTime.UtcNow, Type = TestItemType.Step }; var eventArg = new TestItemStartedEventArgs(Bridge.Service, request); if (BeforeStepStarted != null) { BeforeStepStarted(this, eventArg); } if (!eventArg.Canceled) { CurrentStepId = Bridge.Service.StartTestItem(CurrentScenarioId, request).Id; Bridge.Context.TestId = CurrentStepId; if (AfterStepStarted != null) { AfterStepStarted(this, new TestItemStartedEventArgs(Bridge.Service, request, CurrentStepId)); } } } }
public virtual void Step(StepDefinitionKeyword stepDefinitionKeyword, string keyword, string text, string multilineTextArg, Table tableArg) { StepDefinitionType stepDefinitionType = stepDefinitionKeyword == StepDefinitionKeyword.And || stepDefinitionKeyword == StepDefinitionKeyword.But ? GetCurrentBindingType() : (StepDefinitionType)stepDefinitionKeyword; _contextManager.InitializeStepContext(new StepInfo(stepDefinitionType, text, tableArg, multilineTextArg)); try { var stepInstance = new StepInstance(stepDefinitionType, stepDefinitionKeyword, keyword, text, multilineTextArg, tableArg, _contextManager.GetStepContext()); ExecuteStep(_contextManager, stepInstance); } finally { _contextManager.CleanupStepContext(); } }
protected string GetStepText(StepInstance stepInstance) { string keyword; if (stepInstance.StepDefinitionKeyword == StepDefinitionKeyword.Given || stepInstance.StepDefinitionKeyword == StepDefinitionKeyword.When || stepInstance.StepDefinitionKeyword == StepDefinitionKeyword.Then) { keyword = stepInstance.Keyword; } else { keyword = gherkinDialect.GetStepKeywords((StepKeyword)stepInstance.BindingType).FirstOrDefault(k => !k.StartsWith("*")) ?? ""; } return(keyword + stepInstance.Text); }
public virtual void Step(StepDefinitionKeyword stepDefinitionKeyword, string keyword, string text, string multilineTextArg, Table tableArg) { StepDefinitionType stepDefinitionType = stepDefinitionKeyword == StepDefinitionKeyword.And || stepDefinitionKeyword == StepDefinitionKeyword.But ? GetCurrentBindingType() : (StepDefinitionType)stepDefinitionKeyword; _contextManager.InitializeStepContext(new StepInfo(stepDefinitionType, text, tableArg, multilineTextArg)); _testThreadExecutionEventPublisher.PublishEvent(new StepStartedEvent(FeatureContext, ScenarioContext, _contextManager.StepContext)); try { var stepInstance = new StepInstance(stepDefinitionType, stepDefinitionKeyword, keyword, text, multilineTextArg, tableArg, _contextManager.GetStepContext()); ExecuteStep(_contextManager, stepInstance); } finally { _testThreadExecutionEventPublisher.PublishEvent(new StepFinishedEvent(FeatureContext, ScenarioContext, _contextManager.StepContext)); _contextManager.CleanupStepContext(); } }
public BindingMatch GetBestMatch(StepInstance stepInstance, CultureInfo bindingCulture, out StepDefinitionAmbiguityReason ambiguityReason, out List <BindingMatch> candidatingMatches) { candidatingMatches = GetCandidatingBindingsForBestMatch(stepInstance, bindingCulture).ToList(); KeepMaxScopeMatches(candidatingMatches); ambiguityReason = StepDefinitionAmbiguityReason.None; if (candidatingMatches.Count > 1) { ambiguityReason = StepDefinitionAmbiguityReason.AmbiguousSteps; } else if (candidatingMatches.Count == 0) { ambiguityReason = OnNoMatch(stepInstance, bindingCulture, out candidatingMatches); } if (candidatingMatches.Count == 1 && ambiguityReason == StepDefinitionAmbiguityReason.None) { return(candidatingMatches[0]); } return(BindingMatch.NonMatching); }
public void TraceNoMatchingStepDefinition(StepInstance stepInstance, ProgrammingLanguage targetLanguage, CultureInfo bindingCulture, List <BindingMatch> matchesWithoutScopeCheck) { StringBuilder message = new StringBuilder(); if (matchesWithoutScopeCheck == null || matchesWithoutScopeCheck.Count == 0) { message.AppendLine("No matching step definition found for the step. Use the following code to create one:"); } else { string preMessage = string.Format("No matching step definition found for the step. There are matching step definitions, but none of them have matching scope for this step: {0}.", string.Join(", ", matchesWithoutScopeCheck.Select(m => stepFormatter.GetMatchText(m, null)).ToArray())); traceListener.WriteToolOutput(preMessage); message.AppendLine("Change the scope or use the following code to create a new step definition:"); } message.Append( stepDefinitionSkeletonProvider.GetStepDefinitionSkeleton(targetLanguage, stepInstance, specFlowConfiguration.StepDefinitionSkeletonStyle, bindingCulture) .Indent(StepDefinitionSkeletonProvider.METHOD_INDENT)); traceListener.WriteToolOutput(message.ToString()); }
public override string GetStepDefinitionSkeleton(StepInstance stepInstance) { List <string> extraArgs = new List <string>(); if (stepInstance.MultilineTextArgument != null) { extraArgs.Add("ByVal multilineText As String"); } if (stepInstance.TableArgument != null) { extraArgs.Add("ByVal table As Table"); } StringBuilder result = new StringBuilder(); // in VB "When" and "Then" are language keywords - use the fully qualified namespace string namespaceAddition = "TechTalk.SpecFlow."; if (stepInstance.BindingType == BindingType.Given) { namespaceAddition = ""; } result.AppendFormat(@"<{4}{0}(""{1}"")> _ Public Sub {2}({3}) ScenarioContext.Current.Pending() End Sub", stepInstance.BindingType, EscapeRegex(stepInstance.Text), GetStepText(stepInstance).ToIdentifier(), string.Join(", ", extraArgs.ToArray()), namespaceAddition ); result.AppendLine(); return(result.ToString()); }
protected virtual StepDefinitionAmbiguityReason OnNoMatch(StepInstance stepInstance, CultureInfo bindingCulture, out List <BindingMatch> matches) { /* * //HACK: since out param matching does not support agrument converters yet, we rather show more results than "no match" * matches = GetCandidatingBindings(stepInstance, useParamMatching: false).ToList(); */ // there were either no regex match or it was filtered out by the param/scope matching // to provide better error message for the param matching error, we re-run // the matching without scope and param check var matchesWithoutScopeCheck = GetCandidatingBindings(stepInstance, bindingCulture, useScopeMatching: false).ToList(); if (matchesWithoutScopeCheck.Count > 0) { matches = matchesWithoutScopeCheck; return(StepDefinitionAmbiguityReason.AmbiguousScopes); } Debug.Assert(matchesWithoutScopeCheck.Count == 0); var matchesWithoutParamCheck = GetCandidatingBindings(stepInstance, bindingCulture, useParamMatching: false).ToList(); matches = matchesWithoutParamCheck; if (matchesWithoutParamCheck.Count == 1) { // no ambiguouity, but param error -> execute will find it out return(StepDefinitionAmbiguityReason.None); } if (matchesWithoutParamCheck.Count > 1) // ambiguouity, because of param error { return(StepDefinitionAmbiguityReason.ParameterErrors); } return(StepDefinitionAmbiguityReason.None); // no ambiguouity: simple missing step definition }
public void TraceStep(StepInstance stepInstance, bool showAdditionalArguments) { log.Trace(MethodBase.GetCurrentMethod().Name); if (ReportFileInstance.CurrentScenario != null) { ReportFileInstance.CurrentStep = new StepElement(); ReportFileInstance.CurrentStep.Status = TestResultStatus.Started; string stepTitle = String.Empty; stepTitle += stepInstance.Keyword + " " + stepInstance.Text; if (stepInstance.MultilineTextArgument != null) { stepTitle += stepInstance.MultilineTextArgument; } ReportFileInstance.CurrentStep.Name = stepTitle; ReportFileInstance.CurrentScenario.Steps.Add(ReportFileInstance.CurrentStep); ReportFileInstance.WriteToXml(TestResultsDirectory); } }
public void TraceNoMatchingStepDefinition(StepInstance stepInstance, ProgrammingLanguage targetLanguage, System.Globalization.CultureInfo bindingCulture, List <BindingMatch> matchesWithoutScopeCheck) { if (CurrentScenario != null) { Status = Status.Failed; var errorRequest = new AddLogItemRequest { Level = LogLevel.Error, Time = DateTime.UtcNow.AddMilliseconds(1), Text = "No matching step definition." }; CurrentScenario.Log(errorRequest); var request = new FinishTestItemRequest { Status = Status.Failed, EndTime = DateTime.UtcNow, Issue = new Issue { Type = IssueType.AutomationBug, Comment = "No matching step definition." } }; if (BeforeStepFinished != null) { BeforeStepFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request, null)); } if (AfterStepFinished != null) { AfterStepFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request, null)); } } }