{"record":{"id":"e81b6af81d721a50","repo":"karatelabs/karate","slug":"karate-callonce-is-not-available-in-this-context","errorCode":null,"errorMessage":"karate.callonce() is not available in this context","messagePattern":"karate\\.callonce\\(\\) is not available in this context","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsBase.java","lineNumber":387,"sourceCode":"    JavaInvokable setupOnce() {\n        return args -> {\n            ScenarioRuntime rt = getRuntime();\n            if (rt == null) {\n                throw new RuntimeException(\"karate.setupOnce() is not available in this context\");\n            }\n            String name = args.length > 0 && args[0] != null ? args[0].toString() : null;\n            return rt.executeSetupOnce(name);\n        };\n    }\n\n    /**\n     * karate.callonce() - Execute a feature file once per feature and cache the result.\n     */\n    JavaInvokable callonce() {\n        return args -> {\n            ScenarioRuntime rt = getRuntime();\n            if (rt == null) {\n                throw new RuntimeException(\"karate.callonce() is not available in this context\");\n            }\n            if (args.length == 0) {\n                throw new RuntimeException(\"karate.callonce() requires at least one argument (feature path)\");\n            }\n            String path = args[0].toString();\n            Object arg = args.length > 1 ? args[1] : null;\n            return rt.executeJsCallOnce(path, arg);\n        };\n    }\n\n    /**\n     * karate.callSingle() - Execute a feature/JS file once per Suite and cache the result.\n     */\n    JavaInvokable callSingle() {\n        return args -> {\n            ScenarioRuntime rt = getRuntime();\n            if (rt == null) {\n                throw new RuntimeException(\"karate.callSingle() is not available in this context\");","sourceCodeStart":369,"sourceCodeEnd":405,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsBase.java#L369-L405","documentation":"karate.callonce() caches the result of a feature call per feature, but it needs a live ScenarioRuntime to store the cache and execute the call. Karate throws this when callonce is invoked from a JS context with no bound runtime (getRuntime() returns null).","triggerScenarios":"Invoking `karate.callonce('classpath:...')` from JS evaluated outside a scenario: an init script, a non-scenario thread, or a custom embedded JS engine where no ScenarioRuntime is attached.","commonSituations":"Running shared JS bootstrap code outside a feature; migrating callonce from older Karate versions into a new execution path; calling karate JS helpers from Java-embedded Nashorn/Graal contexts.","solutions":["Invoke karate.callonce() only from within a running scenario step so a ScenarioRuntime is bound.","For suite-wide caching outside scenarios, use karate.callSingle() from a scenario, or the Runner/Suite-level APIs.","Ensure custom JS invocation code binds the current ScenarioRuntime before evaluating the script.","Check you are not accidentally calling callonce in a context where the runtime was cleared (e.g. after scenario teardown)."],"exampleFix":"// before: standalone JS with no runtime\nvar data = karate.callonce('classpath:shared.feature');\n\n// after: inside a scenario step\n// * def data = karate.callonce('classpath:shared.feature')","handlingStrategy":"try-catch","validationCode":"if (typeof rt !== 'undefined' && rt != null) { karate.callonce('classpath:shared.feature'); }","typeGuard":"function canCallOnce() { try { return typeof karate.callonce === 'function' && !!getRuntime(); } catch (e) { return false; } }","tryCatchPattern":"try { return karate.callonce(path, arg); } catch (e) { if (String(e.message).includes('not available in this context')) { throw new Error('callonce must run inside a scenario: ' + path); } throw e; }","preventionTips":["Invoke callonce only within scenario steps","Use callSingle for suite-level caching instead","Keep bootstrap JS out of non-scenario contexts"],"tags":["javascript","runtime-context","callonce","karate"],"backgroundTag":"invalid-state-transition","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}