Various Actions in Karate DSL

Priyanka Brahmane
5 min readAug 19, 2021

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 = $..idAnd print data_idAnd assert data_id[0] == 2
And def first_name = $..first_nameAnd print first_nameAnd assert first_name[0] == "Janet"
And def avatar = $..avatarAnd print avatarAnd assert avatar[0] == "https://reqres.in/img/faces/2-image.jpg"

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! "And replace support_text.replace_text = "CONTRIBUTIONS"And print support_text

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 response.data[*].idAnd print all_ids

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

And def first_id = get response.data[0].idAnd def second_id = get response.data[1].idAnd print "First Id: " + first_id + "," + "Second Id: "+ second_id

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

And set response.data.first_name = "Shirley"And print response

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 response.data.first_name
And def all_ids = get response.data[*].idAnd print all_idsAnd remove all_ids[0,1,2]And print all_ids

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

And def sampleResponseId = 3And eval if(sampleResponseId != 2) karate.fail('Incorrect ID')

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'And def result = sampleResponseId == 'Karate' ? "Equal" : "Unequal"And print result

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

And def sampleResponseId = 'Karate123'And def result = sampleResponseId == 'Karate' ? "Equal" : "Unequal"And print result

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 !!! 😃

--

--

Priyanka Brahmane

AM SDET Automation @M&G | QA Lead @ MyGlamm | QA Automation engineer @ Ex-Paytm Insider| Ex-Automation Tester @ Reliance Jio Infocomm Ltd. | Ex-Software Develop