{"record":{"id":"93c666f1893c40e6","repo":"karatelabs/karate","slug":"image-write-needs-name-path-bytes","errorCode":null,"errorMessage":"image.write: needs (name|path, bytes)","messagePattern":"image\\.write: needs \\(name\\|path, bytes\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-image/src/main/java/io/karatelabs/ext/image/ImageApi.java","lineNumber":345,"sourceCode":"        String baselineLeaf = hasExt ? name : findBaselineLeaf(base);\n        String baselineJoined = join(str(config.get(\"baselineDir\")), baselineLeaf);\n\n        Map<String, Object> out = new LinkedHashMap<>();\n        out.put(\"baselinePath\", absolute(baselineJoined));\n        out.put(\"optionsPath\", absolute(join(optionsDir(), base + \".json\")));\n        out.put(\"baselineExists\", resourceExists(baselineJoined));\n        return out;\n    }\n\n    /**\n     * {@code image.write(name|path, bytes)} — write image bytes to the resolved baseline\n     * (by name) or to an explicit path; returns the absolute path written. The recipe uses\n     * this for auto-establish and programmatic rebase ({@code karate.write} can't target an\n     * absolute path outside the report output dir).\n     */\n    private Object writeVerb(Object... args) {\n        if (args.length < 2) {\n            throw new RuntimeException(\"image.write: needs (name|path, bytes)\");\n        }\n        String first = str(args[0]);\n        String path = looksLikePath(first) ? first : str(resolve(first).get(\"baselinePath\"));\n        writeBytes(path, toBytes(args[1]));\n        return absolute(path);\n    }\n\n    /** Existing {@code <baselineDir>/<base>.<ext>} for any known image ext, else default png. */\n    private String findBaselineLeaf(String base) {\n        String dir = str(config.get(\"baselineDir\"));\n        for (String ext : IMAGE_EXTS) {\n            String leaf = base + \".\" + ext;\n            Resource res = resolveResource(join(dir, leaf));\n            if (res != null && res.exists()) {\n                return leaf;\n            }\n        }\n        return base + \".png\";","sourceCodeStart":327,"sourceCodeEnd":363,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-image/src/main/java/io/karatelabs/ext/image/ImageApi.java#L327-L363","documentation":"The image.write verb writes image bytes either to a named baseline or to an explicit path and returns the absolute path written. It requires exactly (target, bytes); fewer than two arguments throws RuntimeException with this message.","triggerScenarios":"karate.call('image.write') with only a name/path and no byte payload, or with no arguments at all — e.g. forgetting to pass the screenshot bytes, or the second argument being dropped in a dynamic call.","commonSituations":"Capturing a screenshot into a variable that is null/undefined so the call effectively has one argument; copy-pasting the resolve call shape (single arg) for write; building args programmatically and omitting the bytes entry.","solutions":["Call with both args: karate.call('image.write', 'screenshot-home', screenshotBytes)","Or with an explicit path: karate.call('image.write', '/abs/path/baseline.png', bytes)","Verify the screenshot/byte variable is non-null before calling","Remember karate.write cannot target absolute paths — use image.write for that"],"exampleFix":"// before\nkarate.call('image.write', 'screenshot-home'); // missing bytes\n// after\ndef bytes = karate.scenario.embed(screenshot, 'image/png');\nkarate.call('image.write', 'screenshot-home', bytes);","handlingStrategy":"validation","validationCode":"# karate\n* if (!bytes) karate.abort('screenshot bytes missing before image.write')\n* def path = karate.call('image.write', name, bytes)","typeGuard":null,"tryCatchPattern":"try { return karate.call('image.write', target, bytes); } catch (Exception e) { if (('' + e).contains('needs (name|path, bytes)')) throw new IllegalStateException(\"image.write missing args: target=\" + target + \", bytesNull=\" + (bytes == null)); throw e; }","preventionTips":["Capture the screenshot into a variable and assert it is non-null before writing","Remember image.write takes exactly two arguments","Use image.write (not karate.write) for absolute paths"],"tags":["karate","image-comparison","missing-argument"],"backgroundTag":"missing-required-argument","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"}