Various Actions in Karate DSL

Hello TestCommunity!!!

I’m glad to welcome you back to the 7th blog of my Karate DSL series, which covers different types of karate actions. With the use of practical examples, this blog will guide you in comprehending the use of actions.

But, hang in there!!! If you missed any of the prior blogs, don’t worry, here’s a link to get you started. This blog series has been carefully curated to cover all of the fundamental concepts and assist you in learning more about this innovative API Automation framework created by Peter Thomas sir.

Are you eager to learn more about karate?

If that’s the case, let’s get started on this article by learning about the various types of actions that are supported by karate in the following section:

assert- This keyword is used to assert a value and returns it a boolean i.e. test case will pass if assertion returns “true” else it will fail if assertion is “false”

And def data_id = $

print- it is used to print a variable/response on the console in the middle of a script.

And print variable_name

replace- is used to replace words in a text or string. It is most used in the scenarios where we required dynamic string substitution like SQL. Replace can even be used in replacing values in response.

And def support_text = "To keep ReqRes free, <replace_text> towards server costs are appreciated! "

get- is used to get elements from a list-like result (JSON array) and store it in a variable for later use. The result is returned as an array.

The “*” (asterisk) is used to get a specific key from all the elements of a JSON array.

And def all_ids = get[*].id

The index value in square bracket can be used to access a specific element from the resultant array.

And def first_id = get[0].id

set- It is simply used to set values on JSON documents.

And set = "Shirley"

remove- is used to remove key or data elements from a JSON or XML response. JSON array elements can even be removed by index.

And remove
And def all_ids = get[*].id

eval- is used to evaluate a condition and return/perform a desired value/action.

And def sampleResponseId = 3

If you have a scenario where if - then - else check is required then this can be achieved by using conditional logic syntax as stated in below example:

Positive Case- where the condition is true i.e. Equal.

And def sampleResponseId = 'Karate'

Negative Case- where the condition is false i.e. Unequal.

And def sampleResponseId = 'Karate123'

read()- this method is used for reading a file. The file (that is to be read) should be in the same package as the *.feature file by default. All files ending with file .json, .xml, .yaml, .js, .csv or .txt can be read using this built-in JavaScript function.

And def SampleFileResponse = read('SampleResponse.json')

call- is used to reusing common code. It can be used to call a feature file or a JavaScript function.

And def SampleFeaturefile = call read('ReusableFeature.feature')

callonce- The callonce keyword in Karate works in the same way as call but only executes once. The first call’s results are cached, and subsequent calls will simply return the cached result rather than repeatedly using the JavaScript function (or feature).

And def SampleFeaturefile = callonce read('ReusableFeature.feature')

I am going to wrap up this blog now, hoping that it has given you some insight into how to use actions in KarateDSL.

Is this series helpful in learning about Karate DSL? I will love to hear your thoughts in the comments box below. If you found this blog to be informative, please click the follow icon on Medium to receive updates on on more intriguing concepts of this tool. You can also reach out to me on LinkedIn.

Until then stay safe and keep learning!!! Ciao !!! 😃

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