Class KarateJs

java.lang.Object
io.karatelabs.core.KarateJs
All Implemented Interfaces:
PerfContext, io.karatelabs.js.ObjectLike, io.karatelabs.js.SimpleObject

public class KarateJs extends Object implements PerfContext
Main implementation of the karate.* JavaScript API.

This class contains methods that require stateful access to: - The JavaScript engine (for variable lookup and evaluation) - Runtime context via KarateJsContext (scenario, feature, config) - HTTP client and resource resolution - Mock handler context

Stateless utility methods are delegated to KarateJsUtils. Shared state and infrastructure are inherited from KarateJsBase.

See Also:
  • Field Details

    • root

      public final io.karatelabs.common.Resource root
    • engine

      public final io.karatelabs.js.Engine engine
    • client

      public final HttpClient client
    • http

      public final HttpRequestBuilder http
  • Constructor Details

    • KarateJs

      public KarateJs(io.karatelabs.common.Resource root)
    • KarateJs

      public KarateJs(io.karatelabs.common.Resource root, HttpClientFactory factory)
    • KarateJs

      public KarateJs(io.karatelabs.common.Resource root, HttpClient client)
  • Method Details

    • setPrevRequest

      public void setPrevRequest(HttpRequest request)
      Track the previous HTTP request for karate.prevRequest. Called by HttpRequestBuilder after successful HTTP calls.
    • setPrevResponse

      public void setPrevResponse(HttpResponse response)
      Track the previous HTTP response, used to back karate.response in non-mock context. Called by StepExecutor after each HTTP method invocation.
    • capturePerfEvent

      public void capturePerfEvent(String name, long startTime, long endTime)
      Capture a custom performance event (implements PerfContext).

      When running under Gatling, this event will be reported to the statistics engine. When not in performance mode, this is a no-op.

      Usage in Java helpers:

       public static Object myRpc(Map args, PerfContext karate) {
           long start = System.currentTimeMillis();
           // ... custom logic ...
           long end = System.currentTimeMillis();
           karate.capturePerfEvent("myRpc", start, end);
           return result;
       }
       
      Specified by:
      capturePerfEvent in interface PerfContext
      Parameters:
      name - the event name for Gatling reports
      startTime - the start time in epoch milliseconds
      endTime - the end time in epoch milliseconds
    • markup

      public Markup markup()
      Lazy-initialize and return the Markup template engine. Used for doc() and render() template processing.
    • doc

      public String doc(Map<String,Object> options)
      Renders an HTML template and returns the result. Also sends to onDoc consumer if set. Called by the 'doc' keyword in StepExecutor.
    • jsGet

      public Object jsGet(String key)
      Specified by:
      jsGet in interface io.karatelabs.js.SimpleObject
    • setJavaBridgeEnabled

      public void setJavaBridgeEnabled(boolean enabled)
      Enable or disable Java interop (Java.type(...) and reflective dispatch) for this engine. When disabled the external bridge is removed: Java.type(...) throws "java bridge not enabled" and a bare fully-qualified class reference resolves to null. The Mock Server disables this by default so that embedded expressions found in untrusted HTTP request data cannot reach Java class loading / command execution; a mock feature that genuinely needs Java interop can opt back in with configure javaBridgeEnabled = true.
    • setOnDoc

      public void setOnDoc(Consumer<String> onDoc)
    • setResourceResolver

      public void setResourceResolver(ResourceResolver resourceResolver)
    • setOnMatch

      public void setOnMatch(BiConsumer<io.karatelabs.js.Context,Result> onMatch)
    • setContext

      public void setContext(KarateJsContext context)
    • setEnv

      public void setEnv(String env)
    • setMockHandler

      public void setMockHandler(MockHandler handler)
    • setOutputDir

      public void setOutputDir(String outputDir)