ADVANCED
Karate Object API
The karate object is a runtime utility available in JavaScript functions, Karate expressions, and karate-config.js. It provides methods for test control, data manipulation, HTTP access, and system integration.
On this page:
- Test Control - abort, fail, pause, signal
- Data Manipulation - filter, map, merge, sort, distinct
- Variable Management - get, set, remove
- File Operations - read, write, paths
- HTTP and Network - call, http, request, response
- Data Conversion - copy, toJson, typeOf, pretty
- Path Operations - jsonPath, xmlPath, extract
- System and Environment - env, os, properties, exec
Test Control
Control test execution flow with abort, fail, and pause operations.
| Method | Description |
|---|---|
karate.abort() | Exit a Scenario early. Combine with conditional logic: * if (condition) karate.abort() |
karate.fail(message) | Stop test with error message: * if (!response.data) karate.fail('No data') |
karate.pause(ms) | Sleep in milliseconds. Only active in performance testing unless configure pauseIfNotPerf is true |
karate.stop(port) | Pause until socket connection on port. For debugging UI tests. Remove after use. |
karate.signal(result) | Trigger event for listen keyword. Pass data to waiting listener. |
karate.listen(timeout) | Wait for karate.signal() event. Result available in listenResult variable. |
Gherkin
Feature: Test control methods
Scenario: Abort on missing data
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 99999
When method get
* if (responseStatus == 404) karate.abort()
Scenario: Fail with message
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
* if (!response.name) karate.fail('User name is required')
Data Manipulation
Transform and filter arrays and objects using functional operations.
| Method | Description |
|---|---|
karate.append(...items) | Create array from items (can include arrays). Useful for JSON transforms. |
karate.appendTo(name, ...items) | Append to existing array variable. First arg can be reference or variable name string. |
karate.distinct(list) | Return unique items from array of strings or numbers. |
karate.filter(list, predicate) | Filter array. Predicate: (item, [index]) => boolean |
karate.filterKeys(map, keys) | Extract key-value subset. Keys can be array or JSON object (uses only keys). |
karate.forEach(list, function) | Iterate array or object. Function: (item, [index]) for arrays, (key, [value], [index]) for objects. |
karate.keysOf(object) | Return only keys of map-like object. |
karate.lowerCase(object) | Convert all keys and values to lowercase. |
karate.map(list, function) | Transform array. Function: (item, [index]) => newItem |
karate.mapWithKey(list, key) | Transform array of primitives to array of objects with given key. |
karate.merge(...maps) | Merge multiple JSON objects. Later objects override earlier keys. |
karate.range(start, end, [interval]) | Return array of integers (inclusive). Default interval is 1. |
karate.repeat(count, function) | Build array or execute function count times. |
karate.sizeOf(object) | Return size of array or object. |
karate.sort(list, function) | Sort array. Function: (item) => sortValue. Optional for simple arrays. |
karate.valuesOf(object) | Return only values of map-like object. |
Chain filter, map, and distinct to extract unique names from a filtered subset of users. Use merge to combine configuration objects with later values overriding earlier ones:
Gherkin
Feature: Data manipulation
Scenario: Filter and transform arrays
Given url 'https://jsonplaceholder.typicode.com'
And path 'users'
When method get
Then status 200
* def activeUsers = karate.filter(response, x => x.id <= 5)
* def names = karate.map(activeUsers, x => x.name)
* def uniqueNames = karate.distinct(names)
* match karate.sizeOf(uniqueNames) == 5
Scenario: Merge and sort objects
* def defaults = { timeout: 5000, retries: 3 }
* def overrides = { timeout: 10000 }
* def config = karate.merge(defaults, overrides)
* match config == { timeout: 10000, retries: 3 }
* def items = [{ priority: 2 }, { priority: 1 }, { priority: 3 }]
* def sorted = karate.sort(items, x => x.priority)
* match sorted[0].priority == 1
Avoid JavaScript Loops
Prefer karate.filter(), karate.map(), and karate.forEach() over JavaScript for loops. Use karate.repeat() when you need to execute something a fixed number of times.