Best Gherkin-dotnet code snippet using Gherkin.CucumberMessages.Types.Step
AstMessagesConverter.cs
Source:AstMessagesConverter.cs
...8using Examples = Gherkin.CucumberMessages.Types.Examples;9using Feature = Gherkin.CucumberMessages.Types.Feature;10using Location = Gherkin.CucumberMessages.Types.Location;11using Rule = Gherkin.CucumberMessages.Types.Rule;12using Step = Gherkin.CucumberMessages.Types.Step;13using DataTable = Gherkin.CucumberMessages.Types.DataTable;14using DocString = Gherkin.CucumberMessages.Types.DocString;15using GherkinDocument = Gherkin.CucumberMessages.Types.GherkinDocument;16using Scenario = Gherkin.CucumberMessages.Types.Scenario;17using TableCell = Gherkin.CucumberMessages.Types.TableCell;18using TableRow = Gherkin.CucumberMessages.Types.TableRow;19using Tag = Gherkin.CucumberMessages.Types.Tag;20namespace Gherkin.CucumberMessages21{22 public class AstMessagesConverter23 {24 private readonly IIdGenerator _idGenerator;25 public AstMessagesConverter(IIdGenerator idGenerator)26 {27 _idGenerator = idGenerator;28 }29 public GherkinDocument ConvertGherkinDocumentToEventArgs(Ast.GherkinDocument gherkinDocument, string sourceEventUri)30 {31 return new GherkinDocument()32 {33 Uri = sourceEventUri,34 Feature = ConvertFeature(gherkinDocument),35 Comments = ConvertComments(gherkinDocument)36 };37 }38 private IReadOnlyCollection<Comment> ConvertComments(Ast.GherkinDocument gherkinDocument)39 {40 return gherkinDocument.Comments.Select(c =>41 new Comment()42 {43 Text = c.Text,44 Location = ConvertLocation(c.Location)45 }).ToReadOnlyCollection();46 }47 private Feature ConvertFeature(Ast.GherkinDocument gherkinDocument)48 {49 var feature = gherkinDocument.Feature;50 if (feature == null)51 {52 return null;53 }54 var children = feature.Children.Select(ConvertToFeatureChild).ToReadOnlyCollection();55 var tags = feature.Tags.Select(ConvertTag).ToReadOnlyCollection();56 return new Feature()57 {58 Name = CucumberMessagesDefaults.UseDefault(feature.Name, CucumberMessagesDefaults.DefaultName),59 Description = CucumberMessagesDefaults.UseDefault(feature.Description, CucumberMessagesDefaults.DefaultDescription),60 Keyword = feature.Keyword,61 Language = feature.Language,62 Location = ConvertLocation(feature.Location),63 Children = children,64 Tags = tags65 };66 }67 private static Location ConvertLocation(Ast.Location location)68 {69 return new Location(location.Column, location.Line);70 }71 private FeatureChild ConvertToFeatureChild(IHasLocation hasLocation)72 {73 var tuple = ConvertToChild(hasLocation);74 return new FeatureChild(tuple.Item3, tuple.Item1, tuple.Item2);75 }76 77 private RuleChild ConvertToRuleChild(IHasLocation hasLocation)78 {79 var tuple = ConvertToChild(hasLocation);80 return new RuleChild(tuple.Item1, tuple.Item3);81 }82 83 private Tuple<Background, Rule, Scenario> ConvertToChild(IHasLocation hasLocation)84 {85 switch (hasLocation)86 {87 case Gherkin.Ast.Background background:88 var backgroundSteps = background.Steps.Select(ConvertStep).ToList();89 return new Tuple<Background, Rule, Scenario>(new Background90 {91 Id = _idGenerator.GetNewId(),92 Location = ConvertLocation(background.Location),93 Name = CucumberMessagesDefaults.UseDefault(background.Name, CucumberMessagesDefaults.DefaultName),94 Description = CucumberMessagesDefaults.UseDefault(background.Description, CucumberMessagesDefaults.DefaultDescription),95 Keyword = background.Keyword,96 Steps = backgroundSteps97 }, null, null);98 case Ast.Scenario scenario:99 var steps = scenario.Steps.Select(ConvertStep).ToList();100 var examples = scenario.Examples.Select(ConvertExamples).ToReadOnlyCollection();101 var tags = scenario.Tags.Select(ConvertTag).ToReadOnlyCollection();102 return new Tuple<Background, Rule, Scenario>(null, null, new Scenario()103 {104 Id = _idGenerator.GetNewId(),105 Keyword = scenario.Keyword,106 Location = ConvertLocation(scenario.Location),107 Name = CucumberMessagesDefaults.UseDefault(scenario.Name, CucumberMessagesDefaults.DefaultName),108 Description = CucumberMessagesDefaults.UseDefault(scenario.Description, CucumberMessagesDefaults.DefaultDescription),109 Steps = steps,110 Examples = examples,111 Tags = tags112 });113 case Ast.Rule rule:114 {115 var ruleChildren = rule.Children.Select(ConvertToRuleChild).ToReadOnlyCollection();116 var ruleTags = rule.Tags.Select(ConvertTag).ToReadOnlyCollection();117 return new Tuple<Background, Rule, Scenario>(null, new Rule118 {119 Id = _idGenerator.GetNewId(),120 Name = CucumberMessagesDefaults.UseDefault(rule.Name, CucumberMessagesDefaults.DefaultName),121 Description = CucumberMessagesDefaults.UseDefault(rule.Description, CucumberMessagesDefaults.DefaultDescription),122 Keyword = rule.Keyword,123 Children = ruleChildren,124 Location = ConvertLocation(rule.Location),125 Tags = ruleTags126 }, null);127 }128 default:129 throw new NotImplementedException();130 }131 }132 private Examples ConvertExamples(Ast.Examples examples)133 {134 var header = ConvertTableHeader(examples);135 var body = ConvertToTableBody(examples);136 var tags = examples.Tags.Select(ConvertTag).ToReadOnlyCollection();137 return new Examples()138 {139 Id = _idGenerator.GetNewId(),140 Name = CucumberMessagesDefaults.UseDefault(examples.Name, CucumberMessagesDefaults.DefaultName),141 Keyword = examples.Keyword,142 Description = CucumberMessagesDefaults.UseDefault(examples.Description, CucumberMessagesDefaults.DefaultDescription),143 Location = ConvertLocation(examples.Location),144 TableHeader = header,145 TableBody = body,146 Tags = tags147 };148 }149 private IReadOnlyCollection<TableRow> ConvertToTableBody(Ast.Examples examples)150 {151 if (examples.TableBody == null)152 return new List<TableRow>();153 return ConvertToTableRow(examples.TableBody);154 }155 private IReadOnlyCollection<TableRow> ConvertToTableRow(IEnumerable<Gherkin.Ast.TableRow> rows)156 {157 return rows.Select(b =>158 new TableRow159 {160 Id = _idGenerator.GetNewId(),161 Location = ConvertLocation(b.Location),162 Cells = b.Cells.Select(ConvertCell).ToReadOnlyCollection()163 }).ToReadOnlyCollection();164 }165 private TableRow ConvertTableHeader(Ast.Examples examples)166 {167 if (examples.TableHeader == null)168 return null;169 return new TableRow170 {171 Id = _idGenerator.GetNewId(),172 Location = ConvertLocation(examples.TableHeader.Location),173 Cells = examples.TableHeader.Cells.Select(ConvertCell).ToReadOnlyCollection()174 };175 }176 private Tag ConvertTag(Ast.Tag tag)177 {178 return new Tag179 {180 Id = _idGenerator.GetNewId(),181 Location = ConvertLocation(tag.Location),182 Name = tag.Name183 };184 }185 private TableCell ConvertCell(Ast.TableCell c)186 {187 return new TableCell()188 {189 Value = CucumberMessagesDefaults.UseDefault(c.Value, CucumberMessagesDefaults.DefaultCellValue),190 Location = ConvertLocation(c.Location)191 };192 }193 private Step ConvertStep(Ast.Step step)194 {195 DataTable dataTable = null;196 if (step.Argument is Gherkin.Ast.DataTable astDataTable) 197 {198 var rows = ConvertToTableRow(astDataTable.Rows);199 dataTable = new DataTable200 {201 Rows = rows,202 Location = ConvertLocation(astDataTable.Location)203 };204 }205 DocString docString = null;206 if (step.Argument is Gherkin.Ast.DocString astDocString) 207 {208 docString = new DocString209 {210 Content = astDocString.Content,211 MediaType = astDocString.ContentType,212 Delimiter = astDocString.Delimiter ?? "\"\"\"", //TODO: store DocString delimiter in Gherkin AST213 Location = ConvertLocation(astDocString.Location)214 };215 }216 return new Step()217 {218 Id = _idGenerator.GetNewId(),219 Keyword = step.Keyword,220 Text = step.Text,221 DataTable = dataTable,222 DocString = docString,223 Location = ConvertLocation(step.Location)224 };225 }226 }227}...
PickleCompiler.cs
Source:PickleCompiler.cs
...21 return pickles;22 }23 var language = feature.Language;24 var tags = feature.Tags;25 BuildFeature(pickles, language, tags, Enumerable.Empty<PickleStep>, feature.Children, gherkinDocument.Uri);26 return pickles;27 }28 protected virtual void BuildFeature(List<Pickle> pickles, string language, IEnumerable<Tag> tags,29 Func<IEnumerable<PickleStep>> backgroundStepsFactory, IEnumerable<FeatureChild> children,30 string gherkinDocumentUri) 31 {32 if (children == null)33 return;34 foreach (var child in children)35 {36 if (child.Background != null)37 {38 backgroundStepsFactory = BuildBackground(child.Background, backgroundStepsFactory);39 }40 else if (child.Rule != null)41 {42 var mergedRuleTags = tags.Concat(child.Rule.Tags);43 BuildRule(pickles, language, mergedRuleTags, backgroundStepsFactory, child.Rule.Children, gherkinDocumentUri);44 }45 else if (child.Scenario != null)46 {47 BuildScenario(pickles, language, tags, backgroundStepsFactory, gherkinDocumentUri, child.Scenario);48 }49 }50 }51 protected virtual void BuildRule(List<Pickle> pickles, string language, IEnumerable<Tag> tags,52 Func<IEnumerable<PickleStep>> backgroundStepsFactory, IEnumerable<RuleChild> children,53 string gherkinDocumentUri) 54 {55 if (children == null)56 return;57 foreach (var child in children)58 {59 if (child.Background != null)60 {61 backgroundStepsFactory = BuildBackground(child.Background, backgroundStepsFactory);62 }63 else if (child.Scenario != null)64 {65 BuildScenario(pickles, language, tags, backgroundStepsFactory, gherkinDocumentUri, child.Scenario);66 }67 }68 }69 private Func<IEnumerable<PickleStep>> BuildBackground(Background background, Func<IEnumerable<PickleStep>> backgroundStepsFactory)70 {71 var previousFactory = backgroundStepsFactory;72 backgroundStepsFactory = () => previousFactory().Concat(PickleSteps(background.Steps));73 return backgroundStepsFactory;74 }75 private void BuildScenario(List<Pickle> pickles, string language, IEnumerable<Tag> tags, Func<IEnumerable<PickleStep>> backgroundStepsFactory, string gherkinDocumentUri, Scenario scenario)76 {77 if (!scenario.Examples.Any())78 {79 CompileScenario(pickles, backgroundStepsFactory, scenario, tags, language, gherkinDocumentUri);80 }81 else82 {83 CompileScenarioOutline(pickles, backgroundStepsFactory, scenario, tags, language, gherkinDocumentUri);84 }85 }86 protected virtual void CompileScenario(List<Pickle> pickles,87 Func<IEnumerable<PickleStep>> backgroundStepsFactory, Scenario scenario, IEnumerable<Tag> featureTags,88 string language, string gherkinDocumentUri)89 {90 var steps = new List<PickleStep>();91 if (scenario.Steps.Any())92 steps.AddRange(backgroundStepsFactory());93 var scenarioTags = new List<Tag>();94 scenarioTags.AddRange(featureTags);95 scenarioTags.AddRange(scenario.Tags);96 steps.AddRange(PickleSteps(scenario.Steps));97 Pickle pickle = new Pickle(98 _idGenerator.GetNewId(),99 gherkinDocumentUri,100 scenario.Name,101 language,102 steps,103 PickleTags(scenarioTags),104 new []{ scenario.Id }105 );106 pickles.Add(pickle);107 }108 protected virtual void CompileScenarioOutline(List<Pickle> pickles,109 Func<IEnumerable<PickleStep>> backgroundStepsFactory, Scenario scenarioOutline,110 IEnumerable<Tag> featureTags, string language, string gherkinDocumentUri)111 {112 foreach (var examples in scenarioOutline.Examples)113 {114 if (examples.TableHeader == null) continue;115 var variableCells = examples.TableHeader.Cells;116 foreach (var values in examples.TableBody)117 {118 var valueCells = values.Cells;119 var steps = new List<PickleStep>();120 if (scenarioOutline.Steps.Any())121 steps.AddRange(backgroundStepsFactory());122 var tags = new List<Tag>();123 tags.AddRange(featureTags);124 tags.AddRange(scenarioOutline.Tags);125 tags.AddRange(examples.Tags);126 foreach(var scenarioOutlineStep in scenarioOutline.Steps)127 {128 string stepText = Interpolate(scenarioOutlineStep.Text, variableCells, valueCells);129 PickleStep pickleStep = CreatePickleStep(130 scenarioOutlineStep,131 stepText,132 CreatePickleArgument(scenarioOutlineStep, variableCells, valueCells),133 new[] { scenarioOutlineStep.Id, values.Id }134 );135 steps.Add(pickleStep);136 }137 Pickle pickle = new Pickle(138 _idGenerator.GetNewId(),139 gherkinDocumentUri,140 Interpolate(scenarioOutline.Name, variableCells, valueCells),141 language, 142 steps,143 PickleTags(tags),144 new[] { scenarioOutline.Id, values.Id }145 );146 pickles.Add(pickle);147 }148 }149 }150 protected virtual PickleStep CreatePickleStep(Step step, string text, PickleStepArgument argument, IEnumerable<string> astNodeIds)151 {152 return new PickleStep(argument, astNodeIds, _idGenerator.GetNewId(), text);153 }154 protected virtual PickleStepArgument CreatePickleArgument(Step argument)155 {156 var noCells = Enumerable.Empty<TableCell>();157 return CreatePickleArgument(argument, noCells, noCells);158 }159 protected virtual PickleStepArgument CreatePickleArgument(Step step, IEnumerable<TableCell> variableCells, IEnumerable<TableCell> valueCells)160 {161 if (step.DataTable != null) {162 var t = step.DataTable;163 var rows = t.Rows;164 var newRows = new List<PickleTableRow>(rows.Count());165 foreach(var row in rows)166 {167 var cells = row.Cells;168 var newCells = new List<PickleTableCell>();169 foreach(var cell in cells)170 {171 newCells.Add(172 new PickleTableCell(173 Interpolate(cell.Value, variableCells, valueCells)174 )175 );176 }177 newRows.Add(new PickleTableRow(newCells));178 }179 return new PickleStepArgument180 {181 DataTable = new PickleTable(newRows)182 };183 }184 if (step.DocString != null) {185 var ds = step.DocString;186 return187 new PickleStepArgument188 {189 DocString = new PickleDocString(190 Interpolate(ds.Content, variableCells, valueCells),191 ds.MediaType == null ? null : Interpolate(ds.MediaType, variableCells, valueCells))192 };193 } 194 195 return null;196 }197 protected virtual PickleStep[] PickleSteps(IEnumerable<Step> steps)198 {199 var result = new List<PickleStep>();200 foreach(var step in steps)201 {202 result.Add(PickleStep(step));203 }204 return result.ToArray();205 }206 protected virtual PickleStep PickleStep(Step step)207 {208 return CreatePickleStep(209 step,210 step.Text,211 CreatePickleArgument(step),212 new []{ step.Id }213 );214 }215 protected virtual string Interpolate(string name, IEnumerable<TableCell> variableCells, IEnumerable<TableCell> valueCells)216 {217 int col = 0;218 foreach (var variableCell in variableCells)219 {220 var valueCell = valueCells.ElementAt(col++);221 string header = variableCell.Value;222 string value = valueCell.Value;...
Pickle.cs
Source:Pickle.cs
...12 public string Name { get; set; }13 [DataMember(Name = "language")]14 public string Language { get; set; }15 [DataMember(Name = "steps")]16 public IReadOnlyCollection<PickleStep> Steps { get; set; }17 [DataMember(Name = "tags")]18 public IReadOnlyCollection<PickleTag> Tags { get; set; }19 [DataMember(Name = "astNodeIds")]20 public IReadOnlyCollection<string> AstNodeIds { get; set; }21 public Pickle()22 {23 }24 25 public Pickle(string id, string uri, string name, string language, IEnumerable<PickleStep> steps, IEnumerable<PickleTag> tags, IEnumerable<string> astNodeIds)26 {27 Id = id;28 Uri = uri;29 Name = name;30 Language = language;31 Steps = steps.ToReadOnlyCollection();32 Tags = tags.ToReadOnlyCollection();33 AstNodeIds = astNodeIds.ToReadOnlyCollection();34 }35 }36}
Scenario.cs
Source:Scenario.cs
...14 public string Name { get; set; }15 [DataMember(Name = "description")]16 public string Description { get; set; }17 [DataMember(Name = "steps")]18 public IReadOnlyCollection<Step> Steps { get; set; }19 [DataMember(Name = "examples")]20 public IReadOnlyCollection<Examples> Examples { get; set; }2122 [DataMember(Name = "id")]23 public string Id { get; set; }24 }25}
PickleStep.cs
Source:PickleStep.cs
1using System.Collections.Generic;2using System.Runtime.Serialization;3namespace Gherkin.CucumberMessages.Types4{5 public class PickleStep6 {7 [DataMember(Name = "argument")]8 public PickleStepArgument Argument { get; set; }9 [DataMember(Name = "astNodeIds")]10 public IReadOnlyCollection<string> AstNodeIds { get; set; }11 [DataMember(Name = "id")]12 public string Id { get; set; }13 [DataMember(Name = "text")]14 public string Text { get; set; }15 public PickleStep()16 {17 }18 19 public PickleStep(PickleStepArgument argument, IEnumerable<string> astNodeIds, string id, string text)20 {21 Id = id;22 Text = text;23 Argument = argument;24 AstNodeIds = astNodeIds.ToReadOnlyCollection();25 }26 }27}
Background.cs
Source:Background.cs
...17 [DataMember(Name = "description")]18 public string Description { get; set; }1920 [DataMember(Name = "steps")]21 public IReadOnlyCollection<Step> Steps { get; set; }2223 [DataMember(Name = "id")]24 public string Id { get; set; }25 }26}
...
Step.cs
Source:Step.cs
1using System.Runtime.Serialization;2namespace Gherkin.CucumberMessages.Types3{4 public class Step5 {6 [DataMember(Name = "location")]7 public Location Location { get; set; }8 [DataMember(Name = "keyword")]9 public string Keyword { get; set; }10 [DataMember(Name = "text")]11 public string Text { get; set; }12 [DataMember(Name = "docString")]13 public DocString DocString { get; set; }14 [DataMember(Name = "dataTable")]15 public DataTable DataTable { get; set; }16 [DataMember(Name = "id")]17 public string Id { get; set; }18 }...
PickleStepArgument.cs
Source:PickleStepArgument.cs
1using System.Runtime.Serialization;2namespace Gherkin.CucumberMessages.Types3{4 public class PickleStepArgument5 {6 [DataMember(Name = "docString")]7 public PickleDocString DocString { get; set; }8 [DataMember(Name = "dataTable")]9 public PickleTable DataTable { get; set; }10 }11}...
Step
Using AI Code Generation
1using Gherkin.CucumberMessages.Types;2using Gherkin.CucumberMessages;3using Gherkin.CucumberMessages;4{5 public string Keyword { get; set; }6 public string Text { get; set; }7 public int Line { get; set; }8 public int Column { get; set; }9 public string Id { get; set; }10 public string DocString { get; set; }11 public List<DataTableRow> DataTable { get; set; }12 public StepArgument Argument { get; set; }13}14using Gherkin.CucumberMessages.Types;15using Gherkin.CucumberMessages;16using Gherkin.CucumberMessages;17{18 public string Keyword { get; set; }19 public string Text { get; set; }20 public int Line { get; set; }21 public int Column { get; set; }22 public string Id { get; set; }23 public string DocString { get; set; }24 public List<DataTableRow> DataTable { get; set; }25 public StepArgument Argument { get; set; }26}27using Gherkin.CucumberMessages.Types;28using Gherkin.CucumberMessages;29using Gherkin.CucumberMessages;30{31 public string Keyword { get; set; }32 public string Text { get; set; }33 public int Line { get; set; }34 public int Column { get; set; }35 public string Id { get; set; }36 public string DocString { get; set; }37 public List<DataTableRow> DataTable { get; set; }38 public StepArgument Argument { get; set; }39}40using Gherkin.CucumberMessages.Types;
Step
Using AI Code Generation
1Step step = new Step();2step.Keyword = "Given ";3step.Text = "I have a step";4StepArgument stepArgument = new StepArgument();5stepArgument.DocString = new DocString();6stepArgument.DocString.Content = "some content";7stepArgument.DocString.Delimiter = "\"\"\"";8Step step = new Step();9step.Keyword = "Given ";10step.Text = "I have a step";11step.Argument = new StepArgument();12step.Argument.DocString = new DocString();13step.Argument.DocString.Content = "some content";14step.Argument.DocString.Delimiter = "\"\"\"";15Step step = new Step();16step.Keyword = "Given ";17step.Text = "I have a step";18step.Argument = new StepArgument();19step.Argument.DocString = new DocString();20step.Argument.DocString.Content = "some content";21step.Argument.DocString.Delimiter = "\"\"\"";22Step step = new Step();23step.Keyword = "Given ";24step.Text = "I have a step";25step.Argument = new StepArgument();26step.Argument.DocString = new DocString();27step.Argument.DocString.Content = "some content";28step.Argument.DocString.Delimiter = "\"\"\"";29Step step = new Step();30step.Keyword = "Given ";31step.Text = "I have a step";32step.Argument = new StepArgument();33step.Argument.DocString = new DocString();34step.Argument.DocString.Content = "some content";35step.Argument.DocString.Delimiter = "\"\"\"";36Step step = new Step();37step.Keyword = "Given ";
Step
Using AI Code Generation
1{2 {3 public static IEnumerable<Step> GetSteps(string gherkin)4 {5 var parser = new Parser();6 var messages = parser.Parse(gherkin);7 var steps = messages.Where(m => m.MessageType == "Step").Select(m => Step.Parser.ParseFrom(m.MessageBytes));8 return steps;9 }10 }11}12{13 {14 public static IEnumerable<StepArgument> GetStepArguments(string gherkin)15 {16 var parser = new Parser();17 var messages = parser.Parse(gherkin);18 var stepArguments = messages.Where(m => m.MessageType == "StepArgument").Select(m => StepArgument.Parser.ParseFrom(m.MessageBytes));19 return stepArguments;20 }21 }22}23{24 {25 public static IEnumerable<DocString> GetDocStrings(string gherkin)26 {27 var parser = new Parser();28 var messages = parser.Parse(gherkin);29 var docStrings = messages.Where(m => m.MessageType == "DocString").Select(m => DocString.Parser.ParseFrom(m.MessageBytes));30 return docStrings;31 }32 }33}34{35 {36 public static IEnumerable<DataTable> GetDataTables(string gherkin)37 {38 var parser = new Parser();39 var messages = parser.Parse(gherkin);40 var dataTables = messages.Where(m => m.MessageType == "DataTable").Select(m => DataTable.Parser.ParseFrom(m.MessageBytes));41 return dataTables;42 }
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!