CORE SYNTAX
Actions
Load files, evaluate JavaScript dynamically, and embed HTML documentation into test reports using Karate's built-in action keywords.
On This Page
- read() - Load files with auto-conversion (test data, schemas, features)
- karate.readAsString() - Load files as raw text (CSV uploads, templates)
- doc - Embed HTML in reports (custom documentation, visualization)
- eval - Execute JavaScript dynamically (use sparingly)
Reading Files
Karate's read() function loads files from your project with automatic type conversion. Use it to reuse test data, schemas, and utility functions across your test suite.
Loading Test Data
Read JSON and XML files for request payloads and validation:
Gherkin
Feature: Loading test data
Scenario: Read JSON and XML files
# JSON files auto-convert to objects
* def userData = read('user-data.json')
* def userArray = read('users-list.json')
# XML files auto-convert to XML objects
* def configXml = read('config.xml')
* def orderXml = read('sample-order.xml')
Loading Tabular Data
Read CSV and YAML files as structured data:
Gherkin
Feature: Loading tabular data
Scenario: Read CSV and YAML files
# CSV files become JSON arrays (header row required)
* def users = read('users.csv')
# YAML files become JSON objects
* def config = read('config.yaml')
Loading Reusable Code
Read JavaScript files to reuse functions across tests:
Gherkin
Feature: Loading reusable code
Scenario: Read JavaScript functions
# JavaScript files evaluate to functions
* def utils = read('helpers.js')
* def result = utils.processData({ name: 'John' })
# Call JavaScript functions directly
* def transform = read('data-transform.js')
* def transformed = transform(userData)
Loading Templates and Text
Read text files for templates and queries:
Gherkin
Feature: Loading templates
Scenario: Read text files
# Text files load as strings
* def emailTemplate = read('email-template.txt')
* def sqlQuery = read('user-query.sql')
* def graphqlQuery = read('user-query.graphql')
Loading Binary Files
Read binary files for upload operations:
Gherkin
Feature: Loading binary files
Scenario: Read binary files for uploads
# Binary files load as byte arrays
* def pdfFile = read('document.pdf')
* def imageFile = read('logo.png')
# Use in multipart uploads
Given url baseUrl
And path 'upload'
And multipart file document = { read: 'document.pdf' }
When method post
Then status 200
Path Prefixes
Control where files are loaded from using path prefixes:
| Prefix | Description | Example | Best For |
|---|---|---|---|
| classpath: | From classpath root | classpath:auth/login.feature | Shared, reusable resources |
| this: | Relative to current feature | this:helper.feature | Called features |
| file: | Absolute file paths | file:/tmp/output.json | Development only |
| (none) | Relative to feature file | user-data.json | Feature-local resources |
Path Prefix Best Practice
Use classpath: prefix for shared resources that are reused across features, and relative paths for feature-local files.
Gherkin
Feature: Using path prefixes
Scenario: Load files from different locations
# Shared resources (recommended for reuse)
* def commonAuth = read('classpath:auth/login.feature')
* def sharedData = read('classpath:common/test-data.json')
# Feature-local files
* def localData = read('user-data.json')
* def schema = read('user-schema.json')
# Called features (ensures correct relative paths)
* def helper = read('this:helper-scenario.feature')
# Environment-specific files (dynamic loading)
* def env = karate.env || 'dev'
* def config = read('classpath:config/' + env + '.json')