Writing API Tests with Karate by Benjamin Bischoff

Writing API Tests with Karate by Benjamin Bischoff

Author:Benjamin Bischoff
Language: eng
Format: epub
Publisher: Packt
Published: 2023-01-15T00:00:00+00:00


Using embedded expressions with JSON

There can be use cases where you need to have a larger JSON or XML file that you need to use as a request payload but that should be partly dynamic. Karate supports such a templating approach. This means that you can define the outline of a JSON or XML file and define placeholders that can be set dynamically. These templates can either be defined within a scenario or read from an external file (we will explore the different file options in the next section of this chapter).

This example shows the power of this approach. It uses Karate expressions to construct a JSON object that represents a book.

All keys are given here, but all values come from variables and functions:

Scenario: Using JSON templates * def title = 'Testing APIs' * def pages = 250 * def chapters = ['Core concepts', 'Setup'] * def titleBackwards = function(title) { return title.split("").reverse().join(""); } * def format = null * def book = """ { title: '#(title)', pages: #(pages), chapters: #(chapters), totalChapters: #(chapters.length), totalChaptersPlusIntro: #(chapters.length + 1), titleBackwards: '#(titleBackwards(title))', format: '##(format)' } """ * print book

There is quite a lot to uncover here because this feature is quite powerful.

The general approach to using templating in Karate is the #() expression syntax. Everything that is between the parentheses is parsed and executed, and expressions must always start with #( and end with ). This way, variables and functions can be used here to replace these expressions.

Also, it is important to enclose expressions within quotes when they are strings. Otherwise, the resulting JSON will be invalid. An example of such a case is the '#(title)' expression in our book JSON.

In the case of the chapters: #(chapters) line in our JSON template, it is important to note that you cannot just replace an expression with a string or numeric value but also with more complex structures and even full JSON objects. In our case, an array of strings representing chapter titles is injected here.

This approach is not limited to variables, but you can use pretty much all JavaScript functionality in here, even externally defined functions. The totalChapters: #(chapters.length) line shows that we can use the array length of our injected chapters array directly without any custom hacks—this represents the total number of chapters in the book (in our example case, it is 2).

totalChaptersPlusIntro: #(chapters.length + 1) demonstrates that you can even make calculations right within an expression. To take this even further, titleBackwards: '#(titleBackwards(title))' uses a custom function that is defined in the titleBackwards variable. This can then be used again right in the expression.

The last element, format, is special. Here, we use Karate’s ## optional marker to remove this element completely if it is null. In this case, there will be no format key and value in the resulting JSON as the format variable is deliberately set to null here.

Here is the complete resulting JSON array with the replaced expressions highlighted:

20:06:26.323 [pool-1-thread-1] INFO com.intuit.karate - [print] { "title": "Testing APIs", "pages": 250,



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.