How to use execute method of com.intuit.karate.graal.JsExecutable class

Best Karate code snippet using com.intuit.karate.graal.JsExecutable.execute

Source:JsValue.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.graal;25import com.intuit.karate.FileUtils;26import com.intuit.karate.XmlUtils;27import com.intuit.karate.JsonUtils;28import com.intuit.karate.http.ResourceType;29import java.util.ArrayList;30import java.util.LinkedHashMap;31import java.util.List;32import java.util.Map;33import java.util.Set;34import java.util.function.Function;35import org.graalvm.polyglot.Value;36import org.graalvm.polyglot.proxy.Proxy;37import org.slf4j.Logger;38import org.slf4j.LoggerFactory;39import org.w3c.dom.Node;40/**41 *42 * @author pthomas343 */44public class JsValue {45 private static final Logger logger = LoggerFactory.getLogger(JsValue.class);46 public static enum Type {47 OBJECT,48 ARRAY,49 FUNCTION,50 XML,51 NULL,52 OTHER53 }54 private final Value original;55 private final Object value;56 public final Type type;57 public JsValue(Value v) {58 this.original = v;59 try {60 if (v.isNull()) { // apparently this can be a "host object" as well !61 value = null;62 type = Type.NULL;63 } else if (v.isProxyObject()) {64 Object o = v.asProxyObject();65 if (o instanceof JsXml) {66 value = ((JsXml) o).getNode();67 type = Type.XML;68 } else if (o instanceof JsMap) {69 value = ((JsMap) o).getMap();70 type = Type.OBJECT;71 } else if (o instanceof JsList) {72 value = ((JsList) o).getList();73 type = Type.ARRAY;74 } else if (o instanceof JsExecutable) {75 value = (JsExecutable) o;76 type = Type.FUNCTION; 77 } else { // e.g. custom bridge, e.g. Request78 value = v.as(Object.class);79 type = Type.OTHER;80 }81 } else if (v.isHostObject()) { // java object82 if (v.isMetaObject()) { // java.lang.Class !83 value = v; // special case, keep around as graal value84 } else {85 value = v.asHostObject();86 }87 type = Type.OTHER;88 } else if (v.canExecute()) {89 if (v.isMetaObject()) { // js function90 value = v; // special case, keep around as graal value 91 } else { // java function reference92 value = new JsExecutable(v);93 }94 type = Type.FUNCTION;95 } else if (v.hasArrayElements()) {96 int size = (int) v.getArraySize();97 List list = new ArrayList(size);98 for (int i = 0; i < size; i++) {99 Value child = v.getArrayElement(i);100 list.add(new JsValue(child).value);101 }102 value = list;103 type = Type.ARRAY;104 } else if (v.hasMembers()) {105 Set<String> keys = v.getMemberKeys();106 Map<String, Object> map = new LinkedHashMap(keys.size());107 for (String key : keys) {108 Value child = v.getMember(key);109 map.put(key, new JsValue(child).value);110 }111 value = map;112 type = Type.OBJECT;113 } else {114 value = v.as(Object.class);115 type = Type.OTHER;116 }117 } catch (Exception e) {118 if (logger.isTraceEnabled()) {119 logger.trace("js conversion failed", e);120 }121 throw e;122 }123 }124 public <T> T getValue() {125 return (T) value;126 }127 public Map<String, Object> getAsMap() {128 return (Map) value;129 }130 public List getAsList() {131 return (List) value;132 }133 public Value getOriginal() {134 return original;135 }136 public boolean isXml() {137 return type == Type.XML;138 }139 public boolean isNull() {140 return type == Type.NULL;141 }142 public boolean isObject() {143 return type == Type.OBJECT;144 }145 public boolean isArray() {146 return type == Type.ARRAY;147 }148 public boolean isTrue() {149 if (type != Type.OTHER || !Boolean.class.equals(value.getClass())) {150 return false;151 }152 return (Boolean) value;153 }154 public boolean isFunction() {155 return type == Type.FUNCTION;156 }157 public boolean isOther() {158 return type == Type.OTHER;159 }160 @Override161 public String toString() {162 return original.toString();163 }164 165 public String toJsonOrXmlString(boolean pretty) {166 return toString(value, pretty);167 }168 public String getAsString() {169 return JsValue.toString(value);170 }171 public static Object fromJava(Object o) {172 if (o instanceof Function || o instanceof Proxy) {173 return o; 174 } else if (o instanceof List) {175 return new JsList((List) o);176 } else if (o instanceof Map) {177 return new JsMap((Map) o);178 } else if (o instanceof Node) {179 return new JsXml((Node) o);180 } else {181 return o;182 }183 }184 public static Object toJava(Value v) {185 return new JsValue(v).getValue();186 }187 public static Object unWrap(Object o) {188 if (o instanceof JsXml) {189 return ((JsXml) o).getNode();190 } else if (o instanceof JsMap) {191 return ((JsMap) o).getMap();192 } else if (o instanceof JsList) {193 return ((JsList) o).getList();194 } else {195 return o;196 }197 }198 public static byte[] toBytes(Value v) {199 return toBytes(toJava(v));200 }201 202 public static String toString(Object o) {203 return toString(o, false);204 }205 public static String toString(Object o, boolean pretty) {206 if (o == null) {207 return null;208 }209 if (o instanceof Map || o instanceof List) {210 return JsonUtils.toJson(o, pretty);211 } else if (o instanceof Node) {212 return XmlUtils.toString((Node) o, pretty);213 } else if (o instanceof byte[]) {214 return FileUtils.toString((byte[]) o);215 } else {216 return o.toString();217 }218 }219 public static byte[] toBytes(Object o) {220 if (o == null) {221 return null;222 }223 if (o instanceof Map || o instanceof List) {224 return FileUtils.toBytes(JsonUtils.toJson(o));225 } else if (o instanceof Node) {226 return FileUtils.toBytes(XmlUtils.toString((Node) o));227 } else if (o instanceof byte[]) {228 return (byte[]) o;229 } else {230 return FileUtils.toBytes(o.toString());231 }232 }233 public static Object fromBytes(byte[] bytes, boolean jsonStrict, ResourceType resourceType) {234 if (bytes == null) {235 return null;236 }237 String raw = FileUtils.toString(bytes);238 return fromString(raw, jsonStrict, resourceType);239 }240 public static Object fromString(String raw, boolean jsonStrict, ResourceType resourceType) {241 String trimmed = raw.trim();242 if (trimmed.isEmpty()) {243 return raw;244 }245 if (resourceType != null && resourceType.isBinary()) {246 return raw;247 }248 switch (trimmed.charAt(0)) {249 case '{':250 case '[':251 return jsonStrict ? JsonUtils.fromJsonStrict(raw) : JsonUtils.fromJson(raw);252 case '<':253 if (resourceType == null || resourceType.isXml()) {254 return XmlUtils.toXmlDoc(raw);255 } else {256 return raw;257 }258 default:259 return raw;260 }261 }262 public static Object fromStringSafe(String raw) {263 try {264 return fromString(raw, false, null);265 } catch (Exception e) {266 logger.trace("failed to auto convert: {}", e + "");267 return raw;268 }269 }270 271 public static boolean isTruthy(Object o) {272 if (o == null) {273 return false;274 }275 if (o instanceof Boolean) {276 return ((Boolean) o);277 }278 if (o instanceof Number) {279 return ((Number) o).doubleValue() != 0.0;280 }281 return true;282 }283 284}...

Full Screen

Full Screen

Source:JsExecutable.java Github

copy

Full Screen

...38 }39 private static final Object LOCK = new Object();40 protected static Value invoke(Value value, Object... args) {41 try {42 return JsEngine.execute(value, args);43 } catch (Exception e) {44 logger.warn("[*** execute ***] invocation failed: {}", e.getMessage());45 synchronized (LOCK) {46 return JsEngine.execute(value, args);47 }48 }49 }50 @Override51 public Object execute(Value... values) {52 Object[] args = new Object[values.length];53 for (int i = 0; i < args.length; i++) {54 args[i] = values[i].as(Object.class);55 }56 return invoke(value, args);57 }58}...

Full Screen

Full Screen

execute

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.graal.JsExecutable;2import com.intuit.karate.graal.JsValue;3import com.intuit.karate.graal.JsValueFactory;4import java.io.File;5import java.io.IOException;6import java.nio.file.Files;7import java.nio.file.Path;8import java.nio.file.Paths;9import java.util.Map;10import javax.script.ScriptException;11import org.graalvm.polyglot.Context;12import org.graalvm.polyglot.Value;13public class 4 {14 public static void main(String[] args) throws ScriptException, IOException {15 Path path = Paths.get("test.js");16 String script = new String(Files.readAllBytes(path));17 Context context = Context.create("js");18 Value value = context.eval("js", script);19 JsValue jsValue = JsValueFactory.create(value);20 JsExecutable jsExecutable = jsValue.getExecutable();21 Map<String, Object> result = jsExecutable.execute();22 System.out.println(result);23 }24}25import com.intuit.karate.graal.JsExecutable;26import com.intuit.karate.graal.JsValue;27import com.intuit.karate.graal.JsValueFactory;28import java.io.File;29import java.io.IOException;30import java.nio.file.Files;31import java.nio.file.Path;32import java.nio.file.Paths;33import java.util.Map;34import javax.script.ScriptException;35import org.graalvm.polyglot.Context;36import org.graalvm.polyglot.Value;37public class 5 {38 public static void main(String[] args) throws ScriptException, IOException {39 Path path = Paths.get("test.js");40 String script = new String(Files.readAllBytes(path));41 Context context = Context.create("js");42 Value value = context.eval("js", script);43 JsValue jsValue = JsValueFactory.create(value);44 JsExecutable jsExecutable = jsValue.getExecutable();45 Map<String, Object> result = jsExecutable.execute("Hello", "World");46 System.out.println(result);47 }48}49import com.intuit.karate.graal.JsExecutable;50import com.intuit.karate.graal.JsValue;51import

Full Screen

Full Screen

execute

Using AI Code Generation

copy

Full Screen

1package com.intuit.karate.graal;2import com.intuit.karate.ScriptValue;3import com.intuit.karate.core.Feature;4import com.intuit.karate.core.FeatureRuntime;5import com.intuit.karate.core.ScenarioRuntime;6import com.intuit.karate.core.StepRuntime;7import com.intuit.karate.core.engine.FeatureExecutor;8import com.intuit.karate.core.engine.FeatureWrapper;9import com.intuit.karate.core.engine.ScenarioEngine;10import com.intuit.karate.core.engine.StepEngine;11import com.intuit.karate.core.engine.StepResult;12import java.util.HashMap;13import java.util.List;14import java.util.Map;15import javax.script.Bindings;16public class JsExecutable {17 private final FeatureExecutor featureExecutor;18 private final FeatureRuntime featureRuntime;19 private final ScenarioRuntime scenarioRuntime;20 private final StepRuntime stepRuntime;21 public JsExecutable(FeatureExecutor featureExecutor) {22 this.featureExecutor = featureExecutor;23 this.featureRuntime = new FeatureRuntime();24 this.scenarioRuntime = new ScenarioRuntime();25 this.stepRuntime = new StepRuntime();26 }27 public ScriptValue execute(String script, Map<String, Object> args) {28 Feature feature = new Feature();29 feature.setScript(script);30 FeatureWrapper featureWrapper = new FeatureWrapper(feature, featureExecutor);31 ScenarioEngine scenarioEngine = new ScenarioEngine(featureWrapper, featureRuntime, scenarioRuntime);32 StepEngine stepEngine = new StepEngine(scenarioEngine, stepRuntime);33 Bindings bindings = featureRuntime.getBindings();34 bindings.putAll(args);35 List<StepResult> results = stepEngine.executeScript(script, bindings);36 ScriptValue sv = ScriptValue.NULL;37 if (!results.isEmpty()) {38 StepResult lastResult = results.get(results.size() - 1);39 sv = lastResult.getValue();40 }41 return sv;42 }43 public static void main(String[] args) {44 JsExecutable jsExecutable = new JsExecutable(null);45 Map<String, Object> map = new HashMap();46 map.put("foo", "bar");47 ScriptValue sv = jsExecutable.execute("foo", map);48 System.out.println("sv: " + sv);49 }50}

Full Screen

Full Screen

execute

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.graal.JsExecutable;2import com.intuit.karate.graal.JsValue;3import com.intuit.karate.graal.JsEngine;4import java.io.File;5import java.util.HashMap;6import java.util.Map;7public class 4 {8 public static void main(String[] args) {9 String script = "function add(a, b) { return a + b }";10 JsEngine engine = JsEngine.create();11 JsExecutable exec = engine.compile(script);12 Map<String, Object> map = new HashMap();13 map.put("a", 10);14 map.put("b", 20);15 JsValue value = exec.execute(map);16 System.out.println(value);17 }18}19import com.intuit.karate.graal.JsExecutable;20import com.intuit.karate.graal.JsValue;21import com.intuit.karate.graal.JsEngine;22import java.io.File;23import java.util.HashMap;24import java.util.Map;25public class 5 {26 public static void main(String[] args) {27 String script = "function add(a, b) { return a + b }";28 JsEngine engine = JsEngine.create();29 JsExecutable exec = engine.compile(script);30 Map<String, Object> map = new HashMap();31 map.put("a", 10);32 map.put("b", 20);33 JsValue value = exec.execute(map);34 System.out.println(value);35 }36}37import com.intuit.karate.graal.JsExecutable;38import com.intuit.karate.graal.JsValue;39import com.intuit.karate.graal.JsEngine;40import java.io.File;41import java.util.HashMap;42import java.util.Map;43public class 6 {44 public static void main(String[] args) {45 String script = "function add(a, b) { return a + b }";46 JsEngine engine = JsEngine.create();47 JsExecutable exec = engine.compile(script);48 Map<String, Object> map = new HashMap();49 map.put("a", 10);50 map.put("b", 20);

Full Screen

Full Screen

execute

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.graal.JsExecutable;2public class 4 {3public static void main(String[] args) throws Exception {4String jsCode = "function add(a, b) { return a + b; }";5JsExecutable exec = new JsExecutable(jsCode, "add");6Object result = exec.execute(1, 2);7System.out.println(result);8}9}

Full Screen

Full Screen

execute

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.graal.JsExecutable;2import java.util.HashMap;3import java.util.Map;4public class 4 {5 public static void main(String[] args) {6 String script = "function() { return 'Hello World' }";7 String[] argNames = new String[0];8 JsExecutable jsExecutable = new JsExecutable(script, argNames);9 Map<String, Object> argsMap = new HashMap<>();10 Object result = jsExecutable.execute(argsMap);11 System.out.println(result);12 }13}14import com.intuit.karate.graal.JsExecutable;15import java.util.HashMap;16import java.util.Map;17public class 5 {18 public static void main(String[] args) {19 String script = "function() { return 'Hello World' }";20 String[] argNames = new String[0];21 JsExecutable jsExecutable = new JsExecutable(script, argNames);22 Map<String, Object> argsMap = new HashMap<>();23 Object result = jsExecutable.execute(argsMap);24 System.out.println(result);25 }26}27import com.intuit.karate.graal.JsExecutable;28import java.util.HashMap;29import java.util.Map;30public class 6 {31 public static void main(String[] args) {32 String script = "function() { return 'Hello World' }";33 String[] argNames = new String[0];34 JsExecutable jsExecutable = new JsExecutable(script, argNames);35 Map<String, Object> argsMap = new HashMap<>();36 Object result = jsExecutable.execute(argsMap);37 System.out.println(result);38 }39}40import com.intuit.karate.graal.JsExecutable;41import java.util.HashMap;42import java.util.Map;43public class 7 {44 public static void main(String[] args) {45 String script = "function() { return 'Hello World' }";46 String[] argNames = new String[0];47 JsExecutable jsExecutable = new JsExecutable(script, argNames);

Full Screen

Full Screen

execute

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.graal.JsExecutable;2public class 4 {3 public static void main(String[] args) {4 JsExecutable jsExecutable = new JsExecutable("function(){return 'hello world'}");5 System.out.println(jsExecutable.execute());6 }7}8var JsExecutable = Java.type('com.intuit.karate.graal.JsExecutable');9var jsExecutable = new JsExecutable('function(){return "hello world"}');10print(jsExecutable.execute());

Full Screen

Full Screen

execute

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.graal.JsExecutable;2import com.intuit.karate.graal.JsFunction;3import com.intuit.karate.graal.JsValue;4public class 4 {5 public static void main(String[] args) {6 JsExecutable jsExecutable = JsExecutable.of(""7 + "function add(a, b) {"8 + " return a + b;"9 + "}");10 JsFunction jsFunction = jsExecutable.execute();11 JsValue jsValue = jsFunction.invoke(1, 2);12 System.out.println(jsValue.asInt());13 }14}

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 JsExecutable

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful