How to use setLine method of com.intuit.karate.core.Background class

Best Karate code snippet using com.intuit.karate.core.Background.setLine

Source:FeatureParser.java Github

copy

Full Screen

...72 feature = new FeatureParser(feature, FileUtils.toInputStream(text)).feature;73 if (old != null) {74 feature.setCallTag(old.getCallTag());75 }76 feature.setLines(StringUtils.toStringLines(text));77 return feature;78 }79 public static void updateStepFromText(Step step, String text) throws Exception {80 Feature feature = new Feature(step.getFeature().getResource());81 final String stepText = text.trim();82 boolean hasPrefix = PREFIXES.stream().anyMatch(prefixValue -> stepText.startsWith(prefixValue));83 // to avoid parser considering text without prefix as Scenario comments/Doc-string84 if (!hasPrefix) {85 text = "* " + stepText;86 }87 text = "Feature:\nScenario:\n" + text;88 FeatureParser fp = new FeatureParser(feature, FileUtils.toInputStream(text));89 if (fp.errorListener.isFail()) {90 throw new KarateException(fp.errorListener.getMessage());91 }92 feature = fp.feature;93 Step temp = feature.getStep(0, -1, 0);94 if (temp == null) {95 throw new KarateException("invalid expression: " + text);96 }97 step.setPrefix(temp.getPrefix());98 step.setText(temp.getText());99 step.setDocString(temp.getDocString());100 step.setTable(temp.getTable());101 }102 private static InputStream toStream(File file) {103 try {104 return new FileInputStream(file);105 } catch (FileNotFoundException e) {106 throw new RuntimeException(e);107 }108 }109 private FeatureParser(File file, String relativePath, ClassLoader cl) {110 this(new Feature(new Resource(file, relativePath)), toStream(file));111 }112 private FeatureParser(Resource resource) {113 this(new Feature(resource), resource.getStream());114 }115 private final CommonTokenStream tokenStream;116 private FeatureParser(Feature feature, InputStream is) {117 this.feature = feature;118 CharStream stream;119 try {120 stream = CharStreams.fromStream(is, StandardCharsets.UTF_8);121 } catch (IOException e) {122 throw new RuntimeException(e);123 }124 KarateLexer lexer = new KarateLexer(stream);125 tokenStream = new CommonTokenStream(lexer);126 KarateParser parser = new KarateParser(tokenStream);127 parser.addErrorListener(errorListener);128 RuleContext tree = parser.feature();129 if (logger.isTraceEnabled()) {130 logger.debug(tree.toStringTree(parser));131 }132 ParseTreeWalker walker = new ParseTreeWalker();133 walker.walk(this, tree);134 if (errorListener.isFail()) {135 String errorMessage = errorListener.getMessage();136 logger.error("not a valid feature file: {} - {}", feature.getResource().getRelativePath(), errorMessage);137 throw new RuntimeException(errorMessage);138 }139 }140 private static int getActualLine(TerminalNode node) {141 int count = 0;142 for (char c : node.getText().toCharArray()) {143 if (c == '\n') {144 count++;145 } else if (!Character.isWhitespace(c)) {146 break;147 }148 }149 return node.getSymbol().getLine() + count;150 }151 private static List<Tag> toTags(int line, List<TerminalNode> nodes) {152 List<Tag> tags = new ArrayList();153 for (TerminalNode node : nodes) {154 String text = node.getText();155 if (line == -1) {156 line = getActualLine(node);157 }158 String[] tokens = text.trim().split("\\s+"); // handles spaces and tabs also 159 for (String t : tokens) {160 tags.add(new Tag(line, t));161 }162 }163 return tags;164 }165 private static Table toTable(KarateParser.TableContext ctx) {166 List<TerminalNode> nodes = ctx.TABLE_ROW();167 int rowCount = nodes.size();168 if (rowCount < 1) {169 // if scenario outline found without examples170 return null;171 }172 List<List<String>> rows = new ArrayList(rowCount);173 List<Integer> lineNumbers = new ArrayList(rowCount);174 for (TerminalNode node : nodes) {175 List<String> tokens = StringUtils.split(node.getText().trim(), '|', true);176 int count = tokens.size();177 for (int i = 0; i < count; i++) {178 tokens.set(i, tokens.get(i).trim());179 }180 rows.add(tokens);181 lineNumbers.add(getActualLine(node));182 }183 return new Table(rows, lineNumbers);184 }185 private static final String TRIPLE_QUOTES = "\"\"\"";186 private static int indexOfFirstText(String s) {187 int pos = 0;188 for (char c : s.toCharArray()) {189 if (!Character.isWhitespace(c)) {190 return pos;191 }192 pos++;193 }194 return 0; // defensive coding195 }196 private static String fixDocString(String temp) {197 int quotePos = temp.indexOf(TRIPLE_QUOTES);198 int endPos = temp.lastIndexOf(TRIPLE_QUOTES);199 String raw = temp.substring(quotePos + 3, endPos).replaceAll("\r", "");200 List<String> lines = StringUtils.split(raw, '\n', false);201 StringBuilder sb = new StringBuilder();202 int marginPos = -1;203 Iterator<String> iterator = lines.iterator();204 while (iterator.hasNext()) {205 String line = iterator.next();206 int firstTextPos = indexOfFirstText(line);207 if (marginPos == -1) {208 marginPos = firstTextPos;209 }210 if (marginPos < line.length() && marginPos <= firstTextPos) {211 line = line.substring(marginPos);212 }213 if (iterator.hasNext()) {214 sb.append(line).append('\n');215 } else {216 sb.append(line);217 }218 }219 return sb.toString().trim();220 }221 private List<String> collectComments(ParserRuleContext prc) {222 List<Token> tokens = tokenStream.getHiddenTokensToLeft(prc.start.getTokenIndex());223 if (tokens == null) {224 return null;225 }226 List<String> comments = new ArrayList(tokens.size());227 for (Token t : tokens) {228 comments.add(StringUtils.trimToNull(t.getText()));229 }230 return comments;231 }232 private List<Step> toSteps(Scenario scenario, List<KarateParser.StepContext> list) {233 List<Step> steps = new ArrayList(list.size());234 int index = 0;235 for (KarateParser.StepContext sc : list) {236 Step step = new Step(feature, scenario, index++);237 step.setComments(collectComments(sc));238 steps.add(step);239 int stepLine = sc.line().getStart().getLine();240 step.setLine(stepLine);241 step.setPrefix(sc.prefix().getText().trim());242 step.setText(sc.line().getText().trim());243 if (sc.docString() != null) {244 String raw = sc.docString().getText();245 step.setDocString(fixDocString(raw));246 step.setEndLine(stepLine + StringUtils.countLineFeeds(raw));247 } else if (sc.table() != null) {248 Table table = toTable(sc.table());249 step.setTable(table);250 step.setEndLine(stepLine + StringUtils.countLineFeeds(sc.table().getText()));251 } else {252 step.setEndLine(stepLine);253 }254 }255 return steps;256 }257 @Override258 public void enterFeatureHeader(KarateParser.FeatureHeaderContext ctx) {259 if (ctx.featureTags() != null) {260 feature.setTags(toTags(ctx.featureTags().getStart().getLine(), ctx.featureTags().FEATURE_TAGS()));261 }262 if (ctx.FEATURE() != null) {263 feature.setLine(ctx.FEATURE().getSymbol().getLine());264 if (ctx.featureDescription() != null) {265 StringUtils.Pair pair = StringUtils.splitByFirstLineFeed(ctx.featureDescription().getText());266 feature.setName(pair.left);267 feature.setDescription(pair.right);268 }269 }270 }271 @Override272 public void enterBackground(KarateParser.BackgroundContext ctx) {273 Background background = new Background();274 feature.setBackground(background);275 background.setLine(getActualLine(ctx.BACKGROUND()));276 List<Step> steps = toSteps(null, ctx.step());277 if (!steps.isEmpty()) {278 background.setSteps(steps);279 }280 if (logger.isTraceEnabled()) {281 logger.trace("background steps: {}", steps);282 }283 }284 @Override285 public void enterScenario(KarateParser.ScenarioContext ctx) {286 FeatureSection section = new FeatureSection();287 Scenario scenario = new Scenario(feature, section, -1);288 section.setScenario(scenario);289 feature.addSection(section);290 scenario.setLine(getActualLine(ctx.SCENARIO()));291 if (ctx.tags() != null) {292 scenario.setTags(toTags(-1, ctx.tags().TAGS()));293 }294 if (ctx.scenarioDescription() != null) {295 StringUtils.Pair pair = StringUtils.splitByFirstLineFeed(ctx.scenarioDescription().getText());296 scenario.setName(pair.left);297 scenario.setDescription(pair.right);298 }299 List<Step> steps = toSteps(scenario, ctx.step());300 scenario.setSteps(steps);301 if (logger.isTraceEnabled()) {302 logger.trace("scenario steps: {}", steps);303 }304 }305 @Override306 public void enterScenarioOutline(KarateParser.ScenarioOutlineContext ctx) {307 FeatureSection section = new FeatureSection();308 ScenarioOutline outline = new ScenarioOutline(feature, section);309 section.setScenarioOutline(outline);310 feature.addSection(section);311 outline.setLine(getActualLine(ctx.SCENARIO_OUTLINE()));312 if (ctx.tags() != null) {313 outline.setTags(toTags(-1, ctx.tags().TAGS()));314 }315 if (ctx.scenarioDescription() != null) {316 outline.setDescription(ctx.scenarioDescription().getText());317 StringUtils.Pair pair = StringUtils.splitByFirstLineFeed(ctx.scenarioDescription().getText());318 outline.setName(pair.left);319 outline.setDescription(pair.right);320 }321 List<Step> steps = toSteps(null, ctx.step());322 outline.setSteps(steps);323 if (logger.isTraceEnabled()) {324 logger.trace("outline steps: {}", steps);325 }...

Full Screen

Full Screen

Source:Step.java Github

copy

Full Screen

...91 background = false;92 }93 Step step = background ? new Step(scenario.getFeature(), index) : new Step(scenario, index);94 int line = (Integer) map.get("line");95 step.setLine(line);96 Integer endLine = (Integer) map.get("endLine");97 if (endLine == null) {98 endLine = line;99 }100 step.setEndLine(endLine);101 if(map.get("comments") instanceof List) {102 step.setComments((List) map.get("comments"));103 }104 step.setPrefix((String) map.get("prefix"));105 step.setText((String) map.get("text"));106 step.setDocString((String) map.get("docString"));107 if(map.get("table") instanceof List) {108 List<Map<String, Object>> table = (List) map.get("table");109 if (table != null) {110 step.setTable(Table.fromKarateJson(table));111 }112 }113 return step;114 }115 public Map<String, Object> toKarateJson() {116 Map<String, Object> map = new HashMap();117 if (isBackground()) {118 map.put("background", true);119 }120 map.put("index", index);121 map.put("line", line);122 if (endLine != line) {123 map.put("endLine", endLine);124 }125 if (comments != null && !comments.isEmpty()) {126 map.put("comments", comments);127 }128 map.put("prefix", prefix);129 map.put("text", text);130 if (docString != null) {131 map.put("docString", docString);132 }133 if (table != null) {134 map.put("table", table.toKarateJson());135 }136 return map;137 }138 public boolean isBackground() {139 return scenario == null;140 }141 public boolean isOutline() {142 return scenario != null && scenario.isOutlineExample();143 }144 public int getIndex() {145 return index;146 }147 public int getLine() {148 return line;149 }150 public void setLine(int line) {151 this.line = line;152 }153 public int getLineCount() {154 return endLine - line + 1;155 }156 public int getEndLine() {157 return endLine;158 }159 public void setEndLine(int endLine) {160 this.endLine = endLine;161 }162 public String getPrefix() {163 return prefix;164 }...

Full Screen

Full Screen

setLine

Using AI Code Generation

copy

Full Screen

1package com.intuit.karate;2import com.intuit.karate.core.Feature;3import com.intuit.karate.core.FeatureParser;4import com.intuit.karate.core.FeatureRuntime;5import com.intuit.karate.core.FeatureWrapper;6import com.intuit.karate.core.Scenario;7import com.intuit.karate.core.ScenarioRuntime;8import com.intuit.karate.core.ScenarioWrapper;9import com.intuit.karate.core.Step;10import com.intuit.karate.core.StepRuntime;11import com.intuit.karate.core.StepWrapper;12import com.intuit.karate.core.Background;13import java.util.List;14import java.util.Map;15import java.util.HashMap;16import java.util.ArrayList;17import java.util.Arrays;18public class 4 {19 public static void main(String[] args) {20";21 FeatureWrapper fw = FeatureParser.parse(feature, "4.feature");22 Feature f = fw.getFeature();23 Background b = f.getBackground();24 List<Step> steps = b.getSteps();25 for (Step s : steps) {26 System.out.println(s.getLine());27 }28 System.out.println("29");30 Step newStep = new Step();31 newStep.setLine(" * def z = 3");32 b.setLine(2, newStep);33 steps = b.getSteps();34 for (Step s : steps) {35 System.out.println(s.getLine());36 }37 }38}

Full Screen

Full Screen

setLine

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.core.Background;2import com.intuit.karate.core.Feature;3import com.intuit.karate.core.Scenario;4import com.intuit.karate.core.Step;5import java.util.ArrayList;6import java.util.List;7public class 4 {8 public static void main(String[] args) {9 Feature feature = new Feature();10 Background background = new Background();11 background.setLine(2);12 Scenario scenario = new Scenario();13 scenario.setLine(7);14 Step step = new Step();15 step.setLine(8);16 List<Step> steps = new ArrayList<>();17 steps.add(step);18 scenario.setSteps(steps);19 List<Scenario> scenarios = new ArrayList<>();20 scenarios.add(scenario);21 feature.setScenarios(scenarios);22 feature.setBackground(background);23 System.out.println("Line number of the feature: " + feature.getLine());24 System.out.println("Line number of the background: " + feature.getBackground().getLine());25 System.out.println("Line number of the scenario: " + feature.getScenarios().get(0).getLine());26 System.out.println("Line number of the step: " + feature.getScenarios().get(0).getSteps().get(0).getLine());27 }28}29import com.intuit.karate.core.Sc

Full Screen

Full Screen

setLine

Using AI Code Generation

copy

Full Screen

1package demo;2import com.intuit.karate.core.Background;3import com.intuit.karate.core.Feature;4import com.intuit.karate.core.FeatureBuilder;5import com.intuit.karate.core.FeatureResult;6import com.intuit.karate.core.Scenario;7import com.intuit.karate.core.ScenarioResult;8import com.intuit.karate.core.Step;9import com.intuit.karate.core.StepResult;10import com.intuit.karate.core.StepResult.StepResultType;11import com.intuit.karate.core.StepType;12import java.util.ArrayList;13import java.util.List;14public class 4 {15 public static void main(String[] args) {16 Feature feature = new FeatureBuilder().build();17 Background background = new Background();18 background.setLine(1);19 Scenario scenario = new Scenario();20 scenario.setLine(2);21 List<Step> steps = new ArrayList<>();22 Step step = new Step();23 step.setLine(3);24 step.setType(StepType.GIVEN);25 step.setText("a");26 steps.add(step);27 step = new Step();28 step.setLine(4);29 step.setType(StepType.WHEN);30 step.setText("b");31 steps.add(step);32 step = new Step();33 step.setLine(5);34 step.setType(StepType.THEN);35 step.setText("c");36 steps.add(step);37 scenario.setSteps(steps);38 List<Scenario> scenarios = new ArrayList<>();39 scenarios.add(scenario);40 feature.setScenarios(scenarios);41 FeatureResult featureResult = new FeatureResult(feature);42 ScenarioResult scenarioResult = featureResult.getScenarioResults().get(0);43 List<StepResult> stepResults = scenarioResult.getStepResults();44 StepResult stepResult = new StepResult();45 stepResult.setLine(6);46 stepResult.setType(StepType.THEN);47 stepResult.setText("d");48 stepResult.setResultType(StepResultType.PASS);49 stepResults.add(stepResult);50 stepResult = new StepResult();51 stepResult.setLine(7);52 stepResult.setType(StepType.THEN);53 stepResult.setText("e");54 stepResult.setResultType(StepResultType.FAIL);55 stepResults.add(stepResult);56 stepResult = new StepResult();57 stepResult.setLine(8);58 stepResult.setType(StepType.THEN);59 stepResult.setText("f");

Full Screen

Full Screen

setLine

Using AI Code Generation

copy

Full Screen

1Background bg = new Background();2bg.setLine(1);3bg.setLine(2);4bg.setLine(3);5bg.setLine(4);6bg.setLine(5);7bg.setLine(6);8bg.setLine(7);9bg.setLine(8);10bg.setLine(9);11bg.setLine(10);12bg.setLine(11);13bg.setLine(12);14bg.setLine(13);15bg.setLine(14);16bg.setLine(15);17bg.setLine(16);18bg.setLine(17);19bg.setLine(18);20bg.setLine(19);21bg.setLine(20);22bg.setLine(21);23bg.setLine(22);24bg.setLine(23);25bg.setLine(24);26bg.setLine(25);27bg.setLine(26);28bg.setLine(27);29bg.setLine(28);30bg.setLine(29);31bg.setLine(30);32bg.setLine(31);33bg.setLine(32);34bg.setLine(33);35bg.setLine(34);36bg.setLine(35);37bg.setLine(36);38bg.setLine(37);39bg.setLine(38);40bg.setLine(39);41bg.setLine(40);42bg.setLine(41);43bg.setLine(42);44bg.setLine(43);45bg.setLine(44);46bg.setLine(45);47bg.setLine(46);48bg.setLine(47);49bg.setLine(48);50bg.setLine(49);51bg.setLine(50);52bg.setLine(51);53bg.setLine(52);54bg.setLine(53);55bg.setLine(54);56bg.setLine(55);57bg.setLine(56);58bg.setLine(57);59bg.setLine(58);60bg.setLine(59);61bg.setLine(60);62bg.setLine(61);63bg.setLine(62);64bg.setLine(63);65bg.setLine(64);66bg.setLine(65);67bg.setLine(66);68bg.setLine(67);69bg.setLine(68);70bg.setLine(69);71bg.setLine(70);72bg.setLine(71);73bg.setLine(72);74bg.setLine(73);75bg.setLine(74);76bg.setLine(75);77bg.setLine(76);78bg.setLine(77);79bg.setLine(78);80bg.setLine(79);81bg.setLine(80);82bg.setLine(81);83bg.setLine(82);

Full Screen

Full Screen

setLine

Using AI Code Generation

copy

Full Screen

1package com.intuit.karate;2import org.junit.Test;3import org.junit.runner.RunWith;4import com.intuit.karate.junit4.Karate;5import com.intuit.karate.core.Feature;6import com.intuit.karate.core.FeatureBuilder;7import com.intuit.karate.core.Background;8import com.intuit.karate.core.Scenario;9import com.intuit.karate.core.ScenarioOutline;10import com.intuit.karate.core.Section;11import com.intuit.karate.core.Step;12import java.util.List;13import java.util.ArrayList;14import java.util.Map;15import java.util.HashMap;16import java.util.Arrays;17import java.util.Collection;18import java.util.Collections;19import java.util.Set;20import java.util.HashSet;21import java.util.Iterator;22@RunWith(Karate.class)23public class 4 {24 public void test() {25 Map<String, Object> map = new HashMap<>();26 map.put("foo", "bar");27 map.put("bar", "foo");28 Feature feature = FeatureBuilder.feature("Feature: test");29 Background background = new Background();30 background.setLine(1);31 background.setKeyword("Background:");32 background.setName("test");33 background.setSteps(Arrays.asList(new Step("Given", "foo", 2), new Step("And", "bar", 3)));34 feature.setBackground(background);35 Scenario scenario = new Scenario();36 scenario.setLine(4);37 scenario.setKeyword("Scenario:");38 scenario.setName("test");39 scenario.setSteps(Arrays.asList(new Step("Given", "foo", 5), new Step("And", "bar", 6)));40 feature.setScenarios(Arrays.asList(scenario));41 ScenarioOutline scenarioOutline = new ScenarioOutline();42 scenarioOutline.setLine(7);43 scenarioOutline.setKeyword("Scenario Outline:");44 scenarioOutline.setName("test");45 scenarioOutline.setSteps(Arrays.asList(new Step("Given", "foo", 8), new Step("And", "bar", 9)));46 scenarioOutline.setExamples(Arrays.asList(new Section("Examples:", 10, 11, Arrays.asList(new Step("And", "bar", 12)))));47 feature.setScenarios(Arrays.asList(scenario, scenarioOutline));48 System.out.println(feature.getBackground().getLine());49 System.out.println(feature.getScenarios().get(0).getLine());50 System.out.println(feature.getScenarios().get(1).getLine());51 System.out.println(feature

Full Screen

Full Screen

setLine

Using AI Code Generation

copy

Full Screen

1package com.intuit.karate.core;2import org.junit.Test;3import static org.junit.Assert.*;4public class BackgroundTest {5 public void testBackground() {6 Background b = new Background();7 b.setLine(3);8 assertEquals(3, b.getLine());9 }10}11package com.intuit.karate.core;12import org.junit.Test;13import static org.junit.Assert.*;14public class BackgroundTest {15 public void testBackground() {16 Background b = new Background();17 b.setDocString("doc string");18 assertEquals("doc string", b.getDocString());19 }20}21package com.intuit.karate.core;22import org.junit.Test;23import static org.junit.Assert.*;24public class BackgroundTest {25 public void testBackground() {26 Background b = new Background();27 DataTable dt = new DataTable();28 b.setDataTable(dt);29 assertEquals(dt, b.getDataTable());30 }31}32package com.intuit.karate.core;33import org.junit.Test;34import static org.junit.Assert.*;35public class BackgroundTest {36 public void testBackground() {37 Background b = new Background();38 Step s = new Step();

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Karate automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in Background

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful