Variables and their type conversion using KarateDSL

Hola TestCommunity!!!

I’ve covered a few fundamental topics in Karate DSL so far. If you haven’t read any of the previous blogs, here’s a link to get you started.

You’re reading the 6th chapter of my karate DSL series, which is designed to help you grasp different variables and their type conversions.

What is a variable and why is type conversion significant?

A Variables are used to hold data in a computer programme that may be referenced and changed. In short, Variables can be thought of as information-holding boxes. They exist just to categorize and store data in memory.

Type conversions is the process of converting a variable from one data type to another. We may convert string/text/yaml/xml to json.

Let’s take a closer look at each variable and understand in detail-

def - This keyword is typically for defining a new variable. It is a powerful keyword that can be used for storing any type of variable such as number, string, to read JS function, JSON, Array, JSONPath, XML and even JS-Java.

We can you custom names for variables.It is the most preferred karate keyword.

Let us have a look at few examples stated below-

To define a number:

Feature: Type definition Demo

To define a string:

Feature: Type definition Demo

To define a JS Function and JSONPath:

Scenario: Define a JS function and JSONPath

We can declare a JSON in a single line or in a multi-line JSON wrapped between three double inverted commas (“””).

Scenario: Define a JSON in 2 techniques - single line and multi-line

To define an Array:

# Array is defined from above json

string- It is used convert JSON or any other data-type (except XML) to a string.


Feature: Type definition Demo

For more examples, refer this link.

text- This is used to treat payload as raw text. I can be used to disable karate’s default behaviour of parsing everything that looks like JSON or XML.

In the karate documentation, it is stated that — this is especially relevant when manipulating GraphQL queries because although they look suspiciously like JSON, they are not, and tend to confuse Karate’s internals.

It can be used with “Scenario Outline” and “Examples” keyword.

Feature: Type definition Demo

json - Using this keyword, JSON can be created from XML, a map-like or list-like object, a text or even a Java object.

Scenario: Define a JSON

table- It’s used to turn all of the columns into JSON. It can be used to solve nested complex scenarios that are dependent on dynamic data.

Feature: Type definition Demo

yaml- YAML is a human interaction oriented data serialization language. JSON, another data serialization language, is a tight superset of it.
To know more about yaml, refer this blog.

In exceptional cases, the yaml type cast keyword can be used to convert a YAML string to JSON.

The text in the example below is actually a yaml payload. Assigning a defined reference variable with the “yaml” variable type is used to type cast this payload.

Feature: Type definition Demo

csv- It is used to convert a string that is in CSV(Comma Separated Values) format into JSON.

Feature: Type definition Demo

copy- As the name implies, this keyword is used to duplicate a payload to a variable reference.

Copy can be used in situations where a change to the payload is requested while preserving a copy of the original payload for matching/assertion/reference purposes.

A JSON, XML, Map or List can be copied to a reference variable.

Feature: Type definition Demo

For more example, refer this feature file.

It’s time for me to wrap up this blog, anticipating that the examples presented have helped you understand the various types of variables and their type conversions. Stay tuned for more karate DSL concepts in upcoming blogs.

Please share your thoughts in the comments section below and be sure to click the Follow button to stay updated with the latest blogs. You may find me on LinkedIn as well.

Keep yourself safe and happy learning!!!

Until then, Sayonara!! 😃

QA Lead @ MyGlamm | QA Automation engineer @ Ex-Paytm Insider| Ex-Automation Tester @ Reliance Jio Infocomm Ltd. | Ex-Software Developer @TCS