Best Karate code snippet using com.intuit.karate.core.ScenarioEngine.callFeature
Source:ScenarioEngine.java  
...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 {...callFeature
Using AI Code Generation
1import com.intuit.karate.core.ScenarioEngine2import com.intuit.karate.core.Feature3import com.intuit.karate.core.FeatureRuntime4    * def engine = new ScenarioEngine()5    * def feature = Feature.read('classpath:com/intuit/karate/core/call.feature')6    * def runtime = new FeatureRuntime(feature, engine)7    * engine.callFeature(feature, scenario, null)8    * match response == { foo: 'bar' }9import com.intuit.karate.core.ScenarioEngine10import com.intuit.karate.core.Feature11import com.intuit.karate.core.FeatureRuntime12    * def engine = new ScenarioEngine()13    * def feature = Feature.read('classpath:com/intuit/karate/core/call.feature')14    * def runtime = new FeatureRuntime(feature, engine)15    * engine.callFeature(feature, scenario, null)16    * match response == { foo: 'bar' }17import com.intuit.karate.core.ScenarioEngine18import com.intuit.karate.core.Feature19import com.intuit.karate.core.FeatureRuntime20    * def engine = new ScenarioEngine()21    * def feature = Feature.read('classpath:com/intuit/karate/core/call.feature')22    * def runtime = new FeatureRuntime(feature, engine)23    * engine.callFeature(feature, scenario, null)24    * match response == { foo: 'bar' }25import com.intuit.karate.core.ScenarioEngine26import com.intuit.karate.core.Feature27import com.intuit.karate.core.FeatureRuntimecallFeature
Using AI Code Generation
1* def callFeature = engine.callFeature(feature)2* assert callFeatureResult.getPassed() == 13* assert callFeatureResult.getFailed() == 04* assert callFeatureResult.getSkipped() == 05* assert callFeatureResult.getPassed() == 16* assert callFeatureResult.getFailed() == 07* assert callFeatureResult.getSkipped() == 08* assert callFeatureResult.getPassed() == 19* assert callFeatureResult.getFailed() == 010* assert callFeatureResult.getSkipped() == 011* def callFeatureResultPassed = callFeatureResult.getPassed()12* def callFeatureResultFailed = callFeatureResult.getFailed()13* def callFeatureResultSkipped = callFeatureResult.getSkipped()14* assert callFeatureResult.getPassed() == 115* assert callFeatureResult.getFailed() == 016* assert callFeatureResult.getSkipped() == 0callFeature
Using AI Code Generation
1* call read('classpath:sample.feature')2* def args = { "name": "John" }3* call read('classpath:sample.feature') args4* def args = { "name": "John" }5* def response = call read('classpath:sample.feature') args6* match response == { "message": "Hello John" }7* def response = call read('classpath:sample.feature')8* match response == { "message": "Hello World" }9* def args = { "name": "John" }10* def response = call read('classpath:sample.feature') args11* match response == { "message": "Hello John" }12* def args = { "name": "John" }13* def response = call read('classpath:sample.feature') args14* match response == { "message": "Hello John" }15* def args = { "name": "John" }16* def response = call read('classpath:sample.feature') args17* match response == { "message": "Hello John" }callFeature
Using AI Code Generation
1* def result = engine.callFeature('classpath:com/intuit/karate/core/demo.feature')2* match result == { a: 1, b: 2 }3* def result = feature.callFeature('classpath:com/intuit/karate/core/demo.feature')4* match result == { a: 1, b: 2 }5* def result = feature.callFeature('classpath:com/intuit/karate/core/demo.feature', { a: 3, b: 4 })6* match result == { a: 3, b: 4 }7* def result = feature.callFeature('classpath:com/intuit/karate/core/demo.feature', { a: 3, b: 4 }, { config: 'value' })8* match result == { a: 3, b: 4 }9* def result = feature.callFeature('classpath:com/intuit/karate/core/demo.feature', { a: 3, b: 4 }, { config: 'value' })10* match result == { a: 3, b: 4 }11* def result = feature.callFeature('classpath:com/intuit/karate/core/demo.feature', { a: 3, b: 4 }, { config: 'value' }, [ 'tag1', 'tag2' ])12* match result == { a: 3callFeature
Using AI Code Generation
1* def variables = { 'name': 'John', 'age': 20 }2* callFeature('call.feature', variables)3* def variables = { 'name': 'John', 'age': 20 }4* def result = callFeature('call.feature', variables)5* def variables = { 'name': 'John', 'age': 20 }6* def result = callFeature('call.feature', variables)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!!
