How to use isCallSyntax method of com.intuit.karate.core.ScenarioEngine class

Best Karate code snippet using com.intuit.karate.core.ScenarioEngine.isCallSyntax

Source:ScenarioEngine.java Github

copy

Full Screen

...1779 }1780 public static final boolean isWithinParentheses(String text) {1781 return text != null && text.startsWith("(") && text.endsWith(")");1782 }1783 public static final boolean isCallSyntax(String text) {1784 return text.startsWith("call ");1785 }1786 public static final boolean isCallOnceSyntax(String text) {1787 return text.startsWith("callonce ");1788 }1789 public static final boolean isGetSyntax(String text) {1790 return text.startsWith("get ") || text.startsWith("get[");1791 }1792 public static final boolean isJson(String text) {1793 return text.startsWith("{") || text.startsWith("[");1794 }1795 public static final boolean isXml(String text) {1796 return text.startsWith("<");1797 }1798 public static boolean isXmlPath(String text) {1799 return text.startsWith("/");1800 }1801 public static boolean isXmlPathFunction(String text) {1802 return text.matches("^[a-z-]+\\(.+");1803 }1804 public static final boolean isJsonPath(String text) {1805 return text.indexOf('*') != -1 || text.contains("..") || text.contains("[?");1806 }1807 public static final boolean isDollarPrefixed(String text) {1808 return text.startsWith("$");1809 }1810 public static final boolean isDollarPrefixedJsonPath(String text) {1811 return text.startsWith("$.") || text.startsWith("$[") || text.equals("$");1812 }1813 public static StringUtils.Pair parseCallArgs(String line) {1814 int pos = line.indexOf("read(");1815 if (pos != -1) {1816 pos = line.indexOf(')');1817 if (pos == -1) {1818 throw new RuntimeException("failed to parse call arguments: " + line);1819 }1820 return new StringUtils.Pair(line.substring(0, pos + 1), StringUtils.trimToNull(line.substring(pos + 1)));1821 }1822 pos = line.indexOf(' ');1823 if (pos == -1) {1824 return new StringUtils.Pair(line, null);1825 }1826 return new StringUtils.Pair(line.substring(0, pos), StringUtils.trimToNull(line.substring(pos)));1827 }1828 public Variable call(Variable called, Variable arg, boolean sharedScope) {1829 switch (called.type) {1830 case JS_FUNCTION:1831 case JAVA_FUNCTION:1832 return arg == null ? executeFunction(called) : executeFunction(called, new Object[] {arg.getValue()});1833 case FEATURE:1834 Variable res = callFeature(called.getValue(), arg, -1, sharedScope);1835 recurseAndAttach(res.getValue()); // will always be a map, we update entries within1836 return res;1837 default:1838 throw new RuntimeException("not a callable feature or js function: " + called);1839 }1840 }1841 public Variable call(boolean callOnce, String exp, boolean sharedScope) {1842 StringUtils.Pair pair = parseCallArgs(exp);1843 Variable called = evalKarateExpression(pair.left);1844 Variable arg = pair.right == null ? null : evalKarateExpression(pair.right);1845 Variable result;1846 if (callOnce) {1847 result = callOnce(exp, called, arg, sharedScope);1848 } else {1849 result = call(called, arg, sharedScope);1850 }1851 if (sharedScope && result.isMap()) {1852 setVariables(result.getValue());1853 }1854 return result;1855 }1856 private Variable result(ScenarioCall.Result result, boolean sharedScope) {1857 if (sharedScope) { // if shared scope1858 vars.clear(); // clean slate1859 vars.putAll(copy(result.vars, false)); // clone for safety1860 init(); // this will also insert magic variables1861 setConfig(new Config(result.config)); // re-apply config from time of snapshot1862 return Variable.NULL; // since we already reset the vars above we return null1863 // else the call() routine would try to do it again1864 // note that shared scope means a return value is meaningless1865 } else {1866 return result.value.copy(false); // clone result for safety1867 }1868 }1869 private Variable callOnce(String cacheKey, Variable called, Variable arg, boolean sharedScope) {1870 // IMPORTANT: the call result is always shallow-cloned before returning1871 // so that call result (especially if a java Map) is not mutated by other scenarios1872 final Map<String, ScenarioCall.Result> CACHE = runtime.featureRuntime.FEATURE_CACHE;1873 ScenarioCall.Result result = CACHE.get(cacheKey);1874 if (result != null) {1875 logger.trace("callonce cache hit for: {}", cacheKey);1876 return result(result, sharedScope);1877 }1878 long startTime = System.currentTimeMillis();1879 logger.trace("callonce waiting for lock: {}", cacheKey);1880 synchronized (CACHE) {1881 result = CACHE.get(cacheKey); // retry1882 if (result != null) {1883 long endTime = System.currentTimeMillis() - startTime;1884 logger.warn("this thread waited {} milliseconds for callonce lock: {}", endTime, cacheKey);1885 return result(result, sharedScope);1886 }1887 // this thread is the 'winner'1888 logger.info(">> lock acquired, begin callonce: {}", cacheKey);1889 Variable resultValue = call(called, arg, sharedScope);1890 // we clone result (and config) here, to snapshot state at the point the callonce was invoked1891 // this prevents the state from being clobbered by the subsequent steps of this1892 // first scenario that is about to use the result1893 Map<String, Variable> clonedVars = called.isFeature() && sharedScope ? detachVariables() : null;1894 Config clonedConfig = new Config(config);1895 clonedConfig.detach();1896 result = new ScenarioCall.Result(resultValue.copy(false), clonedConfig, clonedVars);1897 CACHE.put(cacheKey, result);1898 logger.info("<< lock released, cached callonce: {}", cacheKey);1899 return resultValue; // another routine will apply globally if needed1900 }1901 }1902 public Variable callFeature(Feature feature, Variable arg, int index, boolean sharedScope) {1903 if (arg == null || arg.isMap()) {1904 ScenarioCall call = new ScenarioCall(runtime, feature, arg);1905 call.setLoopIndex(index);1906 call.setSharedScope(sharedScope);1907 FeatureRuntime fr = new FeatureRuntime(call);1908 fr.run();1909 // VERY IMPORTANT ! switch back from called feature js context1910 THREAD_LOCAL.set(this);1911 FeatureResult result = fr.result;1912 runtime.addCallResult(result);1913 if (result.isFailed()) {1914 KarateException ke = result.getErrorMessagesCombined();1915 throw ke;1916 } else {1917 return new Variable(result.getVariables());1918 }1919 } else if (arg.isList() || arg.isJsOrJavaFunction()) {1920 List result = new ArrayList();1921 List<String> errors = new ArrayList();1922 int loopIndex = 0;1923 boolean isList = arg.isList();1924 Iterator iterator = isList ? arg.<List> getValue().iterator() : null;1925 while (true) {1926 Variable loopArg;1927 if (isList) {1928 loopArg = iterator.hasNext() ? new Variable(iterator.next()) : Variable.NULL;1929 } else { // function1930 loopArg = executeFunction(arg, new Object[] {loopIndex});1931 }1932 if (!loopArg.isMap()) {1933 if (!isList) {1934 logger.info("feature call loop function ended at index {}, returned: {}", loopIndex, loopArg);1935 }1936 break;1937 }1938 try {1939 Variable loopResult = callFeature(feature, loopArg, loopIndex, sharedScope);1940 result.add(loopResult.getValue());1941 } catch (Exception e) {1942 String message = "feature call loop failed at index: " + loopIndex + ", " + e.getMessage();1943 errors.add(message);1944 runtime.logError(message);1945 if (!isList) { // this is a generator function, abort infinite loop !1946 break;1947 }1948 }1949 loopIndex++;1950 }1951 if (errors.isEmpty()) {1952 return new Variable(result);1953 } else {1954 String errorMessage = StringUtils.join(errors, '\n');1955 throw new KarateException(errorMessage);1956 }1957 } else {1958 throw new RuntimeException("feature call argument is not a json object or array: " + arg);1959 }1960 }1961 public Variable evalJsonPath(Variable v, String path) {1962 Json json = Json.of(v.getValueAndForceParsingAsJson());1963 try {1964 return new Variable(json.get(path));1965 } catch (PathNotFoundException e) {1966 return Variable.NOT_PRESENT;1967 }1968 }1969 public static Variable evalXmlPath(Variable xml, String path) {1970 NodeList nodeList;1971 Node doc = xml.getAsXml();1972 try {1973 nodeList = XmlUtils.getNodeListByPath(doc, path);1974 } catch (Exception e) {1975 // hack, this happens for xpath functions that don't return nodes (e.g. count)1976 String strValue = XmlUtils.getTextValueByPath(doc, path);1977 Variable v = new Variable(strValue);1978 if (path.startsWith("count")) { // special case1979 return new Variable(v.getAsInt());1980 } else {1981 return v;1982 }1983 }1984 int count = nodeList.getLength();1985 if (count == 0) { // xpath / node does not exist !1986 return Variable.NOT_PRESENT;1987 }1988 if (count == 1) {1989 return nodeToValue(nodeList.item(0));1990 }1991 List list = new ArrayList();1992 for (int i = 0; i < count; i++) {1993 Variable v = nodeToValue(nodeList.item(i));1994 list.add(v.getValue());1995 }1996 return new Variable(list);1997 }1998 private static Variable nodeToValue(Node node) {1999 int childElementCount = XmlUtils.getChildElementCount(node);2000 if (childElementCount == 0) {2001 // hack assuming this is the most common "intent"2002 return new Variable(node.getTextContent());2003 }2004 if (node.getNodeType() == Node.DOCUMENT_NODE) {2005 return new Variable(node);2006 } else { // make sure we create a fresh doc else future xpath would run against original root2007 return new Variable(XmlUtils.toNewDocument(node));2008 }2009 }2010 public Variable evalJsonPathOnVariableByName(String name, String path) {2011 return evalJsonPath(vars.get(name), path);2012 }2013 public Variable evalXmlPathOnVariableByName(String name, String path) {2014 return evalXmlPath(vars.get(name), path);2015 }2016 public Variable evalKarateExpression(String text) {2017 text = StringUtils.trimToNull(text);2018 if (text == null) {2019 return Variable.NULL;2020 }2021 // don't re-evaluate if this is clearly a direct reference to a variable2022 // this avoids un-necessary conversion of xml into a map in some cases2023 // e.g. 'Given request foo' - where foo is a Variable of type XML2024 if (vars.containsKey(text)) {2025 return vars.get(text);2026 }2027 boolean callOnce = isCallOnceSyntax(text);2028 if (callOnce || isCallSyntax(text)) { // special case in form "callBegin foo arg"2029 if (callOnce) {2030 text = text.substring(9);2031 } else {2032 text = text.substring(5);2033 }2034 return call(callOnce, text, false);2035 } else if (isDollarPrefixedJsonPath(text)) {2036 return evalJsonPathOnVariableByName(RESPONSE, text);2037 } else if (isGetSyntax(text) || isDollarPrefixed(text)) { // special case in form2038 // get json[*].path2039 // $json[*].path2040 // get /xml/path2041 // get xpath-function(expression)2042 int index = -1;...

Full Screen

Full Screen

isCallSyntax

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.core.ScenarioEngine2import com.intuit.karate.core.Feature3import com.intuit.karate.core.FeatureRuntime4import com.intuit.karate.core.FeatureRuntimeOptions5import com.intuit.karate.core.FeatureRuntimeOptionsBuilder6import com.intuit.karate.core.FeatureRuntimeOptionsBuilder7FeatureRuntimeOptionsBuilder builder = new FeatureRuntimeOptionsBuilder()8builder.setCallSyntax(true)9FeatureRuntimeOptions options = builder.build()10FeatureRuntime runtime = new FeatureRuntime(options)11Feature feature = Feature.read("classpath:my.feature")12ScenarioEngine engine = runtime.build(feature)13import com.intuit.karate.core.FeatureRuntime14import com.intuit.karate.core.FeatureRuntimeOptions15import com.intuit.karate.core.FeatureRuntimeOptionsBuilder16import com.intuit.karate.core.FeatureRuntimeOptionsBuilder17FeatureRuntimeOptionsBuilder builder = new FeatureRuntimeOptionsBuilder()18builder.setCallSyntax(true)19FeatureRuntimeOptions options = builder.build()20FeatureRuntime runtime = new FeatureRuntime(options)21import com.intuit.karate.core.FeatureRuntimeOptions22import com.intuit.karate.core.FeatureRuntimeOptionsBuilder23import com.intuit.karate.core.FeatureRuntimeOptionsBuilder24FeatureRuntimeOptionsBuilder builder = new FeatureRuntimeOptionsBuilder()25builder.setCallSyntax(true)26FeatureRuntimeOptions options = builder.build()27import com.intuit.karate.core.FeatureRuntimeOptionsBuilder28FeatureRuntimeOptionsBuilder builder = new FeatureRuntimeOptionsBuilder()29builder.setCallSyntax(true)30import com.intuit.karate.core.FeatureRuntimeOptionsBuilder31FeatureRuntimeOptionsBuilder builder = new FeatureRuntimeOptionsBuilder()32builder.setCallSyntax(true)33import com.intuit.karate.core.FeatureRuntimeOptionsBuilder

Full Screen

Full Screen

isCallSyntax

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.core.ScenarioEngine2import com.intuit.karate.core.FeatureRuntime3import com.intuit.karate.core.FeatureRuntimeOptions4import com.intuit.karate.core.FeatureRuntimeOptions.FeatureRuntimeOptionsBuilder5import com.intuit.karate.core.FeatureRuntimeOptions.FeatureRuntimeOptionsBuilder.FeatureRuntimeOptionsBuilderOptions6FeatureRuntimeOptionsBuilder builder = FeatureRuntimeOptionsBuilder.builder()7FeatureRuntimeOptionsBuilderOptions options = builder.build()8FeatureRuntimeOptions featureRuntimeOptions = options.getFeatureRuntimeOptions()9FeatureRuntime featureRuntime = new FeatureRuntime(feature, featureRuntimeOptions)10ScenarioEngine engine = featureRuntime.getScenarioEngine()11boolean callSyntax = engine.isCallSyntax("def x = 1")12callSyntax = engine.isCallSyntax("def x = call read('classpath:foo.json')")13callSyntax = engine.isCallSyntax("def x = call read('classpath:foo.json') { y = 1 }")14callSyntax = engine.isCallSyntax("def x = call read('classpath:foo.json') { y = 1 } def z = 1")15callSyntax = engine.isCallSyntax("def x = call read('classpath:foo.json') { y = 1 } def z = 1 def w = 2")16callSyntax = engine.isCallSyntax("def x = call read('classpath:foo.json') { y = 1 } def z = 1 def w = 2 def v = 3")17callSyntax = engine.isCallSyntax("def x = call read('classpath:foo.json') { y = 1 } def z = 1 def w = 2 def v = 3 def m = 4")18callSyntax = engine.isCallSyntax("def x = call read('classpath:foo.json') { y = 1 } def z = 1 def w = 2 def v = 3 def m = 4 def n = 5")

Full Screen

Full Screen

isCallSyntax

Using AI Code Generation

copy

Full Screen

1import static com.intuit.karate.core.ScenarioEngine.isCallSyntax2def isCall = isCallSyntax('call read("classpath:sample.feature")')3def isNotCall = isCallSyntax('read("classpath:sample.feature")')4def isNotCall2 = isCallSyntax('read("classpath:sample.feature")')5def isNotCall3 = isCallSyntax('def x = read("classpath:sample.feature")')6def isNotCall4 = isCallSyntax('def x = 1')7def isNotCall5 = isCallSyntax('def x = "1"')8def isNotCall6 = isCallSyntax('def x = 1 + 1')9def isNotCall7 = isCallSyntax('def x = "1" + "1"')10def isNotCall8 = isCallSyntax('def x = 1 + "1"')11def isNotCall9 = isCallSyntax('def x = "1" + 1')12def isNotCall10 = isCallSyntax('def x = 1 + 1 + "1"')13def isNotCall11 = isCallSyntax('def x = "1" + 1 + 1')14def isNotCall12 = isCallSyntax('def x = 1 + 1 + 1')15def isNotCall13 = isCallSyntax('def x = 1 + 1 + 1 + "1"')16def isNotCall14 = isCallSyntax('def x = "1" + 1 + 1 + 1')17def isNotCall15 = isCallSyntax('def x = 1 + 1 + 1 + 1 + "1"')18def isNotCall16 = isCallSyntax('def x = "1" + 1 + 1 + 1 + 1')

Full Screen

Full Screen

isCallSyntax

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.core.ScenarioEngine2def isCallSyntax = engine.isCallSyntax(text)3def isCallSyntax = engine.isCallSyntax("def a = call read('classpath:foo.feature')")4def isCallSyntax = engine.isCallSyntax("def a = call read('classpath:foo.feature')")5def isCallSyntax = engine.isCallSyntax("def a = call read('classpath:foo.feature')")6def isCallSyntax = engine.isCallSyntax("def a = call read('classpath:foo.feature')")7def isCallSyntax = engine.isCallSyntax("def a = call read('classpath:foo.feature')")8def isCallSyntax = engine.isCallSyntax("def a = call read('classpath:foo.feature')")

Full Screen

Full Screen

isCallSyntax

Using AI Code Generation

copy

Full Screen

1 * def isCallSyntax = engine.isCallSyntax()2* def includedScript = karate.read('path/to/script.feature')3* def isCallSyntax = includedScript.isCallSyntax()4* def includedScript = karate.read('path/to/script.feature')5* def isCallSyntax = includedScript.isCallSyntax()6* def includedScript = karate.read('path/to/script.feature')7* def isCallSyntax = includedScript.isCallSyntax()8* def includedScript = karate.read('path/to/script.feature')9* def isCallSyntax = includedScript.isCallSyntax()10* def includedScript = karate.read('path/to/script.feature')11* def isCallSyntax = includedScript.isCallSyntax()12* def includedScript = karate.read('path/to/script.feature')13* def isCallSyntax = includedScript.isCallSyntax()14* def includedScript = karate.read('path/to/script.feature')15* def isCallSyntax = includedScript.isCallSyntax()

Full Screen

Full Screen

isCallSyntax

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.core.ScenarioEngine2import com.intuit.karate.core.Scenario3import com.intuit.karate.core.Feature4def feature = new Feature('test.feature', '''5def scenario = new Scenario(feature, feature.scenarios[0])6def engine = new ScenarioEngine(scenario)7def isCallSyntax = engine.getClass().getDeclaredMethod("isCallSyntax", Scenario.class)8isCallSyntax.setAccessible(true)9assert isCallSyntax.invoke(engine, scenario) == false10def feature2 = new Feature('test.feature', '''11 * call read('test.feature')12def scenario2 = new Scenario(feature2, feature2.scenarios[0])13def engine2 = new ScenarioEngine(scenario2)14def isCallSyntax2 = engine2.getClass().getDeclaredMethod("isCallSyntax", Scenario.class)15isCallSyntax2.setAccessible(true)16assert isCallSyntax2.invoke(engine2, scenario2) == true17def feature3 = new Feature('test.feature', '''18 * def x = call read('test.feature')19def scenario3 = new Scenario(feature3, feature3.scenarios[0])20def engine3 = new ScenarioEngine(scenario3)21def isCallSyntax3 = engine3.getClass().getDeclaredMethod("isCallSyntax", Scenario.class)22isCallSyntax3.setAccessible(true)23assert isCallSyntax3.invoke(engine3, scenario3) == true24def feature4 = new Feature('test.feature', '''25 * def x = call read('test.feature')26def scenario4 = new Scenario(feature4, feature4.scenarios[0])27def engine4 = new ScenarioEngine(scenario4)28def isCallSyntax4 = engine4.getClass().getDeclaredMethod("isCallSyntax", Scenario.class)

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 ScenarioEngine

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful