How to use table method of com.intuit.karate.ScenarioActions class

Best Karate code snippet using com.intuit.karate.ScenarioActions.table

Source:ScenarioActions.java Github

copy

Full Screen

...133 @When("^request (.+)")134 public void request(String body) {135 engine.request(body);136 }137 @When("^table (.+)")138 public void table(String name, DataTable table) {139 table(name, table.asMaps(String.class, String.class));140 }141 @Override142 @Action("^table (.+)")143 public void table(String name, List<Map<String, String>> table) {144 engine.table(name, table);145 }146 @When("^replace (\\w+)$")147 public void replace(String name, DataTable table) {148 replace(name, table.asMaps(String.class, String.class));149 }150 @Override151 @Action("^replace (\\w+)$")152 public void replace(String name, List<Map<String, String>> table) {153 engine.replaceTable(name, table);154 }155 @Override156 @When("^replace (\\w+).([^\\s]+) = (.+)")157 public void replace(String name, String token, String value) {158 engine.replace(name, token, value);159 }160 @Override161 @When("^def (.+) =$")162 public void defDocstring(String name, String exp) {163 engine.assign(AssignType.AUTO, name, exp);164 }165 @Override166 @When("^def (\\w+) = (.+)")167 public void def(String name, String exp) {168 engine.assign(AssignType.AUTO, name, exp);169 }170 @Override171 @When("^text (.+) =$")172 public void text(String name, String exp) {173 engine.assign(AssignType.TEXT, name, exp);174 }175 @Override176 @When("^yaml (.+) = (.+)")177 public void yaml(String name, String exp) {178 engine.assign(AssignType.YAML, name, exp);179 }180 @Override181 @When("^copy (.+) = (.+)")182 public void copy(String name, String exp) {183 engine.assign(AssignType.COPY, name, exp);184 }185 @Override186 @When("^json (.+) = (.+)")187 public void json(String name, String exp) {188 engine.assign(AssignType.JSON, name, exp);189 }190 @Override191 @When("^string (.+) = (.+)")192 public void string(String name, String exp) {193 engine.assign(AssignType.STRING, name, exp);194 }195 @Override196 @When("^xml (.+) = (.+)")197 public void xml(String name, String exp) {198 engine.assign(AssignType.XML, name, exp);199 }200 @Override201 @When("^xmlstring (.+) = (.+)")202 public void xmlstring(String name, String exp) {203 engine.assign(AssignType.XML_STRING, name, exp);204 }205 @Override206 @When("^bytes (.+) = (.+)")207 public void bytes(String name, String exp) {208 engine.assign(AssignType.BYTE_ARRAY, name, exp);209 }210 @Override211 @When("^assert (.+)")212 public void assertTrue(String exp) {213 engine.assertTrue(exp);214 }215 @Override216 @When("^method (\\w+)")217 public void method(String method) {218 engine.method(method);219 }220 @Override221 @When("^retry until (.+)")222 public void retry(String until) {223 engine.retry(until);224 }225 @Override226 @When("^soap action( .+)?")227 public void soapAction(String action) {228 engine.soapAction(action);229 }230 @Override231 @When("^multipart entity (.+)")232 public void multipartEntity(String value) {233 engine.multipartField(null, value);234 }235 @Override236 @When("^multipart field (.+) = (.+)")237 public void multipartField(String name, String value) {238 engine.multipartField(name, value);239 }240 @Override241 @When("^multipart fields (.+)")242 public void multipartFields(String exp) {243 engine.multipartFields(exp);244 }245 @Override246 @When("^multipart file (.+) = (.+)")247 public void multipartFile(String name, String value) {248 engine.multipartFile(name, value);249 }250 @Override251 @When("^multipart files (.+)")252 public void multipartFiles(String exp) {253 engine.multipartFiles(exp);254 }255 @Override256 @When("^print (.+)")257 public void print(String exp) {258 engine.print(exp);259 }260 @Override261 @When("^status (\\d+)")262 public void status(int status) {263 engine.status(status);264 }265 @Override266 @When("^match (.+)(=|contains|any|only|deep)(.*)")267 public void match(String exp, String op1, String op2, String rhs) {268 if (op2 == null) {269 op2 = "";270 }271 if (rhs == null) {272 rhs = "";273 }274 MatchStep m = new MatchStep(exp + op1 + op2 + rhs);275 engine.matchResult(m.type, m.name, m.path, m.expected);276 }277 @Override278 @When("^set ([^\\s]+)( .+)? =$")279 public void setDocstring(String name, String path, String value) {280 engine.set(name, path, value);281 }282 @Override283 @When("^set ([^\\s]+)( .+)? = (.+)")284 public void set(String name, String path, String value) {285 engine.set(name, path, value);286 }287 @When("^set ([^\\s]+)( [^=]+)?$")288 public void set(String name, String path, DataTable table) {289 set(name, path, table.asMaps(String.class, String.class));290 }291 @Override292 @Action("^set ([^\\s]+)( [^=]+)?$")293 public void set(String name, String path, List<Map<String, String>> table) {294 engine.setViaTable(name, path, table);295 }296 @Override297 @When("^remove ([^\\s]+)( .+)?")298 public void remove(String name, String path) {299 engine.remove(name, path);300 }301 @Override302 @When("^call (.+)")303 public void call(String line) {304 engine.call(false, line, true);305 }306 @Override307 @When("^callonce (.+)")308 public void callonce(String line) {...

Full Screen

Full Screen

Source:StepRuntime.java Github

copy

Full Screen

1/*2 * The MIT License3 *4 * Copyright 2020 Intuit Inc.5 *6 * Permission is hereby granted, free of charge, to any person obtaining a copy7 * of this software and associated documentation files (the "Software"), to deal8 * in the Software without restriction, including without limitation the rights9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell10 * copies of the Software, and to permit persons to whom the Software is11 * furnished to do so, subject to the following conditions:12 *13 * The above copyright notice and this permission notice shall be included in14 * all copies or substantial portions of the Software.15 *16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN22 * THE SOFTWARE.23 */24package com.intuit.karate.core;25import com.intuit.karate.Actions;26import com.intuit.karate.Json;27import com.intuit.karate.JsonUtils;28import com.intuit.karate.KarateException;29import com.intuit.karate.ScenarioActions;30import com.intuit.karate.StringUtils;31import cucumber.api.java.en.When;32import java.lang.reflect.InvocationTargetException;33import java.lang.reflect.Method;34import java.util.ArrayList;35import java.util.Arrays;36import java.util.Collection;37import java.util.HashMap;38import java.util.HashSet;39import java.util.List;40import java.util.Map;41import java.util.Objects;42import java.util.StringJoiner;43import java.util.regex.Matcher;44import java.util.regex.Pattern;45import java.util.stream.Collectors;46/**47 * @author pthomas348 */49public class StepRuntime {50 private StepRuntime() {51 // only static methods52 }53 static class MethodPattern {54 final String regex;55 final Method method;56 final Pattern pattern;57 final String keyword;58 MethodPattern(Method method, String regex) {59 this.regex = regex;60 this.method = method;61 try {62 pattern = Pattern.compile(regex);63 } catch (Exception e) {64 throw new RuntimeException(e);65 }66 // assuming all @When or @Action start with a ^, get the first word67 keyword = regex.substring(1).split(" ")[0];68 }69 List<String> match(String text) {70 Matcher matcher = pattern.matcher(text);71 if (matcher.lookingAt()) {72 List<String> args = new ArrayList(matcher.groupCount());73 for (int i = 1; i <= matcher.groupCount(); i++) {74 int startIndex = matcher.start(i);75 args.add(startIndex == -1 ? null : matcher.group(i));76 }77 return args;78 } else {79 return null;80 }81 }82 @Override83 public String toString() {84 return "\n" + pattern + " " + method.toGenericString();85 }86 }87 public static class MethodMatch {88 private static final Pattern METHOD_REGEX_PATTERN = Pattern.compile("([a-zA-Z_$][a-zA-Z\\d_$\\.]*)*\\.([a-zA-Z_$][a-zA-Z\\d_$]*?)\\((.*)\\)");89 final Method method;90 final List<String> args;91 MethodMatch(Method method, List<String> args) {92 this.method = method;93 this.args = args;94 }95 Object[] convertArgs(Object last) {96 Class[] types = method.getParameterTypes();97 Object[] result = new Object[types.length];98 int i = 0;99 for (String arg : args) {100 Class type = types[i];101 if (List.class.isAssignableFrom(type)) {102 result[i] = StringUtils.split(arg, ',', false);103 } else if (int.class.isAssignableFrom(type)) {104 result[i] = Integer.valueOf(arg);105 } else { // string106 result[i] = arg;107 }108 i++;109 }110 if (last != null) {111 result[i] = last;112 }113 return result;114 }115 public static MethodMatch getBySignatureAndArgs(String methodReference) {116 String methodSignature = methodReference.substring(0, methodReference.indexOf(' '));117 String referenceArgs = methodReference.substring(methodReference.indexOf(' ') + 1);118 Matcher methodMatch = METHOD_REGEX_PATTERN.matcher(methodSignature);119 Method method = null;120 if (methodMatch.find()) {121 try {122 String className = methodMatch.group(1);123 String methodName = methodMatch.group(2);124 String params = methodMatch.group(3);125 List<String> paramList = Arrays.asList(params.split(","));126 method = Class.forName(className).getMethod(methodName, paramList.stream().map(param -> {127 try {128 return Class.forName(param);129 } catch (ClassNotFoundException e) {130 return null;131 }132 }).filter(Objects::nonNull).toArray(Class<?>[]::new));133 } catch (ClassNotFoundException | NoSuchMethodException e) {134 return null;135 }136 }137 List<String> args = "null".equalsIgnoreCase(referenceArgs) ? null : Json.of(JsonUtils.fromJson(referenceArgs)).asList();138 return new MethodMatch(method, args);139 }140 public Method getMethod() {141 return method;142 }143 public List<String> getArgs() {144 return args;145 }146 @Override147 public String toString() {148 StringBuilder sb = new StringBuilder();149 sb.append(method.getDeclaringClass().getName());150 sb.append(".");151 sb.append(method.getName());152 sb.append("(");153 StringJoiner sj = new StringJoiner(",");154 for (Class<?> parameterType : method.getParameterTypes()) {155 sj.add(parameterType.getTypeName());156 }157 sb.append(sj);158 sb.append(")");159 return sb.toString() + " " + (args == null || args.isEmpty() ? "null" : JsonUtils.toJson(args));160 }161 }162 private static final Collection<MethodPattern> PATTERNS;163 private static final Map<String, Collection<Method>> KEYWORDS_METHODS;164 public static final Collection<Method> METHOD_MATCH;165 static {166 Map<String, MethodPattern> temp = new HashMap();167 List<MethodPattern> overwrite = new ArrayList();168 KEYWORDS_METHODS = new HashMap();169 for (Method method : ScenarioActions.class.getMethods()) {170 When when = method.getDeclaredAnnotation(When.class);171 if (when != null) {172 String regex = when.value();173 MethodPattern methodPattern = new MethodPattern(method, regex);174 temp.put(regex, methodPattern);175 Collection<Method> keywordMethods = KEYWORDS_METHODS.computeIfAbsent(methodPattern.keyword, k -> new HashSet<>());176 keywordMethods.add(methodPattern.method);177 } else {178 Action action = method.getDeclaredAnnotation(Action.class);179 if (action != null) {180 String regex = action.value();181 MethodPattern methodPattern = new MethodPattern(method, regex);182 overwrite.add(methodPattern);183 }184 }185 }186 for (MethodPattern mp : overwrite) {187 temp.put(mp.regex, mp);188 Collection<Method> keywordMethods = KEYWORDS_METHODS.computeIfAbsent(mp.keyword, k -> new HashSet<>());189 keywordMethods.add(mp.method);190 }191 PATTERNS = temp.values();192 METHOD_MATCH = findMethodsByKeyword("match");193 }194 private static List<MethodMatch> findMethodsMatching(String text) {195 List<MethodMatch> matches = new ArrayList(1);196 for (MethodPattern pattern : PATTERNS) {197 List<String> args = pattern.match(text);198 if (args != null) {199 matches.add(new MethodMatch(pattern.method, args));200 }201 }202 return matches;203 }204 public static Collection<Method> findMethodsByKeywords(List<String> text) {205 Collection<Method> methods = new HashSet();206 text.forEach(m -> {207 methods.addAll(findMethodsByKeyword(m));208 });209 return methods;210 }211 public static Collection<Method> findMethodsByKeyword(String text) {212 return KEYWORDS_METHODS.get(text);213 }214 private static long getElapsedTimeNanos(long startTime) {215 return System.nanoTime() - startTime;216 }217 public static Result execute(Step step, Actions actions) {218 String text = step.getText();219 List<MethodMatch> matches = findMethodsMatching(text);220 if (matches.isEmpty()) {221 KarateException e = new KarateException("no step-definition method match found for: " + text);222 return Result.failed(0, e, step);223 } else if (matches.size() > 1) {224 KarateException e = new KarateException("more than one step-definition method matched: " + text + " - " + matches);225 return Result.failed(0, e, step);226 }227 MethodMatch match = matches.get(0);228 Object last;229 if (step.getDocString() != null) {230 last = step.getDocString();231 } else if (step.getTable() != null) {232 last = step.getTable().getRowsAsMaps();233 } else {234 last = null;235 }236 Object[] args;237 try {238 args = match.convertArgs(last);239 } catch (Exception ignored) { // edge case where user error causes [request =] to match [request docstring]240 KarateException e = new KarateException("no step-definition method match found for: " + text);241 return Result.failed(0, e, step);242 }243 long startTime = System.nanoTime();244 try {245 match.method.invoke(actions, args);246 if (actions.isAborted()) {247 return Result.aborted(getElapsedTimeNanos(startTime), match);248 } else if (actions.isFailed()) {249 return Result.failed(getElapsedTimeNanos(startTime), actions.getFailedReason(), step, match);250 } else {251 return Result.passed(getElapsedTimeNanos(startTime), match);252 }253 } catch (InvocationTargetException e) {254 return Result.failed(getElapsedTimeNanos(startTime), e.getTargetException(), step, match);255 } catch (Exception e) {256 return Result.failed(getElapsedTimeNanos(startTime), e, step, match);257 }258 }259}...

Full Screen

Full Screen

table

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.KarateOptions;2import com.intuit.karate.junit4.Karate;3import org.junit.runner.RunWith;4@RunWith(Karate.class)5@KarateOptions(tags = {"~@ignore"})6public class 4 {7}8import com.intuit.karate.KarateOptions;9import com.intuit.karate.junit4.Karate;10import org.junit.runner.RunWith;11@RunWith(Karate.class)12@KarateOptions(tags = {"~@ignore"})13public class 3 {14}15import com.intuit.karate.KarateOptions;16import com.intuit.karate.junit4.Karate;17import org.junit.runner.RunWith;18@RunWith(Karate.class)19@KarateOptions(tags = {"~@ignore"})20public class 2 {21}22import com.intuit.karate.KarateOptions;23import com.intuit.karate.junit4.Karate;24import org.junit.runner.RunWith;25@RunWith(Karate.class)26@KarateOptions(tags = {"~@ignore"})27public class 1 {28}29import com.intuit.karate.KarateOptions;30import com.intuit.karate.junit4.Karate;31import org.junit.runner.RunWith;32@RunWith(Karate.class)33@KarateOptions(tags = {"~@ignore"})34public class 0 {35}36import com.intuit.karate.KarateOptions;37import com.intuit.karate.junit4.Karate;38import org.junit.runner.RunWith;39@RunWith(Karate.class)40@KarateOptions(tags = {"~@ignore"})41public class 5 {42}43import com.intuit.karate.KarateOptions;44import com.intuit.karate.junit4.Karate

Full Screen

Full Screen

table

Using AI Code Generation

copy

Full Screen

1package demo;2import com.intuit.karate.junit5.Karate;3public class 4 {4 Karate testAll() {5 return Karate.run().relativeTo(getClass());6 }7}8package demo;9import com.intuit.karate.junit5.Karate;10public class 4 {11 Karate testAll() {12 return Karate.run().relativeTo(getClass());13 }14}15package demo;16import com.intuit.karate.junit5.Karate;17public class 4 {18 Karate testAll() {19 return Karate.run().relativeTo(getClass());20 }21}22package demo;23import com.intuit.karate.junit5.Karate;24public class 4 {25 Karate testAll() {26 return Karate.run().relativeTo(getClass());27 }28}

Full Screen

Full Screen

table

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.junit5.Karate;2public class 4 {3 Karate testUsers() {4 return Karate.run("4").table(1, 2, 3);5 }6}7* def input = { id: 1, name: 'John' }

Full Screen

Full Screen

table

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.KarateOptions;2import com.intuit.karate.junit4.Karate;3import org.junit.runner.RunWith;4import java.util.List;5import java.util.Map;6@RunWith(Karate.class)7@KarateOptions(features = "classpath:4.feature")8public class 4 {9 public static void main(String[] args) {10 List<Map<String, String>> table = ScenarioActions.table();11 System.out.println("table = " + table);12 }13}14* def response = { "table": [ { "id": "1", "name": "John" }, { "id": "2", "name": "Peter" } ] }15import com.intuit.karate.KarateOptions;16import com.intuit.karate.junit4.Karate;17import org.junit.runner.RunWith;18import java.util.List;19import java.util.Map;20@RunWith(Karate.class)21@KarateOptions(features = "classpath:4.feature")22public class 4 {23 public static void main(String[] args) {24 List<Map<String, String>> table = ScenarioActions.table();25 System.out.println("table = " + table);26 }27}28* def response = { "table": [ { "id": "1", "name": "John" }, { "id": "2", "name": "Peter" } ] }29import com.intuit.karate.KarateOptions;30import com.intuit.karate.junit4.Karate;31import org.junit.runner.RunWith;32import java.util.List;33import java.util.Map;34@RunWith(Karate.class)35@KarateOptions(features = "classpath:4.feature")36public class 4 {37 public static void main(String[] args) {38 List<Map<String, String>> table = ScenarioActions.table();39 System.out.println("table = " + table

Full Screen

Full Screen

table

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.junit4.Karate;2import org.junit.runner.RunWith;3@RunWith(Karate.class)4public class 4 {5}6import com.intuit.karate.junit4.Karate;7import org.junit.runner.RunWith;8@RunWith(Karate.class)9public class 5 {10}11import com.intuit.karate.junit4.Karate;12import org.junit.runner.RunWith;13@RunWith(Karate.class)14public class 6 {15}16import com.intuit.karate.junit4.Karate;17import org.junit.runner.RunWith;18@RunWith(Karate.class)19public class 7 {20}21import com.intuit.karate.junit4.Karate;22import org.junit.runner.RunWith;23@RunWith(Karate.class)24public class 8 {25}26import com.intuit.karate.junit4.Karate;27import org.junit.runner.RunWith;28@RunWith(Karate.class)29public class 9 {30}31import com.intuit.karate.junit4.Karate;32import org.junit.runner.RunWith;33@RunWith(Karate.class)34public class 10 {35}36import com.intuit.karate.junit4.Karate;37import org.junit.runner.RunWith;38@RunWith(Karate.class)39public class 11 {40}

Full Screen

Full Screen

table

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.junit5.Karate;2class TableTest {3 Karate testTable() {4 return Karate.run("table").table("table", "table");5 }6}7import com.intuit.karate.junit5.Karate;8class TableTest {9 Karate testTable() {10 return Karate.run("table").jsonPath("table", "table");11 }12}13import com.intuit.karate.junit5.Karate;14class TableTest {15 Karate testTable() {16 return Karate.run("table").table("table", "table").jsonPath("table[0].id", "id");17 }18}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful